【问题标题】:ElasticSearch multi-word query, matching multiple words is more valuable than matching few but many timesElasticSearch多词查询,匹配多个词比匹配少而多更有价值
【发布时间】:2020-06-17 02:20:00
【问题描述】:

我在 ElasticSearch 中写了一个多词搜索查询,匹配多个词比匹配 1 更有价值但很多次。

跨几个字段的 1 个查询:

{
      "bool" : {
        "must" : [
          {
            "simple_query_string" : {
              "query" : "effective date ",
              "fields" : [
                "field1^1.0",
                "field2^5.0",
                "field3^10.0",
              ],
              "flags" : -1,
              "default_operator" : "or",
              "analyze_wildcard" : false,
              "auto_generate_synonyms_phrase_query" : true,
              "fuzzy_prefix_length" : 0,
              "fuzzy_max_expansions" : 50,
              "fuzzy_transpositions" : true,
              "boost" : 1.0
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    }

当我搜索“生效或日期”时

例如:

“这是有效计算问题的示例日期

应该得分高于:

date date date是他对孩子们说的”

我怎样才能为此微调弹性搜索?

谢谢!

【问题讨论】:

    标签: elasticsearch search elasticsearch-query


    【解决方案1】:

    由于你在问题中没有提到你索引了多少个字段,所以我只取了一个字段,即title

    索引文档:

    {
        "title":"This is an example date for effective calculation of the problems"
    
    }
    {
        "title":"date date date is what he said to the children"
    
    }
    

    搜索查询:

    {
      "query": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "effective date",
                "operator": "or",
                "fields": [
                 "title"                    --> If you have more fields, you can 
                                                add them here
                ]
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.85874003,
                "_source": {
                    "title": "This is an example date for effective calculation of the problems"
                }
            },
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.289459,
                "_source": {
                    "title": "date date date is what he said to the children"
                }
            }
        ]
    

    关于Multi-Match查询的详细说明,可以参考这个官方documentation

    更新 1:

    使用查询字符串

        {
      "query": {
        "query_string": {
          "default_field": "title",
          "query": "effective OR date"
        }
      }
    }
    

    查询字符串的详细解释可以参考this

    更新 2:

    使用 simple_query_string

    {
      "query": {
        "simple_query_string" : {
            "query": "effective date",
            "fields": ["title"],
            "default_operator": "or"
        }
      }
    }
    

    以上三个搜索查询全部使用,得到相同的搜索结果,_score没有区别

    【讨论】:

    • 这行得通,谢谢 - 有没有办法在 query_string querysimple_query_string 查询中做到这一点?
    • @emraldinho 感谢您接受我的回答 :),您可以查看我更新的答案,其中包括使用 query_string 进行搜索查询
    • 感谢您提供更新的答案,我正在尝试使用 simple_query_string 并且得分大不相同 - 知道为什么会这样吗?
    • @emraldinho 您将获得相同的搜索结果和相同的分数,请查看我的更新答案
    • 感谢回复,对不起,当我使用Type CROSS_FIELDS时,它确实给了我更好的分数
    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2021-12-08
    相关资源
    最近更新 更多