Getting nested object under path is not of nested type in elasticsearch query

4.4k views Asked by At

I am getting this error when trying to query nested fields, this question seems repeating but with little changes, while setting mappings of fields I didn't specify nested but created nested mapping. now when I am trying to query do Neptune full-text search query using gremlin it is working but when I am trying to do a native query it is giving this error :

failed to create a query: [nested] nested object under path [predicates] is not of nested type

Elasticsearch index mapping

{
  "amazon_neptune" : {
    "mappings" : {
      "properties" : {
        "aggregateId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "document_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "entity_id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "entity_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "predicates" : {
          "properties" : {
            "group_name" : {
              "properties" : {
                "value" : {
                  "type" : "text",
                  "analyzer" : "autocomplete_analyzer",
                  "search_analyzer" : "standard"
                }
              }
            },
            "question" : {
              "properties" : {
                "value" : {
                  "type" : "text",
                  "analyzer" : "questions_auto_complete_analyzer",
                  "search_analyzer" : "standard"
                }
              }
            },
            "suggestion" : {
              "properties" : {
                "value" : {
                  "type" : "text",
                  "analyzer" : "autocomplete_analyzer",
                  "search_analyzer" : "standard"
                }
              }
            },
            "type" : {
              "properties" : {
                "value" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },
            "user_name" : {
              "properties" : {
                "value" : {
                  "type" : "text",
                  "analyzer" : "autocomplete_analyzer",
                  "search_analyzer" : "standard"
                }
              }
            }
          }
        }
      }
    }
  }
}

gremlin query which is working

g.withSideEffect('Neptune#fts.endpoint',ES Endpoint).withSideEffect('Neptune#fts.queryType', 'match').V().has('suggestion','Neptune#fts life').limit(5).valueMap().toList()

Elasticsearch query giving error:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "entity_type": "suggestions"
                    }
                },
                {
                    "term": {
                        "document_type": "vertex"
                    }
                },
                {
                    "nested": {
                        "path": "predicates",
                        "query": {
                            "nested": {
                                "path": "predicates.suggestion",
                                "query": {
                                    "match": {
                                        "predicates.suggestion.value": "life"
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

Please let me know if there is anything wrong with this query.

2

There are 2 answers

8
Val On BEST ANSWER

I didn't specify nested but created nested mapping

That's the issue. If you don't specify "type": "nested" explicitly, then predicates won't be of type nested but of type object instead, and your query won't work as you can see.

You need to explicitly define predicates as nested in your mapping, there's no other way.

UPDATE

Your native query should look like this:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "entity_type": "suggestions"
                    }
                },
                {
                    "term": {
                        "document_type": "vertex"
                    }
                },
                {
                    "match": {
                        "predicates.suggestion.value": "life"
                    }
                }
            ]
        }
    }
}
2
ESCoder On

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

Index Mapping:

{
  "mappings":{
    "properties":{
      "predicates":{
        "type":"nested",
        "properties":{
          "suggestion":{
            "type":"nested",
            "properties":{
              "value":{
                "type":"text"
              }
            }
          }
        }
      }
    }
  }
}

Index Data:

 {
      "predicates": [
        {
          "suggestion": [
            {
              "value": "life"
            }
          ]
        }
      ]
    }

Running the same search query, as given in the question:

Search Result:

"hits": [
      {
        "_index": "stof_64312693",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "predicates": [
            {
              "suggestion": [
                {
                  "value": "life"
                }
              ]
            }
          ]
        }
      }
    ]