Elasticsearch-合并多个匹配短语

发布于 2025-01-31 06:29:56 字数 952 浏览 6 评论 0 原文

我有以下查询,而且工作正常。但是我的要求指出,我可以有多个短语,我需要在条件下匹配,有什么方法可以缩短查询。

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "primaryTags": {
              "query": "Audience1",
              "boost": 2
            }
          }
        },
        {
          "match_phrase": {
            "secondaryTags": {
              "query": "Audience1",
              "boost": 3
            }
          }
        },
        {
          "match_phrase": {
            "primaryTags": {
              "query": "Audience2",
              "boost": 2
            }
          }
        },
        {
          "match_phrase": {
            "secondaryTags": {
              "query": "Audience2",
              "boost": 3
            }
          }
        }
      ],
      "minimum_should_match": "1",
      "boost": 1
    }
  }
}

我将不得不继续添加两个匹配短语,而每个新短语都会出现。 有没有办法在一个条件下分别将所有初级和次要的短语分别使用?

I have the below query, and it's working fine. But my requirement states that I could have multiple phrases which I need to match in the OR condition, Is there any way I can shorten the query.

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "primaryTags": {
              "query": "Audience1",
              "boost": 2
            }
          }
        },
        {
          "match_phrase": {
            "secondaryTags": {
              "query": "Audience1",
              "boost": 3
            }
          }
        },
        {
          "match_phrase": {
            "primaryTags": {
              "query": "Audience2",
              "boost": 2
            }
          }
        },
        {
          "match_phrase": {
            "secondaryTags": {
              "query": "Audience2",
              "boost": 3
            }
          }
        }
      ],
      "minimum_should_match": "1",
      "boost": 1
    }
  }
}

I would have to keep adding two match phrases primary and secondary with every new phrase coming in.
Is there a way to have all the phrases for primary and secondary in one condition respectively?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-02-07 06:29:56

您可以使用。尝试以下搜索查询:

{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "Audience1",
                        "fields": [
                            "primaryTags^2",
                            "secondaryTags^3"
                        ],
                        "type": "phrase"
                    }
                },
                {
                    "multi_match": {
                        "query": "Audience2",
                        "fields": [
                            "primaryTags^2",
                            "secondaryTags^3"
                        ],
                        "type": "phrase"
                    }
                }
            ]
        }
    }
}

You can use multi_match query with type phrase. Try out the below search query:

{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "Audience1",
                        "fields": [
                            "primaryTags^2",
                            "secondaryTags^3"
                        ],
                        "type": "phrase"
                    }
                },
                {
                    "multi_match": {
                        "query": "Audience2",
                        "fields": [
                            "primaryTags^2",
                            "secondaryTags^3"
                        ],
                        "type": "phrase"
                    }
                }
            ]
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文