Elastic Search. _Score is null in aggregations. Why?

老子叫甜甜 提交于 2019-12-24 00:53:06

问题


I use ES v 1.7. ES returns _score just only in "hits" section, but i'm not interested in "hits", I need data from "aggregations" section of responce with _score. Why ES do like that and How to fix it?

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
          }
        }
      }
    }
  }
}

Response:

{
   "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",
                  "_type": "participant",
                  "_id": "AVFtQCCtSH3HWHh0wItF",
                  "_score": null,
                  "_source":                   {
                     "creationDate": "2015-12-04T13:48:17.949Z",
                     "ObjectID": 26745417,
                     "ObjectData": {"BRIEFNAME": "Верный-Преверный В. В."}
                  },
                  "sort": [1449236897949]
               }]
            }}
         },
         ...
      ]
   }}
}

回答1:


Since you are using sort you would need to explicitly set "track_scores" for scores to be computed.

Example:

{
    "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
          }
        }
      }
    }
  }


}


来源:https://stackoverflow.com/questions/34175750/elastic-search-score-is-null-in-aggregations-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!