我搜索了单词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"
}
答案 0 :(得分:0)
因为您在此字段中输入的是文本,这意味着ES会对该字段进行全文本搜索分析。 ES搜索过程可以找到与您所给单词最相似的结果。
要准确搜索单词“ form”,请将搜索方法更改为match_phrase
此外,您还可以阅读以下文章可详细了解不同的ES搜索方法:
https://www.cnblogs.com/yjf512/p/4897294.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
答案 1 :(得分:0)
通过实现edge-ngram
过滤器,并且form
是与之类似的单词的子字符串,可以以您所看到的方式获得结果。基本上,在倒排索引中,它还将存储包含formulation
,formal
等的文档ID。
因此,您的相关性也会以这种方式计算。您可以参考this链接,我特别建议您仔细阅读Default Similarity
和BM25
部分。尽管当前的默认相似度为BM25
,但该链接将帮助您了解评分的工作方式。
您将需要创建另一个同级字段,您可以在should子句中应用它。您可以继续使用keyword
创建Term Query
子字段,但是需要注意区分大小写。
相反,如@Val所述,您可以使用标准分析器创建text
字段的同级对象。
{
"word":{
"type": "text",
"analyzer": "abc_vocab_analyzer",
"search_analyzer": "abc_vocab_search_analyzer"
"fields":{
"standard":{
"type": "text"
}
}
}
}
POST <your_index_name>/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"word": "form"
}
}
],
"should": [ <---- Note this
{
"match": {
"word.standard": "form"
}
}
]
}
}
}
让我知道这是否有帮助!
答案 2 :(得分:0)
就像您的自定义分析器中的某个问题一样,我创建了自定义autocomplete
分析器,该分析器使用edge_ngram
和lowercase
过滤器,对我的查询和返回最上面的完全匹配,这就是Elasticsearch的工作方式,完全匹配总会获得更高的分数。,因此无需显式创建另一个字段并将其提高,因为默认情况下,Elasticsearch会提高令牌匹配的完全匹配。 / p>
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
{
"title" : "formless"
}
{
"title" : "form"
}
{
"title" : "formulation"
}
title
字段进行搜索查询{
"query": {
"match": {
"title": "form"
}
}
}
"hits": [
{
"_index": "so-60523240-score",
"_type": "_doc",
"_id": "1",
"_score": 0.16410133,
"_source": {
"title": "form"
}
},
{
"_index": "so-60523240-score",
"_type": "_doc",
"_id": "2",
"_score": 0.16410133,
"_source": {
"title": "formulation"
}
},
{
"_index": "so-60523240-score",
"_type": "_doc",
"_id": "3",
"_score": 0.16410133,
"_source": {
"title": "formaldehyde"
}
},
{
"_index": "so-60523240-score",
"_type": "_doc",
"_id": "4",
"_score": 0.16410133,
"_source": {
"title": "formless"
}
}
]