【问题标题】:Elastic search multiple simple_query_string with boost使用 boost 弹性搜索多个 simple_query_string
【发布时间】:2015-10-28 17:50:14
【问题描述】:

我为所有文档设置了索引:

{
  "mappings" {
    "book" {
      "_source": { "enabled": true },
      "properties": [
        "title": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" },
        "description": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" },
        "author": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" }
      ]
    }
  }
}

我将它推送到一个名为“library”的索引中。

想要做的是执行具有以下要求的搜索。假设用户输入了“大黄铲”之类的内容

  1. 以三种方式执行用户输入关键字的搜索:
    1. 作为一个完整的短语:“简单的黄色铲子”
    2. 作为一组AND关键字:“简单+黄色+铲子”
    3. 作为一组 OR 关键字:“simple|yellow|shovel”
  2. 确保关键字集按优先级顺序执行(提升?):
    1. 全文优先
    2. 然后是第二个
    3. 或第三个

使用简单的查询对单个搜索进行查找:

{
  "query": {
    "simple_query_string": {
      "query": "\"simple yellow shovel\""
    }
  }
}

如何使用 boosting 执行多重搜索? 或者我应该在索引字段上使用类似“匹配”查询的东西吗?

【问题讨论】:

  • 为什么你的属性是not_analyzed?这没有意义。始终分析您要对其运行文本搜索的字段。而且您没有优先考虑搜索字段。我认为匹配的标题应该比描述中的匹配具有更高的优先级?
  • 我会修复 not_analyzed 部分。那是一个很大的声音。为简洁起见,我省略了优先级。

标签: elasticsearch


【解决方案1】:

我不确定我是否正确。我假设优先顺序为 作者>标题>描述

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "simple yellow shovel",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "type": "phrase",
                  "boost": 10
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "simple",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 5
                }
              },
              {
                "multi_match": {
                  "query": "yellow",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 5
                }
              },
              {
                "multi_match": {
                  "query": "shovel",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 5
                }
              }
            ]
          }
        },
        {
          "multi_match": {
            "query": "simple",
            "fields": [
              "author^7",
              "title^3",
              "description"
            ],
            "boost": 2
          }
        },
        {
          "multi_match": {
            "query": "yellow",
            "fields": [
              "author^7",
              "title^3",
              "description"
            ],
            "boost": 2
          }
        },
        {
          "multi_match": {
            "query": "shovel",
            "fields": [
              "author^7",
              "title^3",
              "description"
            ],
            "boost": 2
          }
        }
      ]
    }
  }
}

有人可以验证一下吗?您可以参考Boost Query 链接了解更多信息。这是你要找的吗?

我希望这会有所帮助!

编辑:用 dis_max 重写

{
  "query": {
    "bool": {
      "should": [
        {
          "dis_max": {
            "tie_breaker": 0.7,
            "queries": [
              {
                "bool": {
                  "must": [
                    {
                      "multi_match": {
                        "query": "simple yellow shovel",
                        "fields": [
                          "author^7",
                          "title^3",
                          "description"
                        ],
                        "type": "phrase",
                        "boost": 10
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "dis_max": {
                        "tie_breaker": 0.7,
                        "queries": [
                          {
                            "multi_match": {
                              "query": "simple",
                              "fields": [
                                "author^7",
                                "title^3",
                                "description"
                              ],
                              "boost": 5
                            }
                          },
                          {
                            "multi_match": {
                              "query": "yellow",
                              "fields": [
                                "author^7",
                                "title^3",
                                "description"
                              ],
                              "boost": 5
                            }
                          },
                          {
                            "multi_match": {
                              "query": "shovel",
                              "fields": [
                                "author^7",
                                "title^3",
                                "description"
                              ],
                              "boost": 5
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "multi_match": {
                  "query": "simple",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 2
                }
              },
              {
                "multi_match": {
                  "query": "yellow",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 2
                }
              },
              {
                "multi_match": {
                  "query": "shovel",
                  "fields": [
                    "author^7",
                    "title^3",
                    "description"
                  ],
                  "boost": 2
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这似乎至少在我的数据集上给了我更好的结果。这是了解dismax

的好来源

请多玩这个,看看你是否得到了预期的结果。 使用Explain API 的帮助。

【讨论】:

  • 我认为这可以使用dis_max 查询重写。应该更优雅,更好地配置。
  • 这是*优秀的第一步,即使dis_max 更好。它至少让我指出了正确的道路。如果其他人回复 dis_max 并且它有效,那将赢得答案 - 否则这至少让我开始!
  • 感谢 @EvaldasBuinauskas 建议 dis_max。我已经更新了我的答案。如果它不起作用,请告诉我
  • 太棒了。我接受这个作为答案的原因是它显示了从一种查询类型到另一种查询类型的自然进展。但是,@EvaldasBuinauskas 的回答也很好。我喜欢学习技术和参与 Stack Overflow 的另一个原因 - 我实际上学到了很多,因为这指向了我。谢谢!
【解决方案2】:

我已经使用Dis Max Query 重写了这个。请记住,您可以尝试不同的类型以获得更好的结果。见这些:

  1. best_fields
  2. most_fields
  3. cross_fields

查询:

POST /your_index/your_type/_search
{
  "query": {
    "dis_max": {
      "tie_breaker": 0.7,
      "boost": 1.2,
      "queries": [
        {
          "multi_match": {
            "query": "simple yellow showel",
            "type": "phrase",
            "boost": 3,
            "fields": [
              "title^3",
              "author^2",
              "description"
            ]
          }
        },
        {
          "multi_match": {
            "query": "simple yellow showel",
            "operator": "and",
            "boost": 2,
            "fields": [
              "title^3",
              "author^2",
              "description"
            ]
          }
        },
        {
          "multi_match": {
            "query": "simple yellow showel",
            "fields": [
              "title^3",
              "author^2",
              "description"
            ]
          }
        }
      ]
    }
  }
}

Dis Max 查询将选择所有三个查询中得分最高的文档。我们为"type": "phrase""operator": "and" 提供了额外的提升,同时我们保持最后一个查询不变。

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多