【问题标题】:How to combine simplequerystring with bool/must如何将 simplequerystring 与 bool/must 结合起来
【发布时间】:2020-07-02 11:19:19
【问题描述】:

我有这个 ES 版本 7 的 ElasticSearch 查询:

{
  "from": 0,
  "simple_query_string": {
    "query": "*"
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "organization_id": "fred"
          }
        },
        {
          "term": {
            "assigned_user_id": "24584080"
          }
        }
      ]
    }
  },
  "size": 50,
  "sort": {
    "updated": "desc"
  },
  "terminate_after": 50,
}

但是 ES 给了我这个错误:

原因:[simple_query_string] 中 START_OBJECT 的未知键

我的目标是能够对多个字段使用查询字符串,并且还可以将 term/match 与 bool/must 一起使用。我应该放弃查询字符串而只使用bool.must[{match:"my query"}]吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-7


    【解决方案1】:

    您可以通过这种方式使用 bool 组合多个查询。 must 子句将用作逻辑 AND,并确保所有条件都匹配。

    您需要在查询部分中包含 simple_query_string

    添加带有示例文档和搜索查询的工作示例。

    索引样本数据

    {
      "organization_id": 1,
      "assigned_user_id": 2,
      "title": "welcome"
    }{
      "organization_id": 2,
      "assigned_user_id": 21,
      "title": "hello"
    }{
      "organization_id": 3,
      "assigned_user_id": 22,
      "title": "hello welocome"
    }
    

    搜索查询:

        {
      "query": {
        "bool": {
          "must": [
              {
              "simple_query_string": {
                "fields" : ["title"],
                "query" : "welcome"
              }
            },
            {
              "match": {
                "organization_id": "1"
              }
            },
            {
              "match": {
                "assigned_user_id": "2"
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 3.0925694,
                "_source": {
                    "organization_id": 1,
                    "assigned_user_id": 2,
                    "title": "welcome"
                }
            }
        ]
    

    【讨论】:

    • @rakim 你有机会浏览我的回答吗,期待得到你的反馈
    • @rakim 运气好吗?
    • @rakim 你能回复我上面的问题吗?
    猜你喜欢
    • 2020-09-04
    • 2015-11-22
    • 2018-12-08
    • 2012-03-17
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多