Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi level nested objects inner_hits not working (only root level working) #13064

Closed
distributedlock opened this issue Aug 24, 2015 · 10 comments
Assignees
Labels
>bug :Search/Search Search-related issues that do not fall into other categories

Comments

@distributedlock
Copy link

distributedlock commented Aug 24, 2015

.

@martijnvg
Copy link
Member

@ranadeeppolavarapu the inner_hits syntax in the query dsl doesn't support proper nested. For now you should use the top level inner_hits syntax: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#top-level-inner-hits

This issue is related to this ticket: #11118
Although this ticket is about parent/child inner hits, the the nested inner_hits in the query dsl doesn't work there too and this should be fixed

@clintongormley
Copy link
Contributor

Closing as duplicate of #11118

@martijnvg
Copy link
Member

@ranadeeppolavarapu the following query should work:

curl -XGET "http://localhost:9200/_search" -d'
{
  "query": {
    "nested": {
      "path": "cars",
      "query": {
        "nested": {
          "path": "cars.manufacturers",
          "query": {
            "match": {
              "cars.manufacturers.country": "Japan"
            }
          }
        }
      }
    }
  },
  "inner_hits": {
    "cars": {
      "path": {
        "cars": {
          "query": {
            "nested": {
              "path": "cars.manufacturers",
              "query": {
                "match": {
                  "cars.manufacturers.country": "Japan"
                }
              }
            }
          },
          "inner_hits": {
            "manufacturers": {
              "path": {
                "cars.manufacturers": {
                  "query": {
                    "match": {
                      "cars.manufacturers.country": "Japan"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'

Downside that It is more verbose, because the query needs to be repeated on each nested level. When the embedded query dsl inner_hits syntax also properly supports multiple level of nested object fields and parent child relations then your query should work too.

@abulhol
Copy link

abulhol commented Feb 1, 2016

+1
I am wondering why the reference includes this paragraph:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#hierarchical-nested-inner-hits
which seems exactly the part that is not working

@black-square
Copy link

This bug is closed as well as #11118, but the original example from @ranadeeppolavarapu still doesn't work on 2.4. Should it be reopen again?

@martijnvg
Copy link
Member

@black-square The bug was only fixed in 5.0.x. Fixing it required a refactoring that didn't make its way back in 2.4

@abhishek5678
Copy link

Hi Guys,
In this data when i am trying with one level nested data then it's working fine but when i am adding one more nested data then it's not working if anybody will be knows about that then please reply me.I am sending my document data and mapping file also.

POST /test_word14/doc/1
{
"name": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism (D50-D89)",
"depth": 1,
"children": [{
"name": "Nutritional anemias (D50-D53)",
"depth": 2,
"children": [{
"code": "D50",
"name": "Iron deficiency anemia",
"depth": 3,
"children": [{
"code": "D50.0",
"name": "Iron deficiency anemia secondary to blood loss (chronic)",
"depth": 4
}, {
"code": "D50.1",
"name": "Sideropenic dysphagia",
"depth": 4
}, {
"code": "D50.8",
"name": "Other iron deficiency anemias",
"depth": 4
}, {
"code": "D50.9",
"name": "Iron deficiency anemia, unspecified",
"depth": 4
}]
}]
}]
}

(mapping file)
PUT /test_word16
{
"mappings": {
"doc": {
"properties": {
"name": { "type": "string" },
"depth": {"type":"long"},
"children": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"depth": { "type": "long" },
"children": {
"type": "nested",
"properties": {
"code": { "type": "string" },
"name": { "type": "string" },
"depth": { "type": "long" },
"children": {
"type": "nested",
"properties": {
"code": { "type": "string" },
"name": { "type": "string" },
"depth": { "type": "long" },
}
}
}
}
}
}
}
}
}
}

please tell me what is the query i am applying here.i am applying the query like this:
GET /icd10_codes/doc/_search
{
"_source": false,
"query": {
"nested": {
"path": "children",
"query": {
"nested": {
"path": "children.children",
"query": {
"nested": {
"path": "children.children.children",
"query": {
"bool": {
"must": [
{
"match": {
"children.children.name": "Iron"
}
}
]
}
},
"inner_hits": {
"_source": {
"excludes":["name"]
}
}
}
}
}
}
}
}

@abhishek5678
Copy link

Hi @ranadeeppolavarapu

when i am running your example then it is not working it's showing like that:Unknown key for a START_OBJECT in [inner_hits]

@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Inner Hits labels Feb 14, 2018
@ssllmit
Copy link

ssllmit commented Feb 4, 2021

Multi-level nested queries with inner_hits still appears to be an issue with Elasticsearch 7.10.0:

Create the test index

curl -XPUT "http://localhost:9200/test_index_github" -H 'Content-Type: application/json' -d'
{
  "settings" : {
    "index" : {
      "number_of_shards" : 10,
      "number_of_replicas" : 1
    }
  }
}
'

curl -XPUT "http://localhost:9200/test_index_github/_mapping" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "cars": {
      "type": "nested",
      "properties": {
        "manufacturers": {
          "type": "nested",
          "properties": {
            "country": {
              "type": "text"
            },
            "name": {
              "type": "text"
            }
          }
        },
        "model": {
          "type": "text"
        },
        "make": {
          "type": "text"
        }
      }
    },
    "last_name": {
      "type": "text"
    },
    "first_name": {
      "type": "text"
    }
  }
}
'

curl -XPUT "http://localhost:9200/test_index_github/_doc/0" -H 'Content-Type: application/json' -d'
{
  "first_name": "Zach",
  "last_name": "Foobar",
  "cars": [
    {
      "make": "Saturn",
      "model": "SL",
      "manufacturers": [
        {
          "name": "Saturn",
          "country": "USA"
        },
        {
          "name": "Honda",
          "country": "Canada"
        }
      ]
    },
    {
      "make": "Subaru",
      "model": "Imprezza",
      "manufacturers": [
        {
          "name": "Subaru",
          "country": "Japan"
        },
        {
          "name": "Daimler",
          "country": "Germany"
        }
      ]
    }
  ]
}
'

Query the test index:

curl -XPOST "http://localhost:9200/test_index_github/_search?pretty" -H 'Content-Type: application/json' -d'
{        
  "query": {
    "nested": {
      "path": "cars",
      "query": {
        "nested": {
          "path": "cars.manufacturers",
          "query": {
            "match": {                             
              "cars.manufacturers.country": "Japan"
            }
          },              
          "inner_hits": {}
        }
      },              
      "inner_hits": {}
    }
  }
}
'

Result:

{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 8,
    "skipped" : 0,
    "failed" : 2,
    "failures" : [
      {
        "shard" : 7,
        "index" : "test_index_github",
        "node" : "FohAXm0YRWSJR72OyyNqHA",
        "reason" : {
          "type" : "index_out_of_bounds_exception",
          "reason" : "index_out_of_bounds_exception: Index: 1, Size: 1"
        }
      }
    ]
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [ ]
  }
}

@ssllmit
Copy link

ssllmit commented Feb 4, 2021

Oh I see—the syntax has changed to accommodate this capability: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/inner-hits.html

Key sentence: "This indirect referencing is only supported for nested inner hits."

This query works:

curl -XPOST "http://localhost:9200/test_index_github/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "nested": {
      "path": "cars.manufacturers",
      "query": {
        "match": {
          "cars.manufacturers.country": "Japan"
        }
      },
      "inner_hits": {}
    }
  }
}
'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories
Projects
None yet
Development

No branches or pull requests

7 participants