es笔记

作者: shoyu666 | 来源:发表于2022-04-10 11:20 被阅读0次

    Elasticsearch: 权威指南

    比较系统性的教程

    Java REST Client [7.17]

    详细的api文档,包括示例代码

    概念

    document

    ElasticSearch(简称 ES) 是面向文档的,文档是所有可搜索数据的最小单位。

    Elasticsearch数据存储基础概念
    image.png

    index可以理解为关系型数据库中的Database,是众多document的集合;而document对应着关系型数据库中的一行记录,由于是NoSQL数据库,所以每个文档可以存储不同的字段。Index和document都是逻辑上的数据存储概念,而数据最终会存储在一个或着多个物理的shard(分片)中。


    image.png
    主分片和副本分片
    示例1,清晰重制版
    示例2
    Lucene Index

    每个分片上对应着就是一个 Lucene Index(底层索引文件)
    Lucene Index 是一个统称。由多个 Segment (段文件,就是倒排索引)组成。每个段文件存储着就是 Doc 文档


    image.png
    es索引过程
    image.png
    • character filter
      首先字符串经过字符过滤器(character filter),它们的工作是在表征化(译者注:这个词叫做断词更合适)前处理字符串。
      字符过滤器能够去除HTML标记,或者转换 "&" 为 "and"

    • tokenizer
      下一步,分词器(tokenizer)被表征化(断词)为独立的词。一个简单的分词器(tokenizer)可以根据空格或逗号将单词分开

    • token filters
      最后,每个词都通过所有表征过滤(token filters),它可以修改词(例如将 "Quick" 转为小写),去掉词(例如停用词 像 "a" 、 "and"``"the" 等等),或者增加词(例如同义词像 "jump" 和 "leap" )
      例子:


      image.png
    es倒排索引

    Elasticsearch使用一种叫做倒排索引(inverted index)的结构来做快速的全文搜索。倒排索引由在文档中出现的唯一的term列表,以及对于每个term在文档中的位置组成。

    倒排索引表达的是一个term在哪些文档出现了,而不是一个文档有哪些term。


    image.png

    假设有很多首诗,编号是1,2,3....
    上图表示
    床这个term在编号为1的诗里面出现了。
    前这个term在编号为1,2,...多个诗里面出现了。

    java client api

    示例1:create-index

    参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-create-index.html

    CreateIndexRequest request = new CreateIndexRequest("twitter");
    
    //settings 
    request.settings(Settings.builder()
        .put("index.number_of_shards", 3)
        .put("index.number_of_replicas", 2)
    );
    
    //mappings 方式1
    request.mapping(
    "{\n" +
            "  \"properties\": {\n" +
            "    \"message\": {\n" +
            "      \"type\": \"text\"\n" +
            "    }\n" +
            "  }\n" +
            "}",
      XContentType.JSON);
    
    //mappings 方式2
    Map<String, Object> message = new HashMap<>();
    message.put("type", "text");
    Map<String, Object> properties = new HashMap<>();
    properties.put("message", message);
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    request.mapping(mapping);
    
    //等待所有节点创建返回,超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    
    //连接master超时时间
    request.setMasterTimeout(TimeValue.timeValueMinutes(1));
    
    //在创建索引API返回响应之前等待的活动分片副本的数量
    request.waitForActiveShards(ActiveShardCount.from(2));
    
    //同步执行
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    
    boolean acknowledged = createIndexResponse.isAcknowledged();
    boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
    
    //异步执行
    //client.indices().createAsync(request, RequestOptions.DEFAULT, listener);
    
    ActionListener<CreateIndexResponse> listener =
            new ActionListener<CreateIndexResponse>() {
    
        @Override
        public void onResponse(CreateIndexResponse createIndexResponse) {
     }
    @Override
        public void onFailure(Exception e) {
        }
    };
    
    参考

    官方blog
    https://aws.amazon.com/cn/blogs/china/plan-an-amazon-elasticsearch-service-cluster-with-a-hot-warm-architecture/
    https://wkui.xyz/posts/es-analyzer简介/
    https://segmentfault.com/a/1190000037658997
    https://fdv.gitbooks.io/elasticsearch-cluster-design-the-definitive-guide/content/a-few-things-you-need-to-know-about-lucene.html

    相关文章

      网友评论

          本文标题:es笔记

          本文链接:https://www.haomeiwen.com/subject/ifupsrtx.html