# Elasticsearch query to match all tokens inside a specific field

**URL:** https://discuss.elastic.co/t/elasticsearch-query-to-match-all-tokens-inside-a-specific-field/336927
**Category:** Elasticsearch
**Tags:** kql-kibana-query-language
**Created:** [June 26, 2023, 7:17pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-match-all-tokens-inside-a-specific-field/336927 "2023-06-26T19:17:20Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![vsha041](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vsha041/32/106633_2.png) [@vsha041](https://discuss.elastic.co/u/vsha041)
#### Post date: [June 26, 2023, 7:17pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-match-all-tokens-inside-a-specific-field/336927/1 "2023-06-26T19:17:21Z")

</div>

We have a scenario where for one of the fields of the index should contain all the words in order for that field to be treated as a match. Whereas other fields can contain the rest of search query. Here are few examples. Note that mapping of fields can be changed if needed.

```auto
DELETE mytestindex
PUT mytestindex
{
  "mappings": {
    "properties": {
      "product": {
        "type": "text"
      },
      "brand": {
        "type": "text"
      },
      "attributes": {
        "type": "keyword"
      }
    }
  }
}
POST mytestindex/_doc/1
{
  "product": "Sliced Pineapple",
  "brand": "Dole",
  "attributes" : [
            "Sugar Free",
            "Zero Sugar",
            "Zero Salt",
            "Low Salt",
            "Gluten Free"
          ]
}
POST mytestindex/_doc/2
{
  "product": "Apple",
  "brand": "Sugar company",
  "attributes" : [
            "Sugar Free",
            "Zero Sugar",
            "Zero Salt",
            "Low Salt",
            "Gluten Free"
          ]
}
POST mytestindex/_doc/3
{
  "product": "refined sugar",
  "brand": "Mr Cat",
  "attributes" : [
            "Sugar Free",
			"low sugar",
            "Zero Sugar",
            "Zero Salt",
            "No Salt",
            "Gluten Free"
          ]
}
POST mytestindex/_doc/4
{
  "product": "Wraps",
  "brand": "Dominoes Pizza",
  "attributes" : [
            "Sugar Free",
            "Zero Sugar",
            "Zero Salt",
            "Low Salt",
            "Gluten Free"
          ]
}
POST mytestindex/_doc/5
{
  "product": "Himalayan Pink Salt",
  "brand": "sunfood",
  "attributes" : [
            "Sugar Free",
            "Zero Sugar",                        
            "Gluten Free"
          ]
}

```

Searching for **pineapple** should return document 1 because entire search term is part of product field.

Searching for **pine** will return nothing.

Searching for **gluten** will return nothing.

Searching for **sugar** should return document 2 and 3 only because of matching brand (document 2) and product (document 3) field respectively.

Searching for **salt** should return document 5 only because of matching product field.

Searching for **low sugar salt** should return document 2 because low salt is the 4th item of the array attributes and sugar is present in the brand field. All 3 words matched.

Searching for **zero refined sugar** should return document 3 because zero sugar is the 3rd item of the array attributes and refined is present in the product. All 3 words matched.

Searching for **gluten pineapple free** should return document 1 because gluten free is the last item of the array attributes and pineapple is present in the product. All 3 words matched.

So basically, if attribute field is used by ES then all the words of an array index should be present inside the search query (order doesn't matter so **low sugar** or **sugar low** are same) and whereas rest of the search query fields must match individual words from other fields.

I did some research and looks like I need to use **multi\_match** query of type **cross\_fields** to search across multiple fields but I din't have much success even after trying various combinations. For example - this query isn't working properly.

```auto
GET mytestindex/_search
{
  "query": {
    "multi_match": {
      "fields": ["brand","product","attributes"],
      "operator": "and",
      "query": "Zero refined Sugar",
      "type": "cross_fields"
    }
  }
}

Any help will be great. Thanks.

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 24, 2023, 7:18pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-match-all-tokens-inside-a-specific-field/336927/2 "2023-07-24T19:18:08Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
