[英]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.