Getting search query result in ElasticSearch

I am trying to use ES as an index for my MongoDB. I managed to successfully integrate them, but I find the search API quite complicated and confusing. The Java API is also not very useful.

I can 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); } 
+6
source share
2 answers

I'm not quite sure I understood your question.

If you want to print the result of your search query according to your example, 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 am using Gson to convert from a Json response to FireWall (POJO).

Hope this is what you were looking for.

+14
source

response.getHits (). getHits () [0] .getSourceAsMap () you can try something like this

0
source

Source: https://habr.com/ru/post/987651/


All Articles