You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ElasticSearch 6.x:简化语法实现标题与描述字段短语匹配检索

Got it, let's build the Elasticsearch 6.x query you need to handle searches across the title and description fields, using the bool, should, must, and match_phrase components as requested. This structure will effectively handle queries like "change desk chair" while keeping the syntax clean and simplified.

Here's the working query:

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "change desk chair",
            "fields": ["title", "description"],
            "operator": "and"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "title": "change desk chair"
          }
        },
        {
          "match_phrase": {
            "description": "change desk chair"
          }
        }
      ],
      "minimum_should_match": 0
    }
  }
}

Breakdown of the Query:

  • must Clause: This ensures every term from the user's query ("change", "desk", "chair") exists in either the title or description field. We use multi_match here to avoid repeating logic for each field, with operator: "and" enforcing all terms are present.
  • should Clause: This adds exact phrase matching for the full query string in each field. Documents that have the exact phrase in title or description will get a higher relevance score, so more precise matches rise to the top.
  • minimum_should_match: 0: This keeps the query flexible—documents don't need to have the exact phrase to be returned, but phrase matches still get a boost.

For your example document (title: "Change my room!", description: "I change my desk and chair. These make my room more beautiful!"), this query will correctly match it because all three query terms are present in the description field. If any document had the exact phrase "change desk chair" in either field, it would rank higher than partial matches.

内容的提问来源于stack exchange,提问作者JSKIM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:27:52