Вложенный фильтр возвращает 0 doc_count - PullRequest
1 голос
/ 10 апреля 2020

Для этого индекса и примера данных:

PUT job_offers
{
  "mappings": {
    "properties": {
      "location": {
        "properties": {
          "slug": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        },
        "type": "nested"
      },
      "experience": {
        "properties": {
          "slug": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        },
        "type": "nested"
      }
    }
  }
}

POST job_offers/_doc
{
  "title": "Junior Ruby on Rails Developer",
  "location": [
    {
      "slug": "new-york",
      "name": "New York"
    },
    {
      "slug": "atlanta",
      "name": "Atlanta"
    },
    {
      "slug": "remote",
      "name": "Remote"
    }
  ],
  "experience": [
    {
      "slug": "junior",
      "name": "Junior"
    }
  ]
}

POST job_offers/_doc
{
  "title": "Ruby on Rails Developer",
  "location": [
    {
      "slug": "chicago",
      "name": "Chicago"
    },
    {
      "slug": "atlanta",
      "name": "Atlanta"
    }
  ],
  "experience": [
    {
      "slug": "senior",
      "name": "Senior"
    }
  ]
}

Я пытаюсь запустить фильтр на experience.slug:

GET job_offers/_search
{
  "query": {
    "nested": {
      "path": "location",
      "query": {
        "terms": {
          "location.slug": [
            "remote",
            "new-york"
          ]
        }
      }
    }
  },
  "aggs": {
    "filtered_job_offers": {
      "global": {},
      "aggs": {
        "filtered_location": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "experience.slug": [
                      "junior"
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

Ответ для этого:

  "aggregations" : {
    "filtered_job_offers" : {
      "doc_count" : 2,
      "filtered_location" : {
        "doc_count" : 0
      }
    }
  }

Почему я получаю doc_count: 0 для filtered_location вместо 1? Как я могу заставить это работать?

1 Ответ

2 голосов
/ 10 апреля 2020

Вы были довольно близко! Должен использовать вложенный запрос в агрегатах:

...
  "aggs": {
    "filtered_job_offers": {
      "global": {},
      "aggs": {
        "filtered_location": {
          "filter": {
            "bool": {
              "must": [
                {
                  "nested": {                    <-----
                    "path": "experience",             
                    "query": {
                      "terms": {
                        "experience.slug": [
                          "junior"
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...