##ngram高性能分词搜索(edge ngram将每个单词切分搜索)
##如:hello world,可拆分如下:
##h
##he
##hel
##hell
##hello
##w
##wo
##wor
##worl
##world
##1、建索引,指定自定义分词器
PUT /person
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter":{
"type":"edge_ngram",
"min_gram":1,
"max_gram":20
}
},
"analyzer": {
"autocomplete":{
"type":"custom",
"tokenizer":"standard",
"filter":[
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
##2、查看分词类型
GET person/_analyze
{
"analyzer": "autocomplete",
"text": "hello world"
}
##3、设置索引分词器类型
PUT person/_mapping
{
"properties":{
"title":{
"type":"text",
"analyzer":"autocomplete",
"search_analyzer":"standard"
}
}
}
##4、插入数据
PUT person/_bulk
{"index":{"_id":1}}
{"id":1,"title":"hello world"}
{"index":{"_id":2}}
{"id":2,"title":"hello cat"}
{"index":{"_id":3}}
{"id":3,"title":"hello dog"}
{"index":{"_id":4}}
{"id":4,"title":"hello tom"}
{"index":{"_id":5}}
{"id":5,"title":"hello we"}
{"index":{"_id":6}}
{"id":6,"title":"win 10"}
##5、查询
GET person/_search
{
"query": {
"match_phrase": {
"title": "hello w"
}
}
}
ES7.16.2基础操作之搜索推荐ngram(八)
本文介绍了如何使用Elasticsearch的edge_ngram过滤器创建索引,以便实现高效的分词搜索。通过示例展示了从'helloworld'拆分成多个前缀的过程,并详细说明了建立索引、查看分词器、设置索引映射、插入数据以及执行查询的步骤。这种方法适用于实时搜索和建议功能,提高用户搜索体验。
摘要由CSDN通过智能技术生成