【问题标题】:elastic search sort in aggs by columnaggs 中的弹性搜索按列排序
【发布时间】:2015-06-23 12:03:14
【问题描述】:

我正在尝试在 aggs 中对弹性搜索进行排序,等效于 mysql“ORDER BY Title ASC/DESC”。这是索引结构:

'body' => array(
  'mappings' => array(
    'test_type' => array(
      '_source' => array(
        'enabled' => true
    ),
    'properties' => array(
      'ProductId' => array(
        'type'      => 'integer',
         'index'     => 'not_analyzed'
      ),
      'Title' => array(
        'type'      => 'string',
        'index'     => 'not_analyzed'
      ),
      'Price' => array(
        'type'      => 'double',
         'index'     => 'not_analyzed'
                        )
    )
  )
)

我可以按以下价格订购:

{
      "aggs": {
        "group_by_product": {
          "terms": {
            "field": "ProductId",
              "order": {
                "product_price" : "asc"
            }
          },
          "aggs": {
            "product_price": {
              "avg": {
                "field": "Price"
              }
            }
          }
        }
      }
    }

但不能按标题排序(ORDER BY Title ASC/DESC)。

有没有办法按标题排序?

非常感谢您!

问候

【问题讨论】:

  • 您想如何订购标题字段?按分数?按字母顺序?
  • 感谢您的回复@Daniel Hoffmann-Mitscherling。我想按字母顺序订购。

标签: sorting elasticsearch group-by


【解决方案1】:

编辑以反映 cmets 中的说明:

要按字符串值对聚合进行排序,请使用intrinsic sort,但目前不支持对非数字指标聚合进行排序。

"aggs" : {
        "order_by_title" : {
            "terms" : {
              "field" : "title",
              "order": {
                "_term" : "asc" 
              }
            }
        }
    }

【讨论】:

  • 你说得对,我可以按照你写的那样对它进行排序,但不是我预期的。它对查询中的命中进行排序,但不对聚合中的结果进行排序。当我们使用“aggs”时,它返回聚合,例如。 '聚合' => 数组('group_by' => 数组('dc_count_error_upper_bound' => 1,'sum_other_doc_count' => 270,'buckets' => 数组(0 => 数组(..),1 => 数组(。 ..), ... n => array(..))。我只对聚合中的结果感兴趣。如果我以错误的方式理解您的解释以及我编写的代码,请接受我的歉意评论。问候
  • 已编辑答案以反映仅对聚合进行排序。
  • 谢谢丹尼尔。它帮助我对聚合进行排序。知道如何使用聚合结果进行分页吗?我可以在“aggs”中设置“size”,但不能设置“from”。再次感谢您的帮助,您的解释很完美。
  • 抱歉@Sensini,我从来没有在 aggs 上使用过分页,但是this 线程声明它在 _term 上不存在,尽管讨论了一些解决方法。
  • 谢谢@Daniel。我感谢你的努力。问候
猜你喜欢
  • 2017-02-09
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2020-06-05
  • 1970-01-01
  • 2015-12-30
相关资源
最近更新 更多