【问题标题】:How to do sorting on a field with composite aggregation in elastic search如何在弹性搜索中对具有复合聚合的字段进行排序
【发布时间】:2020-11-15 21:10:15
【问题描述】:

如何在弹性搜索中使用复合聚合对字段进行排序。

我们正在使用弹性搜索 6.8.6 版本并尝试通过复合聚合实现对字段的排序。 但是我们无法通过聚合获得预期的结果。

这是我们的映射

{
  "properties": {
    "department": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },      
    "project": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },
    "billingUnit": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },
    "billingType": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },
    "application": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },
    "environmet": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256.0,
          "type": "keyword"
        }
      }
    },
    "cost": {
      "type": "float"
    }
  }
}

通过使用以下查询,我们无法进行排序,结果不按字母顺序:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "department": {
              "query": "HR",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1.0
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [
    {
      "project.keyword": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "TERM_RANGE": {
      "composite": {
        "size": 10000,
        "sources": [
          {
            "billingUnitKey": {
              "terms": {
                "field": "billingUnit.keyword",
                "missing_bucket": false
              }
            }
          },
          {
            "billingTypeKey": {
              "terms": {
                "field": "billingType.keyword",
                "missing_bucket": false
              }
            }
          }
        ]
      },
      "aggregations": {
        "TOTAL": {
          "sum": {
            "field": "cost"
          }
        },
        "dataHits": {
          "top_hits": {
            "from": 0,
            "size": 1,
            "version": false,
            "seq_no_primary_term": false,
            "explain": false,
            "_source": {
              "includes": [
                "application.keyword",
                "environmet.keyword",
               
              ],
              "excludes": []
            },
            "docvalue_fields": [
              {
                "field": "application.keyword"
              },
              {
                "field": "environmet.keyword"
              }
            ]
          }
        },
        "paginate_bucket": {
          "bucket_sort": {
            "sort": [],
            "from": 0,
            "size": 100,
            "gap_policy": "SKIP"
          }
        }
      }
    }
  }
}

在没有聚合的情况下,排序工作正常

{
  "query": {
    "match": {
      "department": "HR"
    }
  },
  "size": 100,
  "sort": [
    {
      "project.keyword": {
        "order": "desc"
      }
    }
  ]
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-6.8


    【解决方案1】:

    【讨论】:

    • 欢迎提供指向解决方案的链接,但请确保您的答案在没有它的情况下有用:在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在它,然后引用如果目标页面不可用,您链接到的页面的最相关部分。可能会删除仅是链接的答案。
    猜你喜欢
    • 2020-02-21
    • 2016-06-04
    • 2018-08-06
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2015-03-02
    相关资源
    最近更新 更多