目录

Java_ElasticSearchES分布式搜索引擎

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

介绍:

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

https://i-blog.csdnimg.cn/direct/ea7950e8266042708ea27d9f042b3278.png

官网:

官网地址: ,目前最新的版本是8.x.x,国内大多使用6.x.x和7.x.x。

优势:

elasticsearch具备以下优势:

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

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

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

★倒排索引:

首先介绍一下 正向索引

https://i-blog.csdnimg.cn/direct/d1d11f4c1a174acbb320266ab3effa96.png

倒排索引

https://i-blog.csdnimg.cn/direct/ee5a830263274514b94efbe08429886c.png

总结:

https://i-blog.csdnimg.cn/direct/451d3f23443e4c579aa7bb0decfbe32b.png

IK分词器:

https://i-blog.csdnimg.cn/direct/bbdcaef27c154e30bac30df0a28322be.png

https://i-blog.csdnimg.cn/direct/2b46ac58786346098f030264f4b78e69.png

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

总结:

https://i-blog.csdnimg.cn/direct/c968cf5fc9784be49fbd673a8e9a98a1.png

基础概念:

https://i-blog.csdnimg.cn/direct/2c77e1dbce10421eb097302524923103.png

与MySQL对比:

https://i-blog.csdnimg.cn/direct/97e5034ca2f24fa38725afee8a61dd1f.png

索引库操作

Mapping映射属性:

https://i-blog.csdnimg.cn/direct/36125d75a27942869da2620600be0636.png

索引库操作:

https://i-blog.csdnimg.cn/direct/dbf6d8035c8446d5876f69687b065450.png

https://i-blog.csdnimg.cn/direct/02adeb8fca92419a8d3b1db0099d6efa.png

https://i-blog.csdnimg.cn/direct/300603fef4304294894747bef01829c0.png

总结:

https://i-blog.csdnimg.cn/direct/dd10dcbb801d4ad79f5e22e6a2fd0e20.png

文档处理:

CRUD:

新增:

https://i-blog.csdnimg.cn/direct/76e4123a5cd1434cb8f2dac4dae76d6e.png

查找、删除:

https://i-blog.csdnimg.cn/direct/bf5352fa0cf2471d9e2c18b1e696c451.png

修改:

全量修改:

https://i-blog.csdnimg.cn/direct/e6642028ac744b1fb55ee07ff11f49fa.png

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

增量修改:

https://i-blog.csdnimg.cn/direct/34ed12d070834f7cad58f4eeb852f014.png

批量处理:

https://i-blog.csdnimg.cn/direct/08d7e0659f0943928d0f9df1d03f70c5.png

JavaRestClient:

https://i-blog.csdnimg.cn/direct/b94b314a2e404c77b2c28415ecfc6b1a.png

客户端初始化:

https://i-blog.csdnimg.cn/direct/fc07d2f59a48471bbdc7134c432183f1.png

商品表Mapping映射:

以商品表举例:

https://i-blog.csdnimg.cn/direct/b158af98637141b1868410d7b87dfa14.png

在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对比:

https://i-blog.csdnimg.cn/direct/1b006c04c2e245c3b4450680846bc051.png

https://i-blog.csdnimg.cn/direct/6f02c317c6df449e983a2a788d656a08.png

操作步骤:

https://i-blog.csdnimg.cn/direct/406ed417b090433dbe163e5d1cb0738a.png

代码演示:

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 = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\":\"keyword\"\n" +
            "      },\n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_smart\"\n" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"image\":{\n" +
            "        \"type\": \"keyword\", \n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"category\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"brand\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"sold\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"comment_count\":{\n" +
            "        \"type\": \"integer\", \n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"isAD\":{\n" +
            "        \"type\": \"boolean\"\n" +
            "      },\n" +
            "      \"update_time\":{\n" +
            "        \"type\": \"date\"\n" +
            "      }\n" +
            "}\n" +
            "}\n" +
            "}";

}

文档操作:

新增文档:

https://i-blog.csdnimg.cn/direct/d6da972296b845d2a75c43d8d9330864.png

运行代码:
@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);
    }

}

删除文档:

https://i-blog.csdnimg.cn/direct/bb4bb58cf8bb4c69b85927f9fb18b587.png

查询文档:

https://i-blog.csdnimg.cn/direct/4ac57beb0888424da9b2968373b4921c.png

修改文档:

https://i-blog.csdnimg.cn/direct/82136f1e78ac456b90f1cae1a2556565.png

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

局部更新:

https://i-blog.csdnimg.cn/direct/ffde7712e1634c63acfbf4caf613013f.png

