get relevant data on elastic search

Viewed 137

Need to apply search conditions on skills and years of experience on candidates

below are the conditions

  1. Skills can be mandatory or non-mandatory
  2. if we apply skills as mandatory then search result should include records which have skills and sorted accordingly
  3. If we apply skills as non-mandatory search result should sort based on the most matched skills
  4. Along with skills years of experience can also be applied like experience is greater than or less than some years, If years of experience applied then search result include records which match both skill name and years of experience condition and sorted with most matched records

Below are the details on mappings and query applied which is not working

//myindexmapping

{
                              "skills": {
                                "type": "nested",
                                "dynamic": "true",
                                "properties": {
                                  "id": {
                                    "type": "long"
                                  },
                                  "isHighlight": {
                                    "type": "boolean"
                                  },
                                  "skill": {
                                    "type": "text",
                                    "fields": {
                                      "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 32766,
                                        "normalizer": "lowerasciinormalizer"
                                      }
                                    }
                                  },
                                  "yearsOfExp": {
                                    "type": "float"
                                  }
                                }
                              }
                            }

below is my index data

{
                              "_index": "tenant1_candidate",
                              "_type": "docs",
                              "_id": "953",
                              "_score": 0.0074906712,
                              "_source": {
                                "record": {
                                  "skills": [
                                    {
                                      "id": 0,
                                      "skill": "Jr.Software Developer",
                                      "yearsOfExp": 2,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "AJAX",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Angular Developer",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "AngularJS",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Apache Subversion",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "API",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Bachelors Degree",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Back End",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Bootstrap",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Cascading Style Sheets",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "CRUD",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "CSS3",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Design Patterns",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Excellent analytical and logical programming skills",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Front End",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Git",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "HTML",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "HTML5",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "JavaScript",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "jQuery",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Jr. UI Developer",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "JSON",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft Access",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft SQL Server",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft Visual Studio",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "MySQL",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Object Oriented Analysis/Design",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Responsive Web Design",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "REST API",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "RESTful",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Test-Driven Development",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "TypeScript",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Version Control",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Web Services",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "XML",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    }
                                  ]
                                },
                                "entityType": "Candidate",
                                "index": "tenant1_candidate",
                                "dbId": "953",
                                "id": "953"
                              }
                            }

below is my search query

 {
                              "from": "0",
                              "size": "30",
                              "track_scores": true,
                              "query": {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "entityType": "candidate"
                                      }
                                    },
                                    {
                                      "nested": {
                                        "path": "record.skills",
                                        "score_mode": "avg",
                                        "query": {
                                          "bool": {
                                            "should": [
                                              {
                                                "bool": {
                                                  "must": [
                                                    {
                                                      "match": {
                                                        "record.skills.skill.keyword": "XML"
                                                      }
                                                    },
                                                    {
                                                      "range": {
                                                        "record.skills.yearsOfExp": {
                                                          "gte": 0
                                                        }
                                                      }
                                                    }
                                                  ]
                                                }
                                              },
                                              {
                                                "bool": {
                                                  "must": [
                                                    {
                                                      "match": {
                                                        "record.skills.skill.keyword": "SQL"
                                                      }
                                                    },
                                                    {
                                                      "range": {
                                                        "record.skills.yearsOfExp": {
                                                          "gte": 0
                                                        }
                                                      }
                                                    }
                                                  ]
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
 

                      

Example Data:

Candidate1 : name: x, skills: [ { name: sk1, exp: 2 }, { name: sk2, exp: 5 }, { name: sk3, exp: 3 }]

Candidate2 : name: y, skills: [ { name: sk1, exp: 1 }, { name: sk5, exp: 4 }, { name: sk3, exp: 1 }]

in search query skills can be mandatory or nonmandatory

  1. If sk1 with 2 years of experience is applied as mandatory then only candidate1 should come in results
  2. If sk1 with 1 year of experience is applied mandatory then candidate1 and candidate2 should come in results and candidate1 should be the first record as it has more years of experience
  3. If sk1 with 2 years of experience and sk5 with 4 years of experience as nonmandatory then both candidate1 and 2 should appear in the results however candidate2 should be the first record in the result as it matches mores skills
0 Answers
Related