es笔记

2022-04-10  本文已影响0人  shoyu666

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
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

上一篇下一篇

猜你喜欢

热点阅读