简体   繁体   English

Elasticsearch:如何按最大数量排序应匹配

[英]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内容,基本上是具有嵌套技能的用户列表

Elasticsearch mapping from my index 我索引中的Elasticsearch映射

{
   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 : 我的ES查询:

{
   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. 我发现一些文档建议使用Boost,但这并非不可能,因为我的输入(技能名称)是动态的。 Thanks all 谢谢大家

You could use constant_score to give a fixed score per skill. 您可以使用constant_score给出每个技能的固定分数。 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. 这样,您将获得每项技能N分的分数。 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). 由于使用了queryNorm,对于2个以上的标签,N会不同于1(但是2个匹配项始终会比1强,而3个匹配会比2强,依此类推……与标签无关)。 If you want an exact score per field, you can have a look to the keyword datatype . 如果您想要每个字段的准确分数,可以查看关键字datatype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用elasticsearch排序? - How to sort with elasticsearch? 我应该如何构建一个数据库/构建一个查询,我按照朋友的数量与它进行交互来对帖子进行排序 - How should I structure a DB / build a Query where I sort posts by number of friends 'interacting' with it 使用另存为块的查询时如何创建具有minimum_number_should_match的布尔查询 - How to create a bool query with minimum_number_should_match when using queries saved as blocks 如何在Tire内设置Elastic Search的minimum_number_should_match? - How can I set up Elastic Search's minimum_number_should_match within Tire? 如何在rails elasticsearch上模糊匹配ruby。 - How to fuzzy match a ruby on rails elasticsearch. 如何使用Searchkick在Elasticsearch中匹配不完整的术语? - How to match incomplete terms in Elasticsearch using Searchkick? 如何在ElasticSearch中执行部分匹配查询? - How to perform a partial match query in ElasticSearch? 如何使用Elasticsearch对视图内容编制索引? - How should I index view content with Elasticsearch? 如何按字母顺序排序标题和数字? - How to sort titles alphabetically and number? 如何为导轨应用增加 Elasticsearch 中的索引最大结果 window? - how to increase index max result window in Elasticsearch for a rails application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM