Elasticsearch Elasticsearch Array Match: Querying and Filtering Techniques

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction

When working with Elasticsearch, you may encounter situations where you need to query and filter documents based on the values stored in an array field. This article will discuss various techniques to perform array matching in Elasticsearch, including the use of the `terms` query, `match` query, and nested objects.

1. Using the `terms` query for exact array matching

The `terms` query is useful when you want to filter documents based on exact matches in an array field. This query returns documents where the specified field contains one or more of the provided terms.

Example:

Suppose you have an index named “products” with the following documents:

{
  "product_id": 1,
  "tags": ["electronics", "smartphone"]
}

{
  "product_id": 2,
  "tags": ["electronics", "laptop"]
}

{
  "product_id": 3,
  "tags": ["clothing", "shirt"]
}
```

To find all products with the tags "electronics" or "smartphone", you can use the `terms` query as follows:

```
GET /products/_search
{
  "query": {
    "terms": {
      "tags": ["electronics", "smartphone"]
    }
  }
}

This query will return the documents with product_id 1 and 2.

2. Using the `match` query for partial array matching

The `match` query is useful when you want to filter documents based on partial matches in an array field. This query returns documents where the specified field contains the provided term.

Example:

Using the same “products” index as before, to find all products with the tag “electronics”, you can use the `match` query as follows:

GET /products/_search
{
  "query": {
    "match": {
      "tags": "electronics"
    }
  }
}

This query will return the documents with product_id 1 and 2.

3. Using nested objects for complex array matching

In some cases, you may need to perform more complex array matching, such as filtering documents based on multiple conditions within the array. In these situations, you can use nested objects to model your data and perform advanced queries.

Example:

Suppose you have an index named “orders” with the following documents:

{
  "order_id": 1,
  "items": [
    {"product_id": 1, "quantity": 2},
    {"product_id": 2, "quantity": 1}
  ]
}

{
  "order_id": 2,
  "items": [
    {"product_id": 1, "quantity": 1},
    {"product_id": 3, "quantity": 3}
  ]
}

To find all orders containing product_id 1 with a quantity of at least 2, you can use the `nested` query as follows:

First, update your index mapping to include the “items” field as a nested type:

PUT /orders/_mapping
{
  "properties": {
    "items": {
      "type": "nested"
    }
  }
}

Next, perform the nested query:

GET /orders/_search
{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "bool": {
          "filter": [
            {"term": {"items.product_id": 1}},
            {"range": {"items.quantity": {"gte": 2}}}
          ]
        }
      }
    }
  }
}

This query will return the document with order_id 1.

Conclusion

In conclusion, Elasticsearch provides various techniques to perform array matching, depending on your specific use case and data model. By using the `terms` query, `match` query, or nested objects, you can effectively filter documents based on the values stored in an array field.