Elasticsearch Java API(七)--多级嵌套搜索

要在java中实现一个有三级父子关系的嵌套搜索,相关资料很少,发在stackoverflow上以后一个Switzerland的大神很快回复了我,google+stackoverflow很好使。对应的命令行query:

{
      "query": {
        "has_child": {
          "type": "instance",
          "query": {
            "has_child": {
              "type": "instance_permission",
              "query": { "terms": { "uuid": { "index": "user", "type": "user", "id": "5", "path": "uuids" } } } }
          }
        }
      }
    }

java api:

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();

你可能感兴趣的:(Elasticsearch,Elasticsearch专栏)