You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch是否支持多值AND查询?5.3版本嵌套数组查询需求

Answers to Your Elasticsearch Questions

1. Does Elasticsearch support AND queries for multi-value fields?

Absolutely, but the approach depends on how your data is structured:

  • Simple multi-value fields (e.g., an array of strings/numbers like tags: ["foo", "bar"]): Use a bool query with must clauses to enforce AND logic. For example, to find documents with both "foo" and "bar" tags, pair two term queries inside bool.must.

  • Arrays of objects (like your items field): This requires special handling. By default, Elasticsearch flattens object arrays, merging fields from all objects into a single list. Without proper scoping, a query for items.id: "1" AND items.range.from: <=15 might accidentally match across different objects in the array. To fix this, use the nested query to ensure all conditions apply to the same object within the array.

2. Correct Query for Matching ID and Corresponding Range

First, ensure your items field is mapped as a nested type. If it isn’t already, you’ll need to set up or update your index mapping (note: in Elasticsearch 5.3, you can’t modify existing mappings, so you may need to reindex your data if the current mapping uses the default object type for items).

Example Nested Mapping

PUT product
{
  "mappings": {
    "item": {
      "properties": {
        "userId": { "type": "keyword" },
        "items": {
          "type": "nested",
          "properties": {
            "id": { "type": "keyword" },
            "range": {
              "properties": {
                "from": { "type": "integer" },
                "to": { "type": "integer" }
              }
            }
          }
        }
      }
    }
  }
}

Query to Match a Specific ID and Its Range

Let’s say you want to find the document where userId is "123", and there’s an item with id "1" that includes the value 15 in its range. Here’s the correct query:

GET product/item/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "userId": "123" } },
        {
          "nested": {
            "path": "items",
            "query": {
              "bool": {
                "must": [
                  { "term": { "items.id": "1" } },
                  { "range": { "items.range.from": { "lte": 15 } } },
                  { "range": { "items.range.to": { "gte": 15 } } }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

How This Works:

  • The nested query scopes inner conditions to the items array, ensuring the id match and range checks apply to the same object in the array.
  • The bool.must inside the nested query enforces that all conditions (matching id, value >= from, value <= to) are met for that single item.

Adjust the range conditions based on your needs (e.g., looking for items where range.from < 25 or range.to > 50).

内容的提问来源于stack exchange,提问作者parth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:59:28