elasticsearch 访问Elasticsearch建议中的有效负载
我目前正在使用Java API进行Elasticsearch 一个建议查询,返回多个结果'suggestion',我希望能够对其进行迭代并访问变量。这是通过以下方式实现的:
elasticsearch 访问Elasticsearch建议中的有效负载,
elasticsearch,search-suggestion,
elasticsearch,Search Suggestion,我目前正在使用Java API进行Elasticsearch 一个建议查询,返回多个结果'suggestion',我希望能够对其进行迭代并访问变量。这是通过以下方式实现的: val builder = es.prepareSuggest("companies").addSuggestion(new CompletionSuggestionBuilder("companies").field("name_suggest").text(text).size(count()) ) val sug
val builder = es.prepareSuggest("companies").addSuggestion(new CompletionSuggestionBuilder("companies").field("name_suggest").text(text).size(count()) )
val suggestResponse = builder.execute().actionGet()
val it = suggestResponse.getSuggest.iterator()
现在
我需要能够访问信息从有效载荷从'信息'。返回的示例如下所示:
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"companies": [
{
"text": "wells",
"offset": 0,
"length": 16,
"options": [
{
"text": "Wells Fargo",
"score": 123.0,
"payload": {
"industry_id": 130,
"id": 776,
"popularity": 123
}
},
{
"text": "Wells Real Estate Funds",
"score": 140.0,
"payload": {
"industry_id": 100,
"id": 778,
"popularity": 123
}
},
{
"text": "Wellstar Health System",
"score": 126.0,
"payload": {
"industry_id": 19,
"id": 1964,
"popularity": 123
}
}
]
}
]
}
当反复浏览每个建议时,我似乎无法获得有效负载。关于如何实现这一点,您有什么想法吗?要访问有效负载,您必须遍历选项对象。(现在,您正在重复这些建议)
我在的测试中找到了这些信息,谢谢你的帮助回答。作为对其他Java开发人员的提示:我必须像在链接测试中一样,将
Suggest.Suggestion.Entry.Option
转换为CompletionSuggestion.Entry.Option
。
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"companies": [
{
"text": "wells",
"offset": 0,
"length": 16,
"options": [
{
"text": "Wells Fargo",
"score": 123.0,
"payload": {
"industry_id": 130,
"id": 776,
"popularity": 123
}
},
{
"text": "Wells Real Estate Funds",
"score": 140.0,
"payload": {
"industry_id": 100,
"id": 778,
"popularity": 123
}
},
{
"text": "Wellstar Health System",
"score": 126.0,
"payload": {
"industry_id": 19,
"id": 1964,
"popularity": 123
}
}
]
}
]
}
import org.elasticsearch.search.suggest.Suggest.Suggestion
import org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry
...
// Get your "companies" suggestion
val suggestions: Suggestion[Entry] = suggestResponse.getSuggest.getSuggestion("companies")
val it = suggestions.getEntries.iterator()
// Iterate over all entries in the "company" suggestion
while(it.hasNext) {
val entry = it.next()
val optionsIt = entry.getOptions.iterator()
// Iterate over the options of the entry
while (optionsIt.hasNext) {
val option = optionsIt.next()
// As soon as you have the option-object, you can access the payload with various methods
val payloadAsMap = option.getPayloadAsMap
}
}