文档操作基本步骤:

https://i-blog.csdnimg.cn/direct/5f563fbc6844400097513155d5d66510.png

批处理:

https://i-blog.csdnimg.cn/direct/8e78043392694076949d7882bdb2df69.png

代码示例:

https://i-blog.csdnimg.cn/direct/71d0ad27b9424f31987bd1380b83f213.png

DSL 查询:

https://i-blog.csdnimg.cn/direct/5d1d735232ed4ab28d1f5d64995e959d.png

https://i-blog.csdnimg.cn/direct/7c3df5d5e48240709a014cb825c4833b.png

快速入门:

https://i-blog.csdnimg.cn/direct/ffb7f43a5d984085b9ce206c93d38144.png

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

叶子查询:

全文检索:

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

https://i-blog.csdnimg.cn/direct/0460bc22032341baa8de0321d164bad6.png

精确查询:

https://i-blog.csdnimg.cn/direct/9f94a87ff73a4af1b5501ec138661c4b.png

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

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

ids 查询:(批量查询 id)

https://i-blog.csdnimg.cn/direct/535793d10a5040bca8b35efe48f640d1.png

总结:

https://i-blog.csdnimg.cn/direct/d61535040f694fcbbabc77c345d11434.png

复合查询:

https://i-blog.csdnimg.cn/direct/e60bfa1e86984ebb9d689ff1f57842a9.png

布尔查询:

https://i-blog.csdnimg.cn/direct/797dc2e911384be496e1fd55b104a64e.png

示例:

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

https://i-blog.csdnimg.cn/direct/d331e775c3684a74aeb0b841fb80ec81.png

排序和分页:

排序:

https://i-blog.csdnimg.cn/direct/a07013f86f344e3a96ef756fd1be57f0.png

示例:

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

https://i-blog.csdnimg.cn/direct/47f31b8c60d744079403d8f84de7fd76.png

分页:

https://i-blog.csdnimg.cn/direct/7a9aef73452141a0a290c441c6ec0de1.png

示例:

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

https://i-blog.csdnimg.cn/direct/546a5086a5354fa1854f71c940b0f890.png

深度分页问题:

https://i-blog.csdnimg.cn/direct/be7d81817c684aec9053ef0e8f04c4b5.png

解决方案:

https://i-blog.csdnimg.cn/direct/85a5327440334576865bba39cc413d73.png

高亮显示:

https://i-blog.csdnimg.cn/direct/bdb738b9153646d8bdb01b599320d165.png

(↑ 标签默认就为 em)

搜索完整语法:

https://i-blog.csdnimg.cn/direct/874eb9b2409e4beaa10ce6af4081eb0e.png

JavaRestClient 查询:

快速入门:

https://i-blog.csdnimg.cn/direct/790958f291cd461eb12b6a71255c5fc6.png

https://i-blog.csdnimg.cn/direct/2232dfd6f7404c86841a86f9f72756e6.png

构建查询条件:

https://i-blog.csdnimg.cn/direct/5cfa5b91453f42a080a51862f93702fd.png

全文检索查询:

https://i-blog.csdnimg.cn/direct/78f0588774364fd48839ac2a14911dfc.png

精确查询:

https://i-blog.csdnimg.cn/direct/886dba7777994b53901635bb6c7d1180.png

布尔查询:

https://i-blog.csdnimg.cn/direct/28b92c01ab064f3e99303606b8a226f2.png

排序和分页:

https://i-blog.csdnimg.cn/direct/ac6b0c36a25b473c8eefad88d43ec54a.png

高亮显示:

https://i-blog.csdnimg.cn/direct/531036653cf2483ea28a9ced51ce8fca.png

https://i-blog.csdnimg.cn/direct/7dab2c1e22e9476eaa4f24c1af2f46bc.png

聚合:

聚合的分类:

https://i-blog.csdnimg.cn/direct/c3f03f4b1b404ccab3784aae464d630b.png

DSL 实现聚合:

https://i-blog.csdnimg.cn/direct/a85f139d077644c38517497f81ee3507.png

https://i-blog.csdnimg.cn/direct/b34ee7c001db455f8ceb675da618fa78.png

https://i-blog.csdnimg.cn/direct/f8273197544f40e59e49a014b38702bf.png

Java 客户端实现聚合:

构造请求参数:

https://i-blog.csdnimg.cn/direct/6c22cef9fbe74cc3a14a3faf80494f8c.png

解析结果:

https://i-blog.csdnimg.cn/direct/4efe7e0719764502b4f2f8d929b63adb.png