0

I am working on a resume database on elasticsearch. there are nested fields. For example, there is a "skills" section. "skills" is a nested field containing "skill" and "years". I want to be able to do a query that returns a skill with a certain year. For example, I want to get resumes of people with 3 or more years of "python" experience.

I have successfully run a query that does the following:

It returns all the resumes that has "python as a skills.skill and 3 as a skills.year This returns result where python is associated with 2 years or experience as long as some other field is associated with 3 years of experience.

GET /resumes/_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "skills.skill":   "python" }}, 
        { "match": { "skills.years": 3 }}  
      ]
    }
  }
}

Is there a better way to sort the data where that 3 is more associated with python?

1 Answer 1

1

You need to make use of Nested DataType and corresponding to it you would need to make use of Nested Query

What you have in current model appears to be basic object model.

I've mentioned sample mapping, sample documents, nested query and response below. This would give you what you are looking for.

Mapping

PUT resumes
{
  "mappings": {
    "mydocs": {
      "properties": {
        "skills": {
          "type": "nested",
          "properties": {
            "skill": {
              "type": "keyword"
            },
            "years": {
              "type": "integer"
            }

          }
        }
      }
    }
  }
}

Sample Documents:

POST resumes/mydocs/1
{
  "skills": [
    {
      "skill": "python",
      "years": 3
    },
    {
      "skill": "java",
      "years": 3
    }
    ]
}

POST resumes/mydocs/2
{
  "skills": [
    {
      "skill": "python",
      "years": 2
    },
    {
      "skill": "java",
      "years": 3
    }
    ]
}

Query

POST resumes/_search
{
  "query": {
    "nested": {
      "path": "skills",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "skills.skill": "python"
              }
            },
            {
              "match": {
                "skills.years": 3
              }
            }
          ]
        }
      }
    }
  }
}

Query Response:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.6931472,
    "hits": [
      {
        "_index": "resumes",
        "_type": "mydocs",
        "_id": "1",
        "_score": 1.6931472,
        "_source": {
          "skills": [
            {
              "skill": "python",
              "years": 3
            },
            {
              "skill": "java",
              "years": 3
            }
          ]
        }
      }
    ]
  }
}

Note that you only retrieve the document having id 1 in the above response. Also note that just for sake of simplicity I've made skills.skill as keyword type. You can change it to text depending on your use case.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.