2016-01-29 159 views
2

我想在elasticsearch的java querybuilder中實現這個elasticsearch查詢。但我無法獲得同樣的結果。有人能幫忙嗎。Java QueryBuilders的嵌套elasticsearch查詢。

GET /XX/XX/_search 
    { 
     "query": { 
      "bool": { 
      "must": [ 
       { 
        "nested": { 
         "path": "XX", 
         "filter": { 
         "term": { 
          "A": "7:140453136:T" 
         } 
         } 
        } 
       }, 
       { 
        "nested": { 
         "path": "XX", 
         "filter": { 
         "term": { 
          "B": "RF" 
         } 
         } 
        } 
       }, 
       { 
        "nested": { 
         "path": "XX", 
         "filter": { 
         "term": { 
          "C": "RFFF" 
         } 
         } 
        } 
       } 
      ] 
      } 
     } 
    } 

我試圖代碼:

QueryBuilders qbWithArguments = QueryBuilders.boolQuery() 
        .must(QueryBuilders.termQuery("A", "RF")) 
        .must(QueryBuilders.termQuery("B", "EF")) 
        .must(QueryBuilders.termQuery("C", "RF")); 

回答

3

您需要添加nested query了。使用下面的代碼:

QueryBuilders.boolQuery().must(nestedQuery("XX", FilterBuilders.termFilter("A","RF"))) 
          .must(nestedQuery("XX", FilterBuilders.termFilter("B","EF"))) 
          .must(nestedQuery("XX", FilterBuilders.termFilter("C","RF"))) 

對於更高的版本,你可以使用:

QueryBuilders.boolQuery().must(nestedQuery("XX", QueryBuilders.boolQuery().should(QueryBuilders.termQuery("A","RF")) 
      .should(QueryBuilders.termQuery("B","EF")).should(QueryBuilders.termQuery("C","RF")).minimumShouldMatch("1"))) 
+0

nestedQuery()犯規接受FilterBuilders作爲第二個參數。它期望QueryBuilder。 –

+0

QueryBuilders類中有一個接受filterbuilder作爲第二個參數的重載方法。公共靜態NestedQueryBuilder nestedQuery(字符串路徑,FilterBuilder篩選器){ 返回新的NestedQueryBuilder(路徑,過濾器); }。請參閱http://rajish.github.io/api/elasticsearch/0.20.0.Beta1-SNAPSHOT/org/elasticsearch/index/query/QueryBuilders.html – Richa

+0

感謝您的參考。但我使用ES 2.1.1這是最新的,我沒有看到在最新的API文檔重載的方法。 –