Elasticsearch添加拼音搜索支持

网友投稿 869 2022-05-30

一份不错的资料

ELASTIC 搜索开发实战

一、安装插件

拼音分词扩展elasticsearch-analysis-pinyin安装

文档: https://github.com/medcl/elasticsearch-analysis-pinyin

二、新建索引添加拼音支持

替换为 实际 index

替换为 实际 type

PUT { "settings" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin" } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "keep_first_letter":false, "keep_separate_first_letter" : false, "keep_full_pinyin" : true, "keep_original" : false, "limit_first_letter_length" : 16, "lowercase" : true } } } }, "mappings": { "": { "properties": { "name": { "type": "text", "index": true, "fields":{ "pinyin":{ "type":"text", "analyzer":"pinyin_analyzer" } } }, "link": { "type": "keyword", "index": false }, "id": { "type": "long" }, "update_time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

分词测试

GET /_analyze { "field": "name.pinyin", "text": "内蒙古" } 返回 { "tokens": [ { "token": "nei", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "meng", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "gu", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 } ] }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

二、已有索引添加拼音支持

1、新建索引

PUT { "mappings": { "": { "properties": { "name": { "type": "keyword", "index": true }, "link": { "type": "keyword", "index": false }, "id": { "type": "long" }, "update_time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

2、设置拼音分词器

POST /_close PUT /_settings { "index": { "analysis": { "analyzer": { "pinyin_analyzer": { "tokenizer": "my_pinyin" } }, "tokenizer": { "my_pinyin": { "type": "pinyin", "keep_first_letter": true, "keep_separate_first_letter": true, "keep_full_pinyin": true, "keep_original": false, "limit_first_letter_length": 16, "lowercase": true } } } } } POST /_open

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

3、修改mapping,添加拼音分词器

PUT //_mapping { "": { "properties": { "name": { "type": "keyword", "index": true, "fields":{ "pinyin":{ "type":"text", "analyzer":"pinyin_analyzer" } } }, "link": { "type": "keyword", "index": false }, "id": { "type": "long" }, "update_time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } GET /_mapping # 将当前索引的数据重新替换一下当前索引 POST /_update_by_query?conflicts=proceed

1

2

3

4

5

6

Elasticsearch添加拼音搜索支持

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

4、搜索测试

get /_search { "query_string": { "fields": [ "name", "name.pinyin" ], "query": "王苏川", "default_operator": "AND" } }

1

2

3

4

5

6

7

8

9

10

11

12

13

参考

Elastic 搜索开发实战 拼音处理

Elasticsearch

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python+requests接口测试基础
下一篇:Python:mysql-connector-python模块对MySQL数据库进行增删改查
相关文章