Nested query can't work using java API

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Nested query can't work using java API

Margaret-2
I created an index "blog" and a type "post". Docs in "post" are like this:
{
  "user":"Dilbert Brown",
  "posts":[
    {
     "body":"Distribution search is hard.",
     "postDate":"2011-12-15"
     },
    {
     "body":"Elasticsearch is useful",
     "postDate":"2011-12-18" 
     }
   ]
}

The mapping for "post" is:
{
  "post":{
     "properties":{
       "posts":{
          "type":"nested",
          "properties":{
             "body":{
                "type":"string"
              },
             "postDate":{
                "type":"date",
                "format":"dateOptionalTime"
              }
           }
        },
       "user":{
          "type":"string"
        }
     }
  }
}'

In shell, a nested query as follow works:
{
  "query":{
     "nested":{
        "path":"posts",
        "query":{
           "query_string":{
              "query":"posts.body:hard"
            }
         }
      }
   }
}'

In java project, I save the above query(only nested part) in a parameter "queryStr" and then pass it to SearchRequestBuilder:
String queryStr = "{" 
                        +"  \"nested\":{"
                        ......
                        +"   }" 
                        +"}"
SearchResponse resp = client.prepareSearch("blog")
                                    .setTypes("post")
                                    .setQuery(queryStr )
                                    .setFrom(0).setSize(50).setExplain(true)
                                    .execute()
                                    .actionGet();

But "resp" returns nothing.

How can nested query work using java API? Besides, using "query_string" in "nested" query is required.  

--
Thanks,

Xiaowen Huang
Student of School of Software Engineering
Beijing Jiaotong University
Add.:No.3 Shangyuancun, Haidian District, Beijing, P.R.China
Tel.: +86-152-1058-2730

--