【问题标题】:Elasticsearch - combining query_string and bool query in filterElasticsearch - 在过滤器中结合 query_string 和 bool 查询
【发布时间】:2015-02-25 23:56:11
【问题描述】:

query_stringbool query 可以合并到 filter query 中吗?

例如 -

{
  "filter": {
    "query_string": {
      "query": "field:text"
    }
  },
  "bool": {
    "should": {
      "match": {
        "field": "text"
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch lucene query-string booleanquery


    【解决方案1】:

    bool 旨在用于将各种查询组合成一个 bool 查询。 您可以使用 bool 以这种方式组合多个查询 -

    {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "field:text"
              }
            },
            {
              "match": {
                "field": "text"
              }
            }
          ]
        }
      }
    }
    

    must 子句将确保所有条件都匹配。 您还可以使用 should ,这将确保在仅使用 should 的情况下匹配任一查询。

    由于 bool 只是另一种查询类型,您还可以在 bool 查询中加入 bool 查询,如下所示 -

    {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "query_string": {
                      "query": "field:text"
                    }
                  },
                  {
                    "match": {
                      "field": "value"
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "field": "text"
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 我们如何定位多个字段?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多