首页 > 解决方案 > Elasticsearch:按最多的应该匹配进行排名

问题描述

我有一个索引的职位描述字段。我正在尝试按匹配数对结果进行排名或排序。

例如,我正在寻找:

匹配次数最多的记录将排名最高。我在这里尝试了建议https://stackoverflow.com/a/45319822/2445717但没有按我的预期工作。

以下是我当前的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_description": {
                    "query": "friendly",
                    "operator": "and"
                    
                  }
                }
              },
              {
                "match": {
                  "job_description": {
                    "query": "honest personality",
                    "operator": "and"
                    
                  }
                }
              },
              {
                "match": {
                  "job_description": {
                    "query": "excellent communication skills",
                    "operator": "and"
                    
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

标签: elasticsearchelasticsearch-railselasticsearch-model

解决方案


match_query&minimum_should_match成功了。

发布示例代码以防有人需要它。

{
  "query": {
    "bool": {
      "should": [
        {
          "match_query": {
            "job_description": {
              "query": "friendly",
              "boost": 1
            }
          }
        },
        {
          "match_query": {
            "job_description": {
              "query": "honest personality",
              "boost": 1
            }
          }
        },
        {
          "match_query": {
            "job_description": {
              "query": "excellent communication skills",
              "boost": 1
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

推荐阅读