elasticsearch_Score在聚合中为null为什么?

问题描述

由于您正在使用排序,因此您需要为要计算的分数显式设置“track_scores ”。

例:

{
    "size": 1,
        "query": {
            "bool": {
                "must": [
                    { "match": {"_all": {"query": "test","operator": "and","fuzziness": "2"}}}
                ],
                "should": [
                    { "multi_match" : {
                            "query":      "test"
                            ,"type":       "best_fields"
                            ,"fields":     ["ObjectData.PRTNAME","ObjectData.EXTERNALID","ObjectData.contactList.VALUE","*SERIES","*NUMBER","ObjectData.INN"]
                            ,"operator":   "or"
                            ,"boost": 3
                    }}
                ]
            } 
        },   
  "aggs": {
    "byObjectID": {
      "terms": {"field": "ObjectID"},
      "aggs": {
        "latestVer": {
          "top_hits": {
            "sort": [{"creationDate": { "order": "desc" }}]
            ,"_source": { "include": ["ObjectData.BRIEFNAME", "creationDate", "ObjectID" ]}
            ,"size": 1,
            'track_scores" : 1
          }
        }
      }
    }
  }


}

解决方法

我使用ES v 1.7。ES仅在“命中”部分返回_score,但是我对“命中”不感兴趣,我需要 使用_score 从响应的“聚合”部分中
获取数据 。为什么ES会喜欢这样,以及如何解决?

Requset:

{
    "size": 1,"query": {
            "bool": {
                "must": [
                    { "match": {"_all": {"query": "test","operator": "and","fuzziness": "2"}}}
                ],"should": [
                    { "multi_match" : {
                            "query":      "test","type":       "best_fields","fields":     ["ObjectData.PRTNAME","ObjectData.EXTERNALID","ObjectData.contactList.VALUE","*SERIES","*NUMBER","ObjectData.INN"],"operator":   "or","boost": 3
                    }}
                ]
            } 
        },"aggs": {
    "byObjectID": {
      "terms": {"field": "ObjectID"},"aggs": {
        "latestVer": {
          "top_hits": {
            "sort": [{"creationDate": { "order": "desc" }}],"_source": { "include": ["ObjectData.BRIEFNAME","creationDate","ObjectID" ]},"size": 1
          }
        }
      }
    }
  }
}

响应:

{
   "took": 16,"timed_out": false,"_shards":    {
      "total": 5,"successful": 5,"failed": 0
   },"hits":    {
      "total": 87,"max_score": 5.3479624,"hits": [      {
         "_index": "crmws","_type": "participant","_id": "AVFtAkIcSH3HWHh0wIkd","_score": 5.3479624,"_source":          {
            "mostRecentVersion": null,"UserLogin": "ap","creationDate": "2015-12-04T12:40:43.292Z","_id": null,"ObjectID": 26784418,"EventID": null,"version_id": 3798,"ObjectTypeId": null,"ObjectData":   {...},"ObjectTypeSysName": "participant","versionNumber": null
         }
      }]
   },"aggregations": {"byObjectID":    {
      "doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets":       [
                  {
            "key": 26745417,"doc_count": 21,"latestVer": {"hits":             {
               "total": 21,"max_score": null,"hits": [               {
                  "_index": "crmws","_id": "AVFtQCCtSH3HWHh0wItF","_score": null,"_source":                   {
                     "creationDate": "2015-12-04T13:48:17.949Z","ObjectID": 26745417,"ObjectData": {"BRIEFNAME": "Верный-Преверный В. В."}
                  },"sort": [1449236897949]
               }]
            }}
         },...
      ]
   }}
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...