2016-07-27 46 views
0

如何使用elasticsearch在Java的孩子查詢,以實現以下目標:如何使用elasticsearch在Java中有子查詢?

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

我曾嘗試:

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

,但返回錯誤。

回答

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(); 
相關問題