2

I am new to Elastic Search, and my data has the form:

Index Mapping

{
    "company:product:index": {
        "aliases": {},
        "mappings": {
            "company:product:mapping": {
                "properties": {
                    "attribute_heel-style": {
                        "properties": {
                            "label": {
                                "type": "keyword"
                            },
                            "name": {
                                "type": "keyword"
                            },
                            "source": {
                                "type": "keyword"
                            },
                            "value": {
                                "type": "keyword"
                            },
                            "value_label": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

Index Data

{
    "attribute_heel-style": [
        {
            "name": "heel-style",
            "label": "Heel Style",
            "value": "stiletto",
            "value_label": "Stiletto"
        },
        {
            "name": "heel-style",
            "label": "Heel Style",
            "value": "wedge"
            "value_label": "Wedge"
        },
        ... // a bunch of other objects in this array as well
    ]
}

I'd like to create a query so that I can filter all objects within the array who have the value "Wedge" for the key "value_label". How can I do this?

Edit: Found the solution! Posting below. Thank you to @EsCoder.

{
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "attribute_heel-style.value_label": "wedge"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

1 Answer 1

7

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

Refer to this ES official document on Arrays to get a detailed explanation.

Adding a working example with index data, mapping, search query, and search result

You have to reindex your data, after applying nested data type

Index Mapping:

{
  "mappings": {
    "properties": {
      "attribute_heel-style": {
        "type": "nested"
      }
    }
  }
}

Index Data:

{
  "attribute_heel-style": [
    {
      "name": "heel-style",
      "label": "Heel Style",
      "value": "stiletto",
      "value_label": "Stiletto"
    },
    {
      "name": "heel-style",
      "label": "Heel Style",
      "value": "wedge",
      "value_label": "Wedge"
    }
  ]
}

Search Query:

{
  "query": {
    "nested": {
      "path": "attribute_heel-style",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "attribute_heel-style.value_label": "Wedge"
              }
            }
          ]
        }
      },
    "inner_hits":{}
    }
  }
}

Search Result:

"inner_hits": {
          "attribute_heel-style": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.0,
              "hits": [
                {
                  "_index": "65585632",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "attribute_heel-style",
                    "offset": 1
                  },
                  "_score": 0.0,
                  "_source": {
                    "name": "heel-style",
                    "label": "Heel Style",
                    "value": "wedge",
                    "value_label": "Wedge"     // note this
                  }
                }
              ]
            }
          }
        }
      }

Update 1:

{
  "mappings": {
    "company:product:mapping": {
      "properties": {
        "attribute_heel-style": {
          "type": "nested",             // note this
          "properties": {
            "label": {
              "type": "keyword"
            },
            "name": {
              "type": "keyword"
            },
            "source": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            },
            "value_label": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi! Thanks for this, it helped put me on the right track. I was playing with ES some more, and noticed that I did not include the Index Mapping that I'm using. Could you take a look at the question with my updated edits? I would greatly appreciate it
@DavidPham Is the format of sample index data given in the question correct? And which version of elasticsearch are you using?
5.6.16 of ES. The index data has a bunch of more "keys" aside from the attribute_heel-style key, but I've ommitted some fields that are irrelevant for clarity (didn't want to clog up the post).
@DavidPham with your current index mapping, you cannot query each object independently of the other objects in the array., if you want to get the object within the array that has the value "Wedge" for the key "value_label", then you have to change the index mapping, as shown above (in the updated part). You can then use the same search query to get your desired result.
@DavidPham glad you found the solution, but with the search query given by you, you will not be able to find the object of the array that matched your query (for that you have to use nested data type) because in the question you mentioned filter all objects within the array who have the value "Wedge" for the key "value_label". If my answer helped you then please don't forget to upvote the answer as well :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.