elasticsearch - 如何将simplequerystring与bool/must结合

标签 elasticsearch elasticsearch-7

我有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]


我的目标是能够对多个字段使用查询字符串,并且还可以将术语/匹配与 bool(boolean) /必须一起使用。我应该放弃查询字符串而只使用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"
            }
        }
    ]

关于elasticsearch - 如何将simplequerystring与bool/must结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62686818/

相关文章:

elasticsearch - Elasticsearch仅在索引具有字段时才对索引使用过滤器

docker - docker-compose down, up, stop 中抛出错误。 (命名卷 X 在服务 Y 中使用,但在卷部分中未找到声明。)

elasticsearch - FluentD无法刷新ElasticSearch(K8S,AWS)

elasticsearch - Elasticsearch 中的匹配短语查询中的单个单词是否有字符限制?

elasticsearch - Elasticsearch _count查询请求缓存

elasticsearch - 在ElasticSearch 7.5中多样化搜索结果

elasticsearch - 与第一个搜索词相对应的Elastic Search Boost查询

Elasticsearch 术语聚合性能

elasticsearch - Windows无法在本地启动Elastic Search Service 7.4.2

logging - 在ELK中结合日志和查询