简体   繁体   中英

Elasticsearch java top level query

I am trying to run a search on the following json using the java api

{
  "query": {
    "has_child": {
      "query" : {
        "filtered": {
          "query": { "match_all": {}},
          "filter" : {
            "and": {"filters":[
              {"term": {"term": "value"}}
            ]}
          }
        }
      },
      "child_type": "child"
    }
  }
}

Here is the java code I have,

        QueryBuilders.hasChildQuery("child", QueryBuilders
                           .filteredQuery(QueryBuilders.matchAllQuery(),
                                         FilterBuilders.andFilter(FilterBuilders.
                                          termFilter("term", "value"))));

However this just produces the json

{"has_child":
    {"query":
        {"filtered":
            {"query":{"match_all":{}},
                "filter":{
                "and":
                    {"filters":[
                        {"term":{"term":"value"}}
                        ]
                    }
                }
            }
        },
        "child_type":"child"
    }
}

As you can see I am missing a top level { "query" : ... } but I cannot find out how to add this top level query using the java api for elasticsearch.

From my understanding inside the code

client.prepareSearch("parent-child")
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(query)
            .setFrom(0).setSize(60).setExplain(true)
            .execute()
            .actionGet();

It autowraps the query with {"query" : ...}

My issue was that I was searching on the wrong index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM