Sort aggregation by other query


(Mihalikv) #1

Hi,

i want to ask you if it is possible to do something like this:
Mapping

{
    "mappings": {
        "test": {
            "properties": {
                "title": {
                    "type": "text"
                },
                "categories": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword",
                            "index": "true"
                        }
                    }
                }
            }
        }
    }
}

Data

POST test_index/test/
{
    "title" : "title 0",
    "categories" : ["category 0", "category 1"]
}

POST test_index/test/
{
    "title" : "title 1",
    "categories" : ["category 0", "category 2"]
}

Query

{
  "size": 5,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "fields": [
              "name",
              "categories"
            ],
            "query": "category 1"
          }
        }
      ]
    }
  },
  "aggs": {
    "services": {
      "terms": {
        "field": "categories.raw",
        "size": 10
      }
    }
  }
}

Result

"hits": {
    "total": 2,
    "max_score": 0.68324494,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "AWQTm9qnWteqYLKtTuAR",
        "_score": 0.68324494,
        "_source": {
          "title": "title 0",
          "categories": [
            "category 0",
            "category 1"
          ]
        }
      },
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "AWQTm-DZWteqYLKtTuAS",
        "_score": 0.39556286,
        "_source": {
          "title": "title 1",
          "categories": [
            "category 0",
            "category 2"
          ]
        }
      }
    ]
  },
  "aggregations": {
    "categories": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "category 0",
          "doc_count": 2
        },
        {
          "key": "category 1",
          "doc_count": 1
        },
        {
          "key": "category 2",
          "doc_count": 1
        }
      ]
    }
  }

I know that aggs are default sorted by doc_count, but i want to know if it's possible somehow sort aggregeation by some inner score. Use case is mixed search where i have listed articles and also categories. When user type category 1, he maybe expect search just in categories and category 1 should be first.

Is it some option for this or maybe solution is combining multiple queryes ?


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.