【发布时间】: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