Doubt in query

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?

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:

{
	"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:

{
  "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.

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