如何将 simplequerystring 与 bool/must 相结合

How to combine simplequerystring with bool/must

我有这个 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 返回这个错误:

reason: Unknown key for a START_OBJECT in [simple_query_string]

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

您可以使用 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"
            }
        }
    ]