【问题标题】:Elasticsearch custom function scoreElasticsearch 自定义函数评分
【发布时间】:2016-06-22 15:48:12
【问题描述】:

我正在尝试使用自定义函数进行搜索以修改文档分数。 我有一个存储在医院内的专业的映射,每个专业都有优先级: 类似的东西:

hospital:{
  name: 'Fortis', 
  specialities: [
   {
   name: 'Cardiology',
   priority: 10
  },
   {
   name: 'Oncology',
   priority: 15
  }
]
}

现在我有一个功能分数:

functions: [{
     filter: {terms: {'specialities.name' => params[:search_text]}},
     script_score: {script: "_score * doc['specialities.priority'].value"}
                        },

我有一个过滤查询来匹配搜索文本到任何专业。 就像我搜索肿瘤学一样,它会匹配,然后我指定了一个 script_score 来优先考虑该专业并将其添加到文档的最终分数中。

但是,它会优先遇到它遇到的第一个专业,即 10,匹配过滤器的得分为 1,最终得分为 11 而不是 21(肿瘤学的优先级 + 1过滤匹配)

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我在 elasticsearch 中使用 嵌套映射 解决了这个问题。 Lucene 内部没有默认存储对象映射的概念,所以如果我想为每个专业存储优先级,我应该有这样的映射:

    hospital: {
      properties: {
        specialities: {
          type: nested,
          properties: {
            name: {
              type: 'string'
            }priority: {
              type: 'long'
            }
          }
        }
      }
    }
    

    参考:https://www.elastic.co/guide/en/elasticsearch/reference/2.0/nested.html

    之后,我可以使用嵌套查询定义函数得分,我的查询如下所示:

      "query": {
        "filtered": {
          "query": {
            "bool": {
              "must": [
                {
                  "nested": {
                    "path": "specialities",
                    "query": {
                      "function_score": {
                        "score_mode": "sum",
                        "boost_mode": "sum",
                        "filter": {
                          "terms": {
                            "specialities.name.raw": ["Oncology"]
                          }
                        },
                        "functions": [
                          {
                            "field_value_factor": {
                              "field": "specialities.priority"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2014-10-24
      • 1970-01-01
      • 2015-03-21
      相关资源
      最近更新 更多