Как заказать агрегирование отфильтрованных терминов по количеству нефильтрованных документов - PullRequest
0 голосов
/ 06 апреля 2020

Я изучаю Elasticsearch и пытаюсь выполнить поиск по поиску работы. Мой индекс выглядит следующим образом:

PUT job_offers
{
  "mappings": {
    "properties": {
      "locations": {
        "type": "keyword"
      },
      "experience": {
        "type": "keyword"
      }
    }
  }
}

POST job_offers/_doc
{
  "title": "Junior Ruby on Rails Developer",
  "locations": ["New York", "Atlanta", "Remote"],
  "experience": ["Junior"]
}

POST job_offers/_doc
{
  "title": "Ruby on Rails Developer",
  "locations": ["Chicago", "Atlanta"],
  "experience": ["Senior"]
}

Я хочу найти все документы с предложениями работы с опытом работы: Junior и местонахождением Remote или New York и создать правильное количество фасетов.

Ниже запрос работает нормально, я получаю правильные значения.

GET job_offers/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "locations": [
              "Remote",
              "New York"
            ]
          }
        },
        {
          "terms": {
            "experience": [
              "Junior"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "filtered_job_offers": {
      "global": {},
      "aggs": {
        "locations": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "experience": [
                      "Junior"
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "filtered_locations": {
              "terms": {
                "field": "locations",
                "min_doc_count": 0
              }
            }
          }
        },
        "experience": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "locations": [
                      "Remote",
                      "New York"
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "filtered_experience": {
              "terms": {
                "field": "experience",
                "min_doc_count": 0
              }
            }
          }
        }
      }
    }
  }
}

Ответ:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.0,
    "hits" : [
      {
        "_index" : "job_offers",
        "_type" : "_doc",
        "_id" : "MEZOTHEBWOTjdViQKfx-",
        "_score" : 2.0,
        "_source" : {
          "title" : "Junior Ruby on Rails Developer",
          "locations" : [
            "New York",
            "Atlanta",
            "Remote"
          ],
          "experience" : [
            "Junior"
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "filtered_job_offers" : {
      "doc_count" : 2,
      "locations" : {
        "doc_count" : 1,
        "filtered_locations" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "Atlanta",
              "doc_count" : 1
            },
            {
              "key" : "New York",
              "doc_count" : 1
            },
            {
              "key" : "Remote",
              "doc_count" : 1
            },
            {
              "key" : "Chicago",
              "doc_count" : 0
            }
          ]
        }
      },
      "experience" : {
        "doc_count" : 1,
        "filtered_experience" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "Junior",
              "doc_count" : 1
            },
            {
              "key" : "Senior",
              "doc_count" : 0
            }
          ]
        }
      }
    }
  }
}

Можно ли как-то отсортировать результаты агрегации по _count из нефильтрованной версии этих агрегаций?

В этом случае Chicago всегда будет первым (при условии, что я использую desc заказ), поскольку есть 2 документа с местоположением Chicago?

...