5

I have following json structure in elastic search which stored @ http://localhost:9200/mongoindex/documents/ :

{
"text" : "OTesting1"
"otag" : "otag1"
"pages" : [{
      "text" : "1"
  "name" : "itag1"

    }, {
     "text" : "2"
     "name" : "itag2"
    }
]
}

I have created nested mapping as following to enable nested search and nested filter on it :

http://localhost:9200/mongoindex/documents/_mapping [PUT]

{
  "documents": {
    "properties": {
      "pages": {
        "type": "nested"
      }
    }
  }
}

Now executed following java code :

Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "xyz").build();
        Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
        SearchResponse response = client.prepareSearch("mongoindex")
                .setTypes("documents")
                .setQuery(QueryBuilders.nestedQuery("documents", QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("pages.text", "1"))).scoreMode("avg"))
                .execute()
                .actionGet();

but its giving me following exception :

> Exception in thread "main"
> org.elasticsearch.action.search.SearchPhaseExecutionException: Failed
> to execute phase [query], total failure; shardFailures
> {[kSKaBxjGTMSS352kukrYVw][mongoindex][0]:
> SearchParseException[[mongoindex][0]: from[-1],size[-1]: Parse Failure
> [Failed to parse source
> [{"query":{"nested":{"query":{"bool":{"must":{"match":{"pages.text":{"query":"1","type":"boolean"}}}}},"path":"documents","score_mode":"avg"}}}]]];
> nested: QueryParsingException[[mongoindex] [nested] nested object
> under path [documents] is not of nested type];
> }{[kSKaBxjGTMSS352kukrYVw][mongoindex][4]:
> SearchParseException[[mongoindex][4]: from[-1],size[-1]: Parse Failure
> [Failed to parse source
> [{"query":{"nested":{"query":{"bool":{"must":{"match":{"pages.text":{"query":"1","type":"boolean"}}}}},"path":"documents","score_mode":"avg"}}}]]];
> nested: QueryParsingException[[mongoindex] [nested] nested object
> under path [documents] is not of nested type]; }

1 Answer 1

2

You have to specify the path to your nested object in the Nested Query, in your case "path" : "pages". I'm not familiar with the Java syntax, but the equivalent REST request would look like this:

{
   "nested" : {
       "path" : "pages",
       "score_mode" : "avg",
       "query" : {
           "bool" : {
               "must" : [
                   {
                       "match" : {"pages.text" : "1"}
                   }
               ]
           }
       }
   }

}

Tangentially related, the last line of ElasticSearch error messages usually contains the info you need to debug (while the rest is pretty useless and/or repetitive). So this is the key part:

QueryParsingException[[mongoindex] [nested] nested object under path [documents] is not of nested type]; }

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Zach, thanks for your reply. Will you please verify that nested mapping which i have created is right or wrong? I tried searching with pages path also but its giving me same error.
@user1660340 Did you change your query like this: QueryBuilders.nestedQuery("pages", yourQUery);? That seems the right fix, mapping looks good at first glance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.