简体   繁体   中英

Elasticsearch : How to sort by max number should match

I have the following Elasticsearch content, basically a list of user with nested skills require

Elasticsearch mapping from my index

{
   worker:{
    dynamic:"false",
    properties: {
     skills: {
       type:"nested",
         properties:{
           name:{
              type:"text"
           },
           id:{
              type:"text"
            }
          }
         }
      }
    }
}

and my simple data for users :

{
  "first_name": "Samir",
  "last_name": "Yundt",
  "skills":[
    {
      "id": 1,
      "name": "HTML"
    },
    {
      "id": 1,
      "name": "Javascript"
    }
  ]
},
{
  "first_name": "Kaley",
  "last_name": "Fadel",
  "skills":[
    {
      "id": 1,
      "name": "HTML"
    },
    {
      "id": 1,
      "name": "CSS"
    }
  ]
}

My ES query :

{
   query:{
      bool:{
         should:[
            {
               nested:{
                  path:"sub_processes",
                  query:{
                     bool:{
                        should:[
                           {
                              match:{
                                 "skills.name": "HTML"
                              }
                           },
                            {
                              match:{
                                 "skills.name": "CSS"
                              }
                           }
                        }
                     ]
                  }
               }
            }
         }
      ]
   }
  }
}

So how can I sort user by the max number skill match ? I found some documentation recommend use boost but it's not impossible because my input(skill name) is dynamic. Thanks all

You could use constant_score to give a fixed score per skill. Here is an example :

{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "boost": 1,
            "query": {
              "match": {
                "skills.name": "HTML"
              }
            }
          }
        },
        {
          "constant_score": {
            "boost": 1,
            "query": {
              "match": {
                "skills.name": "CSS"
              }
            }
          }
        }
      ]
    }
  }
}

This way, you will get N score point per skill. Your results will be naturally sort by skills count.

Because of queryNorm, N will be different than 1 for more than 2 tags (but 2 matching will be always strong than 1, 3 stronger than 2, etc ... no matters the tag). If you want an exact score per field, you can have a look to the keyword datatype .

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