Java – get the result of searchresponse in elasticsearch

I am trying to use es as the index of mongodb I successfully integrated them together, but I found the Search API quite complex and confusing The Java API doesn't help much either

I can find a perfect match, but how can I get this result? This 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);
}

Solution

I'm not sure I understand your problem

If you want to print the result of searchresponse according to your example, it should be as follows:

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 the JSON response to firewall (POJO)

I hope that's what you're looking for

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>