【问题标题】:ElasticSearch - Aggregation/Group by: sorting and paggingElasticSearch - 聚合/分组方式:排序和分页
【发布时间】:2017-10-05 11:59:27
【问题描述】:

我正在尝试使用 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) 另外,我正在尝试使用分页 - “大小”键实际上可以工作,但“来自”键不会

**UPDATE - 聚合示例结果**

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

希望有人能帮忙

【问题讨论】:

  • 您在terms 聚合中使用“订单”,这是不允许的。您要对哪些文档应用排序。你能提供示例输出吗?
  • 我的意思是“按_score排序”..抱歉评论不完整。重申我的问题。您要对哪些文档应用排序。你能提供示例输出吗?
  • @Richa,添加聚合的示例结果,希望你所要求的
  • 你的意思是你想按分数降序排列你的热门歌曲。如果我错了,请纠正我

标签: javascript node.js elasticsearch indexing nosql


【解决方案1】:

试试下面的查询

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

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

【讨论】:

  • 试过了,它仍然是随机排序的,你能尝试帮助分页吗?谢谢
  • order 在答案中设置为 asc。已经更新了。
  • 你想在哪里应用分页。在热门点击中,您只获取一个文档。 paging 在那里没有任何意义。在查询数据中,我们也设置了size=0。
  • 我将 order 更改为 desc,仍然是随机的 .. 我想在 buckets 数组上应用分页...
  • 您的意思是对桶进行聚合。这是不可能的,我想参考这个stackoverflow.com/questions/27776582/…
猜你喜欢
  • 2018-06-17
  • 2019-12-02
  • 2019-07-12
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
相关资源
最近更新 更多