BlueNeon
BlueNeon

Reputation: 33

Elasticsearch query nested object

I have this record in elastic:

{
    "FirstName": "Winona",
    "LastName": "Ryder",
    "Notes": "<p>she is an actress</p>",
    "Age": "40-50",
    "Race": "Caucasian",
    "Gender": "Female",
    "HeightApproximation": "No",
    "Armed": false,
    "AgeCategory": "Adult",
    "ContactInfo": [
        {
            "ContactPoint": "[email protected]",
            "ContactType": "Email",
            "Details": "Details of tv show",
        }
    ]
}

I want to query inside the contact info object and I used the query below but I dont get any result back:

{
    "query": {
        "nested" : {
            "path" : "ContactInfo",
            "query" : {
                "match" : {"ContactInfo.Details" : "Details of tv show"}
            }
        }
    }
}

I also tried:

{
   "query": {
     "term" : { "ContactInfo.ContactType" : "email" } 
   }
}

here is the mapping for contact info:

"ContactInfo":{
    "type": "object"
}

I think I know the issue which is the field is not set as nested in mapping, is there a way to still query nested without changing the mapping, I just want to avoid re-indexing data if its possible. I'm pretty new to elastic search so need your help.

Thanks in advance.

Upvotes: 3

Views: 13331

Answers (1)

Bhavya
Bhavya

Reputation: 16172

Elasticsearch has no concept of inner objects.

Some important points from Elasticsearch official documentation on Nested field type

  1. The nested type is a specialized version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.
  2. If you need to index arrays of objects and to maintain the independence of each object in the array, use the nested datatype instead of the object data type.
  3. Internally, nested objects index each object in the array as a separate hidden document, such that that each nested object can be queried independently of the others with the nested query.

Refer to this SO answer, to get more details on this

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

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

Index Mapping:

{
  "mappings": {
    "properties": {
      "ContactInfo": {
        "type": "nested"
      }
    }
  }
}

Search Query:

{
    "query": {
        "nested" : {
            "path" : "ContactInfo",
            "query" : {
                "match" : {"ContactInfo.Details" : "Details of tv show"}
            }
        }
    }
}

Search Result:

"hits": [
      {
        "_index": "stof_64269180",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.1507283,
        "_source": {
          "FirstName": "Winona",
          "LastName": "Ryder",
          "Notes": "<p>she is an actress</p>",
          "Age": "40-50",
          "Race": "Caucasian",
          "Gender": "Female",
          "HeightApproximation": "No",
          "Armed": false,
          "AgeCategory": "Adult",
          "ContactInfo": [
            {
              "ContactPoint": "[email protected]",
              "ContactType": "Email",
              "Details": "Details of tv show"
            }
          ]
        }
      }
    ]

Upvotes: 10

Related Questions