我搜索单词form
,但完全匹配的单词form
不是第一个结果。有什么办法可以解决这个问题?
{
"query": {
"match": {
"word": "form"
}
}
}
word score
--------------------------
formulation 10.864353
formaldehyde 10.864353
formless 10.864353
formal 10.84412
formerly 10.84412
forma 10.84412
formation 10.574185
formula 10.574185
formulate 10.574185
format 10.574185
formally 10.574185
form 10.254687
former 10.254687
formidable 10.254687
formality 10.254687
formative 10.254687
ill-formed 10.054999
in form 10.035862
pro forma 9.492243
搜索中的单词form
只有一个标记form
。
在索引中,form
个标记为[“ f”,“ fo”,“ for”,“ form”]; formulation
标记为[“ f”,“ fo”,...,“公式”,“公式”]。
"edgengram_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
"analyzer": {
"abc_vocab_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"keyword_repeat",
"lowercase",
"asciifolding",
"edgengram_filter",
"unique"
]
},
"abc_vocab_search_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"keyword_repeat",
"lowercase",
"asciifolding",
"unique"
]
}
}
"word": {
"type": "text",
"analyzer": "abc_vocab_analyzer",
"search_analyzer": "abc_vocab_search_analyzer"
}
因为您在此字段中输入的是文本,这意味着ES会对该字段进行全文搜索分析。 ES搜索过程可以找到与您所给单词最相似的结果。要准确地搜索单词“ form”,请将搜索方法更改为match_phrase
。此外,您还可以阅读以下文章,以了解有关不同ES搜索方法的更多信息:https://www.cnblogs.com/yjf512/p/4897294.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
[在我看来,您的用例是在存在完全匹配时提供boost
。
为此,我建议为word
创建同级字段作为word.keyword
类型的keyword,并通过以下方式使用Term Query进行增强。关键字字段不会进行分析,而是按原样存储。
{
"word":{
"type": "text",
"analyzer": "abc_vocab_analyzer",
"search_analyzer": "abc_vocab_search_analyzer"
"fields":{
"keyword":{
"type": "keyword"
}
}
}
}
POST <your_index_name>/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"word": "form"
}
}
],
"should": [ <---- Note this
{
"term": {
"word.keyword": {
"value": "form",
"boost": 10 <---- Provide boosting for exact matches.
}
}
}
]
}
}
}
让我知道是否有帮助!