Elasticsearch query to match all tokens inside a specific field

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.

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.

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.

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