elasticsearch aggs groupby字段和orderby得分

时间:2018-04-12 08:07:10

标签: java elasticsearch

目前我是从postman实现的,但我无法从JAVA代码实现它。下面是post json的身体。 我只想对Innovation进行全文搜索。并通过电子邮件和orderby得分。但似乎得分的总和仍然没有奏效。谁能帮助我?它现在有效。

{
"from": 0,  
  "size": 10,
  "query": {  
    "bool": {  
      "must": [  
        {  
          "query_string": {  
            "query": "Innovation"
          }  
        }  
      ]  
    }  
  },
  "highlight": {
    "require_field_match": false,
    "pre_tags" : [ "<b>" ],
    "post_tags" : [ "</b>" ],
    "order" : "score",
    "highlight_filter" : false,
    "fields": {
      "*": {}
    }
  },
  "aggs": {
   "group_by_emails": {
     "terms": { "field": "email" }
   }
 }
  }





SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
searchSourceBuilder.from(0); 
searchSourceBuilder.size(10); 
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));


QueryStringQueryBuilder queryString = new QueryStringQueryBuilder(keyword);
searchSourceBuilder.query(queryString);

Script script = new Script("_score;");
AggregationBuilder aggregation = AggregationBuilders
        .terms("agg")
        .field("email")
        .order(BucketOrder.aggregation("sum_score", false))
        .subAggregation(AggregationBuilders.sum("sum_score").script(script))
;

searchSourceBuilder.aggregation(aggregation);

System.out.println(searchSourceBuilder.toString());
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.source(searchSourceBuilder);

0 个答案:

没有答案