Philip O'Brien
Philip O'Brien

Reputation: 4266

Indexing/tokenizing nested JSON with elasticsearch

Say I have the following JSON object

{
  "name" : "Hulk",
  "traits" : {
    "colour" : "green",
    "patience" : "none"
  }
}

I want to be able to search using a term like "traits:patience". What is the best approach?

To make things clearer (hopefully), here is another example, say I have the following object

{
  "characters": {
    "hulk": {
      "strength": 100,
      "specialty": "smash"
    },
    "cyclops": {
      "strength": 25,
      "specialty": "lasers"
    }
  }
}

Ideally, I want to be able to search using the term hulk:specialty and get back smash. Is this possible?

Upvotes: 1

Views: 562

Answers (2)

paweloque
paweloque

Reputation: 18864

To reference nested fields use the '.'-notation:

POST /<index-name>/_search
{
    "query": {
        "match": {
            "traits.patience": "none"
        }
    }
}

Upvotes: 2

Related Questions