[英]Is there any solution for searching exact word and containing word both in elasticsearch
index: process.env.elasticSearchIndexName,
body: {
query: {
bool: {
must: [
{
match_phrase: {
title: `${searchKey}`,
},
},
],
},
},
},
from: (page || constants.pager.page),
size: (limit || constants.pager.limit),
我正在使用上述方法,但问题在于它只在整个文本中搜索完全匹配的单词。 它无法搜索包含单词.. 例如,如果 title = "sweatshirt" 而不是我输入单词 "shirt" 它应该是结果,但目前没有使用上述方法得到结果
标准分析器(如果未指定,则为默认分析器)在标记中分解文本。 对于句子“这是一个测试”生成的标记是 [this,is,a,test] Match_pharse 查询使用与索引分析器相同的分析器在标记中中断文本,并返回 1. 包含所有标记 2. 标记以相同顺序出现的文档。
由于您的文本是运动衫,因此倒排索引中有一个标记为“运动衫”,它与汗水或衬衫都不匹配
每当 ngram 标记器遇到指定字符列表中的一个时,它首先将文本分解为单词,然后发出指定长度的每个单词的 N-gram
映射
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"properties": {
"text":{
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
询问:
{
"query": {
"match": {
"text": "shirt"
}
}
}
如果您将运行 _analyze 查询
GET my_index/_analyze
{
"text": ["sweatshirt"],
"analyzer": "my_analyzer"
}
您将看到为文本运动衫生成了以下标记。 可以使用 min_gram 和 max_gram 调整令牌的大小
{
"tokens" : [
{
"token" : "swe",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 0
},
{
"token" : "wea",
"start_offset" : 1,
"end_offset" : 4,
"type" : "word",
"position" : 1
},
{
"token" : "eat",
"start_offset" : 2,
"end_offset" : 5,
"type" : "word",
"position" : 2
},
{
"token" : "ats",
"start_offset" : 3,
"end_offset" : 6,
"type" : "word",
"position" : 3
},
{
"token" : "tsh",
"start_offset" : 4,
"end_offset" : 7,
"type" : "word",
"position" : 4
},
{
"token" : "shi",
"start_offset" : 5,
"end_offset" : 8,
"type" : "word",
"position" : 5
},
{
"token" : "hir",
"start_offset" : 6,
"end_offset" : 9,
"type" : "word",
"position" : 6
},
{
"token" : "irt",
"start_offset" : 7,
"end_offset" : 10,
"type" : "word",
"position" : 7
}
]
}
警告:Ngrams 增加了倒排索引的大小,因此使用合适的 min_gram 和 max_gram 值
另一种选择是使用通配符查询。 对于通配符,必须扫描所有文档以检查文本是否与模式匹配。 它们的性能很低。 在 not_analyzed 字段上使用通配符搜索时,如果您想包含空格 ex text.keyword
{
"query": {
"wildcard": {
"text": {
"value": "*shirt*"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.