如何使用elasticsearch在Java中有子查询?

时间:2016-07-27 06:37:48

标签: elasticsearch

如何使用elasticsearch在Java中进行子查询以实现以下目的:

TermsLookup termsLookup = new TermsLookup("user", "user", "5", null, "uuids", null);
QueryBuilder qbquery = QueryBuilders.termQuery("uuid", termsLookup);
HasChildQueryBuilder bookNameQuery = QueryBuilders
                    .hasChildQuery("instance", QueryBuilders
                    .hasChildQuery("instance_permission", qbquery));

我试过了:

{{1}}

但返回错误。

1 个答案:

答案 0 :(得分:0)

我找到了答案:

TermsLookupQueryBuilder terms = QueryBuilders
                    .termsLookupQuery("uuid")
                    .lookupIndex("user")
                    .lookupType("user")
                    .lookupId("5")
                    .lookupPath("uuids");

HasChildQueryBuilder   hQuery = QueryBuilders
                       .hasChildQuery("instance", QueryBuilders
                       .hasChildQuery("instance_permission", terms));

        System.out.println("Exectuing Query 1");
        System.out.println(hQuery.toString());
        SearchResponse searchResponse1 = client
                .prepareSearch("foo_oa_hr_askforleave")
                .setQuery(hQuery).execute().actionGet();

        System.out.println("There were " + searchResponse1.getHits().getTotalHits()
                         + " results found for Query 1.");
        System.out.println(searchResponse1.toString());
        System.out.println();