【问题标题】:Elasticsearch : How to get score for each word matched in a match queryElasticsearch:如何获得匹配查询中匹配的每个单词的分数
【发布时间】:2016-05-15 01:19:53
【问题描述】:

所以我有一个用例,我需要查询一些特定单词的多个句子,我的数据集看起来像这样

{
        "_index": "12_index",
        "_type": "skill_strings",
        "_id": "AVKv-kM4axmY3fECZw9T",
        "_source": {
           "str": "SQL Server"
        }
     },
     {
        "_index": "12_index",
        "_type": "skill_strings",
        "_id": "AVKv-kNfaxmY3fECZw9U",
        "_source": {
           "str": "SQL .net java"
        }
     },
     {
        "_index": "12_index",
        "_type": "skill_strings",
        "_id": "AVKv-kPDaxmY3fECZw9X",
        "_source": {
           "str": "My SQL java php .net"
        }
     },
     {
        "_index": "12_index",
        "_type": "skill_strings",
        "_id": "AVKv-kOtaxmY3fECZw9W",
        "_source": {
           "str": "My SQL Server"
        }
     },
     {
        "_index": "12_index",
        "_type": "skill_strings",
        "_id": "AVKv-kOeaxmY3fECZw9V",
        "_source": {
           "str": "php asp.net Server"
        }
     }

我当前的查询看起来像这样

GET 12_index/skill_strings/_search
{
   "explain": true,
   "query" : { 
      "bool": {
         "should": [
            {"match_phrase" : { "str" : "SQL Server" }},
            {"match_phrase" : { "str" : ".net" }}
         ]
      }
   } 
}

因此,用例是查找具有所要求技能的文档,并且我需要获取每个匹配项的个人分数(我需要它们用于稍后的计算)。我不能使用匹配文档的总分,而是需要每个匹配单词/短语的分数。

使用 explain : true,为我提供了评分的解释,但我无法找到任何方法将这个分数取出并在 java 中使用

我的java代码是这样的

SearchResponse searchResponse = client.prepareSearch(index)
        .setQuery(jsonQuery)
        .setTypes(type)
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setFrom(from).setSize(size).setExplain(true)
        .execute()
        .actionGet()

for(SearchHit hit : searchResponse.getHits()){
            def id = hit.getId()
            Map resMap = hit.getSource()
            resMap.eplaination = hit.getExplanation().getDetails()
            resData.add(resMap)
        }
return resData

我可以看到当我执行 hit.getExplanation().getDetails() 时,我得到了一个 org.apache.lucene.search.Explanation 类的对象,但我不知道如何从中检索单个分数。欢迎任何帮助。谢谢

【问题讨论】:

    标签: java search elasticsearch lucene


    【解决方案1】:

    hit.getExplanation().getValue() 会给你分数。正如您提到的 hit.getExplanation().getDetails() 本身就是 org.apache.lucene.search.Explanation 数组。调用getValue()方法可以得到所有子计算的score

    【讨论】:

    • 是的,我明白,但我需要找到分数和相应的查询,因为我将在整个查询中有很多其他匹配/范围查询,我不需要分数那些。我只需要这个特定查询的分数。我可以这样做吗?
    猜你喜欢
    • 2018-11-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多