Java_ElasticSearch(ES)——分布式搜索引擎

介绍:

Elasticsearch是一个开源的分布式搜索和分析引擎,最初由Elastic公司开发。它构建在Apache Lucene搜索引擎库之上,提供了一个强大的全文搜索和分析引擎,它结合kibana、Logstash、Beats,是一整套技术栈,被叫做ELK,适用于各种用例,包括文本搜索、日志分析、实时数据分析、监控和报警等。

官网:

官网地址:Elastic --- The Search AI Company | Elastic,目前最新的版本是8.x.x,国内大多使用6.x.x和7.x.x。

优势:

elasticsearch具备以下优势:

**·**支持分布式。可水平拓展

**·**提供Restful接口,可被任何语言调用

es在处理海量数据搜索时,速度非常的快,是因为它底层采用倒排索引。

★倒排索引:

首先介绍一下正向索引

倒排索引

总结:

IK分词器:

上述配置文件即表示添加扩展词典ext.dic,它就会在当前配置文件所在的目录中找这个文件。

总结:

基础概念:

与MySQL对比:

索引库操作

Mapping映射属性:

索引库操作:

总结:

文档处理:

CRUD:

新增:
查找、删除:
修改:
全量修改:

这种方式在文档id不存在时,就会相当于一个新增操作。

增量修改:

批量处理:

JavaRestClient:

客户端初始化:

商品表Mapping映射:

以商品表举例:

在kibana中写出即为:

复制代码
PUT /items
{
  "mappings": {
    "properties": {
      "id": {
        "type":"keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "price":{
        "type": "integer"
      },
      "image":{
        "type": "keyword", 
        "index": false
      },
      "category":{
        "type": "keyword"
      },
      "brand":{
        "type": "keyword"
      },
      "sold":{
        "type": "integer"
      },
      "comment_count":{
        "type": "integer", 
        "index": false
      },
      "isAD":{
        "type": "boolean"
      },
      "update_time":{
        "type": "date"
      }
}

索引库操作:

创建索引库的JavaAPI与Restful接口API对比:

操作步骤:
代码演示:
复制代码
class ItemTest {

    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        // 初始化 RestHighLevelClient 对象
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.178.130:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }

    @Test
    void testCreateIndex() throws IOException {
        //1.准备Request对象
        CreateIndexRequest request = new CreateIndexRequest("items");
        //2.准备请求参数
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        //3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    @Test
    void testGetIndex() throws IOException {
        //1.准备Request对象
        GetIndexRequest request = new GetIndexRequest("items");
        //2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println("exists: " + exists);
    }

    @Test
    void testDeleteIndex() throws IOException {
        //1.准备Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("items");
        //2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    private static final String MAPPING_TEMPLATE = "{
" +
            "  "mappings": {
" +
            "    "properties": {
" +
            "      "id": {
" +
            "        "type":"keyword"
" +
            "      },
" +
            "      "name":{
" +
            "        "type": "text",
" +
            "        "analyzer": "ik_smart"
" +
            "      },
" +
            "      "price":{
" +
            "        "type": "integer"
" +
            "      },
" +
            "      "image":{
" +
            "        "type": "keyword", 
" +
            "        "index": false
" +
            "      },
" +
            "      "category":{
" +
            "        "type": "keyword"
" +
            "      },
" +
            "      "brand":{
" +
            "        "type": "keyword"
" +
            "      },
" +
            "      "sold":{
" +
            "        "type": "integer"
" +
            "      },
" +
            "      "comment_count":{
" +
            "        "type": "integer", 
" +
            "        "index": false
" +
            "      },
" +
            "      "isAD":{
" +
            "        "type": "boolean"
" +
            "      },
" +
            "      "update_time":{
" +
            "        "type": "date"
" +
            "      }
" +
            "}
" +
            "}
" +
            "}";

}

文档操作:

新增文档:
运行代码:
复制代码
@SpringBootTest(properties = "spring.profiles.active=local")
class ESDocTest {

    private RestHighLevelClient client;
    @Autowired
    private IItemService itemService;

    @BeforeEach
    void setUp() {
        // 初始化 RestHighLevelClient 对象
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.178.130:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }

    @Test
    void testIndexDoc() throws IOException {
        //获取数据
        Item item = itemService.getById(317578L);
        ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
        //创建request对象
        IndexRequest request = new IndexRequest("item").id(itemDoc.getId());
        //准备JSON文档
        request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        //发送请求
        client.index(request, RequestOptions.DEFAULT);
    }


}
删除文档:
查询文档:
修改文档:

全量更新:可以使用新增文档的代码,在得到ItemDoc后修改它的属性值在新增即可。

局部更新:

文档操作基本步骤:

批处理:

代码示例:

DSL查询:

快速入门:

注意:单次查询默认最大数据数为10000,最多返回10条数据

叶子查询:

全文检索:

FIELD为要搜索的字段,TEXT为要搜索的内容

精确查询:

term查询一般用来搜不分词的字段,比如品牌等。如果搜分词的字段,VALUE只能写分好的词条,比如"脱脂"、"牛奶"等,才能搜到

range查询中gte和lte也可以写成gt和lt这样就是大于和小于。

ids查询:(批量查询id)

总结:

复合查询:

布尔查询:
示例:

搜索"智能手机",但品牌必须是华为,价格必须是900~1599

排序和分页:

排序:
示例:

搜索商品,按照销量排序,销量一样则按照价格排序。

分页:
示例:

搜索商品,查询出销量排名前10的商品,销量一样时按照价格升序。

深度分页问题:
解决方案:

高亮显示:

(↑标签默认就为em)

搜索完整语法:

JavaRestClient查询:

快速入门:

构建查询条件:

全文检索查询:
精确查询:
布尔查询:

排序和分页:

高亮显示:

聚合:

聚合的分类:

DSL实现聚合:

Java客户端实现聚合:

构造请求参数:

解析结果:

相关推荐
团子的二进制世界6 分钟前
G1垃圾收集器是如何工作的?
java·jvm·算法
long31610 分钟前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
rannn_11137 分钟前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
灵感菇_39 分钟前
Java HashMap全面解析
java·开发语言
qq_124987075341 分钟前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
短剑重铸之日1 小时前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
若鱼19191 小时前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
倒流时光三十年1 小时前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch
觉醒大王1 小时前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
努力学编程呀(๑•ี_เ•ี๑)1 小时前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea