在ElasticSearch中获取SearchResponse的结果 [英] Getting the result of a SearchResponse in ElasticSearch
问题描述
我正在尝试使用ES作为我的MongoDB的索引。我已经成功地整合了他们,但是我发现搜索API比较复杂和混乱。 Java API也没有太大的帮助。
我可以找到完全匹配的内容,但是如何获得这个结果?这是我的代码:
Node node = nodeBuilder()。
SearchResponse sr = node.client()。prepareSearch()
.addAggregation(
AggregationBuilders.terms(user)。field(admin2san)
.subAggregation(AggregationBuilders.terms(SPT)。field(64097))
)
.execute()。actionGet();
SearchHit [] results = sr.getHits()。getHits();
列表<防火墙> myfirewall = results.getSourceAsObjectList(Firewall.class);
for(防火墙信息:myfirewall){
System.out.println(search result is+ info);
}
我不太确定我明白你的问题
如果您要将searchResponse的结果打印到您的例子中,应该是这样的:
SearchHit [] results = sr.getHits()。getHits();
for(SearchHit命中:结果){
String sourceAsString = hit.getSourceAsString();
if(sourceAsString!= null){
Gson gson = new GsonBuilder()。setDateFormat(dateFormat)
.create();
System.out.println(gson.fromJson(sourceAsString,Firewall.class));
}
}
我使用Gson从Json响应中转换到FireWall(POJO)。
我希望这是你要找的。 p>
I am trying to use ES as the index for my MongoDB. I've managed to integrate them successfully, but I find the search API rather complex and confusing. The Java API is not too helpful either.
I am able to find exact matches, but how can I get this result? Here is my code:
Node node = nodeBuilder().node();
SearchResponse sr = node.client().prepareSearch()
.addAggregation(
AggregationBuilders.terms("user").field("admin2san")
.subAggregation(AggregationBuilders.terms("SPT").field("64097"))
)
.execute().actionGet();
SearchHit[] results = sr.getHits().getHits();
List<Firewall> myfirewall = results.getSourceAsObjectList(Firewall.class);
for (Firewall info : myfirewall) {
System.out.println("search result is " + info);
}
i'm not quite sure i understood your question.
if you want to print the result of your searchResponse accordind to your exemple it should be something like this :
SearchHit[] results = sr.getHits().getHits();
for(SearchHit hit : results){
String sourceAsString = hit.getSourceAsString();
if (sourceAsString != null) {
Gson gson = new GsonBuilder().setDateFormat(dateFormat)
.create();
System.out.println( gson.fromJson(sourceAsString, Firewall.class));
}
}
i'm using Gson to convert from the Json response to the FireWall(POJO).
I hope it's what you were looking for.
这篇关于在ElasticSearch中获取SearchResponse的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!