简体   繁体   中英

Nested bool and filter queries in ElasticSearch

I have an index of resumes with some nested fields. I am running the following code to get the resumes which have Ruby in their skills, and computer in their education.

GET /resume/candidates/_search
{
    "query": {
        "nested" : {
            "path" : "sections",
            "query" : {
                "bool" : {
                  "must": [
                    {"match": {"sections.skills": "Ruby"}}
                  ], 
                    "filter" : [
                    { "term" : {"sections.education": "computer"} }
                    ]
                }
            }
        }
    }
}

The bool and filter queries give proper results when I run them separately. However when i combine them, they give no result. There is a resume in my index with skills ruby and computer under education.

Alternatively, for the moment I'm leaving the score part, and putting both under the same must conditions as follows:

GET /resume/candidates/_search
{
    "query": {
        "nested" : {
            "path" : "sections",
            "query" : {
                "bool" : {
                  "must": [
                    {"match": {
                      "sections.skills": "Ruby"}},

                    {"match": {
                      "sections.education": "computer"}}

                  ]
               }
            }
        }
    }
}

It still does not return any result, but it does return results if written separately. Can anyone please help?

It would be nice to have your mapping. By the way I've tried to create your scenario using this codes and the query works:

First define the mapping

PUT resume
{
  "mappings": {
    "candidates": {
      "properties": {
        "sections": {
          "type": "nested",
          "properties": {
            "skills": {
              "type": "text"
            },
            "education": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

Then add two candidates to it:

PUT resume/candidates/1
{
  "sections": [
    {
      "skills": "Ruby",
      "education": "computer"
    },
    {
      "skills": "elastic",
      "education": "computer"
    }
  ]
}


PUT resume/candidates/2
{
  "sections": [
    {
      "skills": "kibana",
      "education": "computer"
    },
    {
      "skills": "elastic",
      "education": "computer"
    }
  ]
}

And this is the query:

GET resume/candidates/_search
{
  "query": {
    "nested": {
      "path": "sections",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "sections.education": {
                  "value": "computer"
                }
              }
            },
            {
              "match": {
                "sections.skills": "Ruby"
              }
            }
          ]
        }
      }
    }
  }
}

And it only returns the first canidate:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.87546873,
    "hits": [
      {
        "_index": "resume",
        "_type": "candidates",
        "_id": "1",
        "_score": 0.87546873,
        "_source": {
          "sections": [
            {
              "skills": "Ruby",
              "education": "computer"
            },
            {
              "skills": "elastic",
              "education": "computer"
            }
          ]
        }
      }
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM