# Doubt in query

**URL:** <https://discuss.elastic.co/t/doubt-in-query/272759>\
**Category:** Elasticsearch\
**Created:** [May 12, 2021, 4:21am UTC](https://discuss.elastic.co/t/doubt-in-query/272759 "2021-05-12T04:21:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSON\_SHAJI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anson_shaji/32/88066_2.png) [@ANSON\_SHAJI](https://discuss.elastic.co/u/ANSON_SHAJI)\
**Post date:** [May 12, 2021, 4:21am UTC](https://discuss.elastic.co/t/doubt-in-query/272759/1 "2021-05-12T04:21:28Z")

</div>

Hi,  
I have an index named product in elastic search. Each document contains an attribute field. This is shown below.  
{  
"category": "xyx",  
"name": "product-1",  
"attribute": [  
{  
"unit": "mm",  
"name": "Length",  
"value": "10"  
},  
{  
"unit": "",  
"name": "Color",  
"value": "Orange"  
},  
{  
"unit": "cm",  
"name": "Width",  
"value": "34"  
}  
]  
}

{  
"category": "xyz",  
"name": "product-2",  
"attribute": [  
{  
"unit": "mm",  
"name": "Length",  
"value": "20"  
},  
{  
"unit": "",  
"name": "Color",  
"value": "Red"  
},  
{  
"unit": "cm",  
"name": "Width",  
"value": "45"  
}  
]  
}

I want to get all attributes whose value be like  
{"Length": [10,20],"Color": ["Orange","Red"], "width": [34,45]

Also need the unit of each attribute name. Can anyone help me to solve this problem?

---

<div class="post-metadata">

**Author:** ![sagarpatel](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sagarpatel](https://discuss.elastic.co/u/sagarpatel)\
**Post date:** [May 12, 2021, 5:56am UTC](https://discuss.elastic.co/t/doubt-in-query/272759/2 "2021-05-12T05:56:27Z")

</div>

In ElasticSearch query work based on field name and as i know it is not possible to query using field value, what i mean is here colour, width and length is your field value and not field name.

For resolving you can denormlize your data before indexing into elasticsearch like below:

```auto
{
	"category": "xyz",
	"name": "product-2",
	"length": {
		"value": "20",
		"unit": "mm"
	},
	"Color": "Red",
	"Width": {
		"unit": "cm",
		"value": "45"
	}
}

```

Then you can query your data like this:

```auto
{
  "query": {
  "bool": {
    "must": [
      {
        "terms": {
          "length.value": [
            "10",
            "20"
          ]
        }
      },
      {
        "terms": {
          "Color": [
            "Orange",
            "Red"
          ]
        }
      },
      {
        "terms": {
          "Width": [
            "34",
            "45"
          ]
        }
      }
    ]
  }
  }
}

```

Here, I have used boolean query but you can do same using filter query as well if you dont want to cosider this fields while calculating relvancy score.

---

<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:** [June 9, 2021, 5:56am UTC](https://discuss.elastic.co/t/doubt-in-query/272759/3 "2021-06-09T05:56:34Z")

</div>

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