2
votes

I am using Elasticsearch 5.1.1. While doing a nested query, it is returning 400 Error

My Doc will look like this

{
"_index" : "test",
"_type" : "test_five",
"_source" : {
  "doc" : {
    "keyword_elas" : "elasticsearch",
    }
  },
  "doc_as_upsert" : true }

This is my Query Code

 {
"query": {
    "nested": {
        "path":"_source",
        "query": {
            "nested": {
                "path": "_source.doc",
                "query": {
                    "match": {
                        "_source.doc.keyword_elas": "elasticsearch"
                    }
                }
            }
        }
    }
}}

For the above Query i got an exception

elasticsearch.exceptions.RequestError: TransportError(400, u'search_phase_execution_exception', u'failed to create query: {\n "nested" : {\n "query" : {\n "nested" : {\n

Is this an exception due to some Query Mistake ? or any Version problem...

Thank You

1
The bottom of my error message had a specific reason property which told me I failed to create the nested type properly (when setting up my mappings): "caused_by": { "type": "illegal_state_exception", "reason": "[nested] nested object under path [comments] is not of nested type" }The Red Pea

1 Answers

3
votes

I am not quite sure why you're using a nested query in this environment.

If I have a document like this :

{
"_index" : "test",
"_type" : "test_five",
"_source" : {
  "doc" : {
    "keyword_elas" : "elasticsearch",
    }
  },
  "doc_as_upsert" : true }

And my goal is to match the keyword_elas field. All I would do is :

GET test/test_five/_search
{
    "query": {
        "match" : {
            "keyword_elas" : "elasticsearch"
        }
    }
}

Exact Matches :

Analyzed field :

 GET test/test_five/_search
    {
        "query": {
            "match" : {
                "keyword_elas" : "elasticsearch",
                 "fuzziness": "0"

            }
        }
    }

Note : If you have a document in keyword_elas that contains elasticsearch ABC , this query will work because it will be zero fuzziness on the first token (elasticsearch).

For not analyzed fields (fully exact match)

GET test/test_five/_search
 {
  "query": {
    "term" : { "keyword_elas" : "elasticsearch" } 
  }
}

If you have two documents in your index with

keyword_elas : elasticsearch

and

keyword_elas : elasticsearch abc

The term query will only match the first document.