2016-02-05 311 views
1

所以我要,我需要对一些句子对于一些特定的词的查询使用案例匹配的每一个字,我的数据集看起来像这样Elasticsearch:如何获得分数的匹配查询

{ 
     "_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" }} 
     ] 
     } 
    } 
} 

所以用例是找到任何的询问技巧的文件,我需要得到每个比赛的个人得分(我需要他们对我稍后计算)。我无法使用匹配文档的总体分数,而是需要每个匹配的单词/短语的分数。

使用说明:真正的,为我提供了得分的解释,但即时通讯无法找到任何方式把这个比分并在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的对象,但我不知道如何检索它的个人分数。欢迎任何帮助。谢谢

回答

0

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

+0

雅我明白,但我需要找到分数和相应的查询片段,因为我将在整个查询中有很多其他匹配/范围查询,我不需要那些分数。我只需要这个特定查询的分数。我可以那样做吗? –