ElasticSearch学习总结

1、通过term 和 match查询数据时细节点以及数据类型keyword与text区别

1.1 term查询

1.1.1 term查询keyword字段。

term不会分词。而keyword字段也不分词。需要完全匹配才可。

hello world === hello world

hello world

1.1.2 term查询text字段。

因为text字段会分词,而term不分词,所以term查询的条件必须是text字段分词后的某一个。

1.2.match查询

1.2.1 match查询keyword字段

match会被分词,而keyword不会被分词,match的需要跟keyword的完全匹配可以。

1.2.2 match查询text字段

match分词,text也分词,只要match的分词结果和text的分词结果有相同的就匹配。

1 PUT /es_db_test

2 {

3 "mappings":{

4 "properties":{

5 "name":{"type":"keyword","index":true,"store":true},

6 "sex":{"type":"integer","index":true,"store":true},

7 "age":{"type":"integer","index":true,"store":true},

8 "book":{"type":"text","index":true,"store":true},

9 "address":{"type":"keyword","index":true,"store":true}

10 }

11 }

12 }

13

14

15 PUT /es_db_test/_doc/116 {

17 "name": "Jack",

18 "sex": 1,

19 "age": 25,

20 "book": "elasticSearch入门至精通",

21 "address": "广州车陂"

22 }

23

24

25 POST /es_db_test/_doc/_search

26 {

27 "query": {

28 "term": {

29 "address": "广州车陂"

30 }

31 }

32 }

33

34 POST /es_db_test/_doc/_search

35 {

36 "query": {

37 "match": {

38 "address": "广州车陂"

39 }

40 }

41 }

2、乐观锁并发控制 if_seq_no 和 if_primary_term意义

if_seq_no 和 if_primary_term 是用来并发控制,他们和version不同,

version属于当个文档,而seq_no属于整个index。

_primary_term表示文档所在主分片的编号

1 GET /es_db_tem/_doc/1

3 DELETE /es_db_tem/_doc/1

6 PUT /es_db_tem/_doc/1

7 {

8 "name": "Jack",9 "sex": 1,

10 "age": 25,

11 "book": "elasticSearch入门至精通",

12 "address": "广州车陂"

13 }

14 PUT /es_db_tem/_doc/2

15 {

16 "name": "Jack",

17 "sex": 1,

18 "age": 25,

19 "book": "elasticSearch入门至精通",

20 "address": "广州车陂"

21 }

_primary_term:_primary_term也和_seq_no一样都是整数,每当Primary Shard发生重新

分配时,比如重启,Primary选举等,_primary_term会递增1。

_primary_term主要是用来恢复数据时处理当多个文档的_seq_no一样时的冲突,比如当一个

shard宕机了,raplica需要用到最新的数据,就会根据_primary_term和_seq_no这两个值来

拿到最新的document

3、对已存在的mapping映射进行修改

具体方法

1)如果要推倒现有的映射, 你得重新建立一个静态索引

2)然后把之前索引里的数据导入到新的索引里

3)删除原创建的索引

4)为新索引起个别名, 为原索引名

1 DELETE /myes_db

1 PUT /myes_db

1 PUT /myes_db/_doc/1

2 {

3 "name": "Jack",

4 "sex": 1,

5 "age": 25,6 "book": "java入门至精通",

7 "address": "广州小蛮腰"

8 }

1 GET /myes_db/_mapping

1 PUT /myes_db2

2 {

3 "mappings":{

4 "properties":{

5 "name":{"type":"keyword","index":true,"store":true},

6 "sex":{"type":"integer","index":true,"store":true},

7 "age":{"type":"integer","index":true,"store":true},

8 "book":{"type":"text","index":true,"store":true},

9 "address":{"type":"text","index":true,"store":true}

10 }

11 }

12 }

1 POST _reindex

2 {

3 "source": {

4 "index": "myes_db"

5 },

6 "dest": {

7 "index": "myes_db2"

8 }

9 }

11 DELETE /myes_db

13 PUT /myes_db2/_alias/myes_db

上一篇:分享三个游戏源码下载网站


下一篇:Impacts of long-term service disruptions on passenger travel behaviour