【问题标题】:How to fine tune search results that contain most searching words in Elasticsearch 6.8?如何微调 Elasticsearch 6.8 中包含最多搜索词的搜索结果?
【发布时间】:2019-08-27 19:38:26
【问题描述】:

下面是我的映射:

{
  "mappings": {
    "_doc": {
      "properties": {
        "text": { 
          "type": "text",
          "fields": {
            "raw": { 
              "type":     "keyword",
              "normalizer": "case_insensitive"
            }
          }
        }
      }
    }
  }
}

设置如下所示:

{
  "settings": {
    "index": {
      "analysis" : {
        "normalizer" : {
          "case_insensitive" : {
            "filter" : "lowercase"
          }
        },
        "analyzer" : {
          "en_std" : {
            "type" : "standard",
            "stopwords" : "_english_"
          }
        }
      },
    }
  }
} 

以下是我的查询:

{
  "query": {
    "bool" : {
      "must" : {
        "query_string" : {
          "query" : "hawaii beach 2019",
          "analyze_wildcard: true,
          "fields": [
            "text"
          ]
        }
      },
    }
  }
}

以下是存储在 Elasticsearch 中的示例数据:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "nice hotel 2019"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
  {
     "text": "hawaii vacation 2019"
  },
]

如果我的搜索词是hawaii,我会得到三个结果:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": " some 2019 white beach hawaii beach photo"
  },
]

如果我的搜索词是hawaii beach,我会得到四个结果:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
]

如果我的搜索词是hawaii beach 2019,我会得到五个结果,它们是:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "nice hotel 2019"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
]

这是因为每条记录都包含一个搜索文本的单词。这是有道理的,但这并不是我想要的。我希望包含最多匹配词的记录出现在搜索结果的顶部,而包含较少匹配词的记录出现在搜索结果的底部。如何在 Elasticsearch 6.8 中做到这一点?如果这不能实现,也希望仅显示包含最多匹配词的记录作为搜索结果。

如果我的搜索文本是所需的搜索结果,例如hawaii beach 2019:

[
  {
     "text": " some 2019 white beach hawaii photo" // Contains most matching words.
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "blue hawaii hotel" // Contains less matching words.
  },
  {
     "text": "costa beach" // Contains less matching words.
  },

  {
     "text": "nice hotel 2019" // Contains less matching words.
  },

]

[
  {
     "text": " some 2019 white beach hawaii photo" // Contains most matching words
  },
]

【问题讨论】:

  • 我测试了您的配置和查询,并且在我的测试中一切正常。 some white beach hawaii 2019 photo 是得分最高的文档,其次是 white hawaii beach,依此类推。
  • 每个文档的分数是多少?
  • 我在 Elasticsearch 中又添加了一条示例记录:{"text": "hawaii vacation 2019"}。您可以添加这条额外的记录并再次尝试搜索文本hawaii beach 2019 吗?恐怕你会先得到hawaii vacation 2019,即使some 2019 white beach hawaii photo包含最多匹配的单词。
  • 我的观点是,Elasticsearch 对搜索文本的最后一个词的评分高于包含最多匹配词的文档。
  • 您的索引有多少个分片?有多少文件?再说一遍,Elasticsearch 在结果中给你的分数是多少?

标签: elasticsearch elasticsearch-6.8


【解决方案1】:

您可以修改输入查询:

hawaii AND beach AND 2019

然后您将获得所有 3 个单词的结果。

【讨论】:

  • 根据query_string manual,还有一个使用双引号的选项。查询"hawaii beach 2019" 将按照相同的顺序搜索短语中的所有单词。
  • 我确实在单词之间尝试了AND。它显示与没有AND 相同的结果。检索并显示包含这些单词之一的所有记录。包含最多匹配词的记录没有出现在搜索结果的顶部。同样,匹配词少的记录也不会出现在搜索结果的底部。
  • 这真的很奇怪,因为在documentation中提到过> 默认情况下,所有术语都是可选的,只要匹配一个术语即可。 > 搜索 foo bar baz 将找到包含 foo 或 bar 或 baz 中的一个或多个的任何文档
  • 你可以试试这样的查询:+hawaii +beach +2019
  • 在每个单词前面使用+ 符号没有任何作用。搜索结果保持不变。
【解决方案2】:

我想我已经找到了一个变通解决方案,将搜索字符串中的每个单词用* 括起来,如下所示。

{ 
  "query": { 
    "bool": { 
      "must": { 
        "bool": { 
          "should": { 
            "query_string": { 
              "query": "*hawaii* *beach* *2019*", 
              "fields": ["text"]
            } 
          } 
        } 
      } 
    } 
  } 
}

通过这个查询,我得到所有包含至少一个搜索字符串单词的文档。搜索词最匹配的文档显示在列表顶部。

【讨论】:

  • 查询的bool部分是不必要的,可以简单地改写为 { "query": { "query_string": { "query": "*hawaii* *beach* *2019*", “字段”:[“文本”] } } }
  • 你是对的!以防万一人们想添加更多查询过滤器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
相关资源
最近更新 更多