Aggregation order by prefix


(Emrah) #1

Hi,

How I can get sorted bucket by prefix?

For example, score it is higher for "Buffalo" and but I want results with "Fal..." in the beginning to be on top.

If I set include in terms then Buffalo won't appear :confused:

Should I achieve this using some sort of script?

Thanks


(Zachary Tong) #2

Can you share the query you are using? Are you trying to improve the ranking of search hits, or the sorting of aggregation results?

If you're trying to order the bucket results for display purposes, the bucket_sort pipeline agg may do what you need


(Emrah) #3

Sure, below you may find the agg I use.

Because there are 4 buckets I can not do sorting and pagination.

  "aggs": {
    "prefix": {
      "filter": {
        "prefix": {
          "name": {
            "value": "in"
          }
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "include": "in.*",
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "second": {
      "filter": {
        "wildcard": {
          "name": "* in*"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "middle": {
      "filter": {
        "wildcard": {
          "name": "*in*"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "suffix": {
      "filter": {
        "wildcard": {
          "name": "*in"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    }
  }