2016-04-13 113 views
0

我需要使用Java API實現下面的function_score查詢。我無法找到elasticsearch如何使用Java API的Elasticsearch FunctionScore查詢

{ 
    "size": 40, 
    "query": { 
    "function_score": { 
     "functions": [ 
     { 
      "gauss": { 
      "location": { 
       "origin": { 
       "lat": 39.99606, 
       "lon": 116.480484 
       }, 
       "offset": "2km", 
       "scale": "3km" 
      } 
      } 
     } 
     ], 
     "score_mode": "sum", 
     "boost_mode": "replace", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "range": { 
       "startTime": { 
        "from": "2016-04-12T12:00:00.000Z", 
        "to": "2016-04-12T16:00:00.000Z", 
        "include_lower": true, 
        "include_upper": true 
       } 
       } 
      }, 
      { 
       "range": { 
       "endTime": { 
        "from": "2016-04-12T12:00:00.000Z", 
        "to": "2016-04-12T16:00:00.000Z", 
        "include_lower": true, 
        "include_upper": true 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

的Java API的節function_score查詢的任何正式文件我使用Java API這樣

RangeQueryBuilder queryStartDate = QueryBuilders.rangeQuery("startTime").gte(param.getStartTime()) 
       .lte(param.getEndTime()); 

     RangeQueryBuilder queryEndDate = QueryBuilders.rangeQuery("endTime").gte(param.getStartTime()) 
       .lte(param.getEndTime()); 

// query 
QueryBuilder querys = QueryBuilders.boolQuery().must(queryStartDate).must(queryEndDate); 

// function 
Map<String, Double> locationMap = new HashMap<String, Double>(); 
     locationMap.put("lat", param.getLat()); 
     locationMap.put("lon", param.getLng()); 
     ScoreFunctionBuilder sfb2 = ScoreFunctionBuilders.gaussDecayFunction("location", locationMap, "1km") 
       .setOffset("2km"); 

//function_score 
FunctionScoreQueryBuilder sa = new FunctionScoreQueryBuilder().boostMode("replace").add(sfb2); 

//final search 
SearchResponse response = client.prepareSearch().setIndices("film").setTypes("data").setQuery(sa) 
       .setQuery(querys).setSize(40).execute().actionGet(); 

,但它沒有作用

回答

2

您需要若要在您的FunctionScoreQueryBuilder中添加如下產品的查詢:

FunctionScoreQueryBuilder fqBuilder = QueryBuilders.functionScoreQuery(querys); 
    fqBuilder.boostMode(CombineFunction.REPLACE); 
    fqBuilder.scoreMode(ScoreMode.Sum.name().toLowerCase()); 
    fqBuilder.add(sfb2); 
    SearchResponse response = client.prepareSearch().setIndices("film").setTypes("data").setQuery(fqBuilder).setSize(40).execute().actionGet(); 
+0

非常感謝您幫助我,它工作正常,我想知道如何查找elasticsearch java-api文檔?我找不到官方文檔,像CombineFunction.REPLACE,我甚至不知道const參數... – kevin

+0

如果你只是看api文檔,你可以很容易地找到它。只要看看類,你會發現一個帶簽名的api:public FunctionScoreQueryBuilder boostMode(CombineFunction combineFunction) – Rahul

相關問題