Bacteries
Bacteries

Reputation: 603

Elastic : function_score on nested that match my query

I have documents indexed in Elastic like this (it's resumes) :

{
    id: 1
    title: 'Full Stack Developper',
    updatedAt: '2016-01-01'
    experiences: [
        {
            title: 'Java Software Engineer',
            endedAt: '2016-01-01'
        },
        {
            title: 'PHP Software Engineer',
            endedAt: '2008-01-01'
        },
    ]
}

{
    id : 2
    title: 'Backend Developper',
    updatedAt: '2016-01-01'
    experiences: [
        {
            title: 'Senior PHP Software Engineer',
            endedAt: '2016-01-01'
        },
        {
            title: 'Ruby On Rails advocate',
            endedAt: '2008-01-01'
        },
    ]
}

I would like to boost score for resume with XP ending in the last 5 years by eg and containing "PHP".

Is it possible to do this? (I don't see a way, but Elastic have a tons of capabilities!)

Thanks

Upvotes: 0

Views: 302

Answers (2)

Bacteries
Bacteries

Reputation: 603

Thanks to @ChintanShah25 for your answer, it's really help me. Here a full request, that search for "java" everywhere and will increase document with the term "java" in experiences title by eg. I use function_score because in my real case I have some function on it.

Hope it help (I'm not a Elastic expert, so if you see bad syntax feel free to add a comment ;) )

{
   "query": {
      "function_score": {
         "query": {
            "bool": {
               "must": {
                  "term": {
                     "_all": "java"
                  }
               },
               "should": [
                  {
                     "nested": {
                        "path": "experiences",
                        "score_mode": "sum",
                        "query": {
                           "bool": {
                              "should": [
                                 {
                                    "match": {
                                       "experiences.title": "java"
                                    }
                                 },
                                 {
                                    "range": {
                                       "experiences.start": {
                                          "gte": "now-5y"
                                       }
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

Upvotes: 0

ChintanShah25
ChintanShah25

Reputation: 12672

Yes you can boost the score of particular resumes matching certain conditions. You can combine nested query with function score. This is a basic example

{
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "experiences",
          "score_mode": "sum",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "experiences.title": "php"
                  }
                },
                {
                  "range": {
                    "endedAt": {
                      "gte": "now-5y"
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "boost": 5
    }
  }
}

This will give you desired results, I am using score_mode:sum so that candidate with multiple php experience get higher score.

Hope this helps!!

Upvotes: 2

Related Questions