Kru*_*kla 46 substring autocomplete stringtokenizer n-gram elasticsearch
我想要执行精确的单词匹配和部分单词/子串匹配.例如,如果我搜索"男士剃须刀",那么我应该能够在结果中找到"男士剃须刀".但是在我搜索"en的剃须刀"的情况下,我也应该能够在结果中找到"男士剃须刀".我使用以下设置和映射:
索引设置:
PUT /my_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
映射:
PUT /my_index/my_type/_mapping
{
"my_type": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
插入记录:
POST /my_index/my_type/_bulk
{ "index": { "_id": 1 }}
{ "name": "men's shaver" }
{ "index": { "_id": 2 }}
{ "name": "women's shaver" }
Run Code Online (Sandbox Code Playgroud)
查询:
1.按精确短语匹配搜索 - >"男人"
POST /my_index/my_type/_search
{
"query": {
"match": {
"name": "men's"
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的查询在返回结果中返回"men's shaver".
2.按部分单词匹配搜索 - >"en's"
POST /my_index/my_type/_search
{
"query": {
"match": {
"name": "en's"
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上查询不返回任何内容.
我也试过以下查询
POST /my_index/my_type/_search
{
"query": {
"wildcard": {
"name": {
"value": "%en's%"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
还没有得到任何东西.我认为这是因为Index上的"edge_ngram"类型过滤器无法找到"部分字/ sbustring匹配".我也尝试了"n-gram"类型的过滤器,但它正在减慢搜索速度.
请建议我如何使用相同的索引设置实现精确的短语匹配和部分短语匹配.
Bla*_*POP 63
要搜索部分字段匹配和完全匹配,如果将字段定义为"未分析"或关键字(而不是文本),则可以更好地工作,然后使用通配符查询.
另见.
要使用通配符查询,请在要搜索的字符串的两端附加*:
POST /my_index/my_type/_search
{
"query": {
"wildcard": {
"name": {
"value": "*en's*"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要使用不区分大小写的情况,请使用带有小写过滤器和关键字标记生成器的自定义分析器.
定制分析仪:
"custom_analyzer": {
"tokenizer": "keyword",
"filter": ["lowercase"]
}
Run Code Online (Sandbox Code Playgroud)
使搜索字符串小写
如果您将搜索字符串作为AsD:将其更改为*asd*
@BlackPOP 给出的答案是可行的,但它使用通配符方法,这不是首选方法,因为它存在性能问题,如果滥用,可能会在 Elastic 集群中产生巨大的多米诺骨牌效应(性能问题)。
我写了一篇关于部分搜索/自动完成的详细博客,涵盖了截至今天(2020 年 12 月)Elasticsearch 中可用的最新选项,并考虑到了性能。有关更多权衡信息,请参阅此答案。
恕我直言,更好的方法是根据用例使用定制的n-gram 标记生成器,它已经具有搜索词所需的标记,因此速度会更快,尽管它会有更大的索引大小,但您的大小不是那么大通过更多地控制您希望子字符串搜索的工作方式,成本和速度会更好。
如果您在分词器设置中定义最小和最大克时保守的话,也可以控制大小。