ElasticSearch - 聚合/分组:排序和分页

时间:2017-05-07 12:27:10

标签: javascript node.js elasticsearch indexing nosql

我尝试使用Elasticsearch(2.4)聚合分组" productId"使用该查询的多个索引

{
  "from": 0,
  "size": 0,
  "min_score": 0.15,
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "match_phrase_prefix": {
                "keywords.family": {
                  "query": "low fat milk",
                  "fuzziness": 0.7,
                  "boost": 5
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "group_by_0": {
      "terms": {
        "field": "productId",
        "size": 50

      },
      "aggs": {
        "top_tag_hits": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

1)我想按_score排序,所以我尝试使用

"order": {"_score": "desc"}

返回

"type": "aggregation_execution_exception",
"reason": "Invalid term-aggregator order path [_score]. Unknown aggregation [_score]"

2)另外,我尝试使用分页 - " size"关键实际上工作但是"来自"韩元'吨

**更新 - 聚合的示例结果**

{
  "took": 5108,
  "timed_out": false,
  "_shards": {
    "total": 105,
    "successful": 105,
    "failed": 0
  },
  "hits": {
    "total": 9963,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_0": {
      "doc_count_error_upper_bound": 69,
      "sum_other_doc_count": 9779,
      "buckets": [
        {
          "key": 98761,
          "doc_count": 36,
          "top_tag_hits": {
            "hits": {
              "total": 36,
              "max_score": 0.36901662,
              "hits": [
                {
                  "_index": "retailer-1",
                  "_type": "product",
                  "_id": "1409421",
                  "_score": 0.36901662,
                  "_source": {
                    "productId": 98761
                  }
                }
              ]
            }
          }
        },
        {
          "key": 107459,
          "doc_count": 36,
          "top_tag_hits": {
            "hits": {
              "total": 36,
              "max_score": 0.42744976,
              "hits": [
                {
                  "_index": "retailer-2",
                  "_type": "product",
                  "_id": "1402563",
                  "_score": 0.42744976,
                  "_source": {
                    "productId": 107459
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

希望有人可以提供帮助

1 个答案:

答案 0 :(得分:0)

尝试以下查询

{
 "from": 0,
 "size": 0,
 "query": {
 "filtered": {
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase_prefix": {
            "keywords.family": {
              "query": "low fat milk",
              "fuzziness": 0.7,
              "boost": 5
            }
               }
             }
            ]
          }
        }
      }
   },
  "aggs": {
  "group_by_0": {
    "terms": {
    "field": "productId",
    "size": 50

  },
     "aggs": {
        "top_tag_hits": {
           "top_hits": {
              "size": 1,
              "sort": [
                 {
                    "_score": {
                       "order": "desc"
                    }
                 }
              ]
           }
        }
     }
    }
   }
  }

这将按照得分的降序排列热门命中。对于聚合桶的分页,这是目前无法实现的。可能会在较新版本中获得此功能。