【问题标题】:elasticsearch sort aggregation on categorical value分类值上的弹性搜索排序聚合
【发布时间】:2016-02-05 17:22:49
【问题描述】:

在 elasticsearch 中,我可以在第二个聚合的 numeric 字段上对聚合进行聚合和排序。

例如

GET myindex/_search
{
  "size":0,
  "aggs": {
    "a1": {
      "terms": { 
        "field": "FIELD1",
        "size":0,
        "order": {"a2": "desc"}  
      },
      "aggs":{
        "a2":{
          "sum":{
            "field":"FIELD2"
          }
        }
      }
    }
  }
}

但是,我想根据 categorical 字段值对聚合进行排序。 IE。假设 FIELD2 的值是 ("a", "b", "c") 之一——我想首先按所有文档对 a1 进行排序,FIELD2:“a”,然后是 FIELD2:“b”,然后是 FIELD2: “C”。

在我的例子中,每个 FIELD1 都有一个 唯一的 FIELD2。所以我真的只是想要一种按 FIELD2 对 a1 结果进行排序的方法。

【问题讨论】:

  • 您是否尝试过与上述查询相同的方式。 ?

标签: sorting elasticsearch aggregation categorical-data


【解决方案1】:

我不确定您到底想要什么,但我尝试了以下操作。 我用映射创建了索引

PUT your_index
{
  "mappings": {
    "your_type": {
      "properties": {
        "name": {
          "type":   "string"
        },
        "fruit" : {"type" : "string", "index": "not_analyzed"}
      }
    }
  }
}

然后我索引了一些这样的文档

PUT your_index/your_type/1
{
  "name" : "federer",
  "fruit" : "orange"
}

然后我用以下聚合对所有玩家进行排序

{
  "size": 0,
  "aggs": {
    "a1": {
      "terms": {
        "field": "name",
        "order": {
          "_term": "asc"
        }
      },
      "aggs": {
        "a2": {
          "terms": {
            "field": "fruit",
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    }
  }
}

我得到的结果是

"aggregations": {
      "a1": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "federer",
               "doc_count": 3,
               "a2": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "green apple",
                        "doc_count": 1
                     },
                     {
                        "key": "orange",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "messi",
               "doc_count": 2,
               "a2": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "apple",
                        "doc_count": 1
                     },
                     {
                        "key": "banana",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "nadal",
               "doc_count": 2,
               "a2": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "blueberry",
                        "doc_count": 1
                     },
                     {
                        "key": "watermelon",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "ronaldo",
               "doc_count": 2,
               "a2": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "banana",
                        "doc_count": 1
                     },
                     {
                        "key": "watermelon",
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }

确保您的 FIELD2 为 not_analyzed,否则您会得到意想不到的结果。

这有帮助吗?

【讨论】:

  • 他试图实现的是根据第二个聚合(内部)的结果对第一个聚合进行排序。这里的转折是第二个聚合是关于字符的术语,存储桶结果的类型是字符串值而不是整数值。
  • 如果内部聚合是一个多值存储桶,则无法将父 agg 排序在子 agg 上。
  • 是的,@piyushGoyal 是正确的。就我而言,内部聚合从来都不是多值存储桶,所以我应该能够以某种方式按 agg2 值对 agg1 值进行排序。
  • 目前 elasticsearch API 不提供此功能。我试着调整一下,看看有没有用。它只提供对单个桶 aggs 的排序,而且数字太高了..! :(
  • 即使我尝试了一些不同的查询,也没有成功。抱歉帮不上忙:(
【解决方案2】:

我找到了一种可行的方法。您必须先在 FIELD2 上聚合,然后在 FIELD1 上。

    {
      "size": 0,
      "aggs": {
        "a2": {
          "terms": {
            "size": 0,
            "field": "FIELD2",
            "order": {
              "_term": "asc"
            }
          },
          "aggs": {
            "a1": {
              "terms": {
                "size": 0,
                "field": "FIELD1",
                "order": {
                  "_term": "asc"
                }
              }
            }
          }
        }
      }
    }

【讨论】:

  • 这与您在问题中所问的有点相反,不是吗?
  • @ChintanShah25 我的目标是按 FIELD2 对 FIELD1 聚合进行排序。通过这种方式执行,FIELD1 按 FIELD2 排序(尽管以嵌套方式)。
猜你喜欢
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2015-06-06
  • 2014-10-05
相关资源
最近更新 更多