I am using Java High level client to fetch data from elasticsearch
@GetMapping("/search")
public @ResponseBody Object search() throws IOException
{
Map<Object,Object> finalresponse = new HashMap<Object,Object>();
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("check1");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchResponse result = new SearchResponse();
result = searchResponse;
List<Object> llist = new ArrayList<Object>();
llist.add(result);
finalresponse.put("data",llist);
return finalresponse;
}
It shows All details of the data but i need only sourceAsMap in My json output what i need to do?? Any idea
It gives json as like this
dadoonet
(David Pilato)
March 19, 2019, 10:10am
3
You can probably iterate the hits and add to your llist
the content of hit.get sourceAsMap()
?
I can't clear idea in it? can u give me a snippet of code based on my code? to sourceAsMap()
dadoonet
(David Pilato)
March 19, 2019, 10:52am
5
List<Object> llist = new ArrayList<Object>();
SearchResponse response = client.search(new SearchRequest("test"), RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
llist.add(hit.getSourceAsMap());
}
I need All match not only matching "test" for that?
dadoonet
(David Pilato)
March 19, 2019, 1:43pm
8
test
is the index name in my example. You can put check1
instead if you wish.
yah!! I got it,
''' public @ResponseBody SearchResponse datesearch() throws IOException
{
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("check1");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.rangeQuery("date").from("2019-10-01").to("2019-10-02"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
}'''
For this snippet i need to take same source only what i need to do?
In this we used matchAllQuery for that I used your snippet to get SourceAsMap as list
But for,
but for range query where i need to modify with your code i can modify?
dadoonet
(David Pilato)
March 20, 2019, 7:36am
12
I don't understand what's wrong with your code.
Dv_Thiyanesh:
''' public @ResponseBody SearchResponse datesearch() throws IOException
{
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("check1");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.rangeQuery("date").from("2019-10-01").to("2019-10-02"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
}'''
it gives all details
but I need to get only SourceAsMap what to do?
dadoonet
(David Pilato)
March 20, 2019, 8:10am
14
I'm sorry but I don't understand. I think I gave you how to access the sourceAsMap()
.
In this we can take specific fields under SourceAsMap like computer_name and type like this two instead of showing all?
Any idea
dadoonet
(David Pilato)
March 20, 2019, 4:33pm
17
You asked the same question at Elasticsearch API SourceAsMap . Please don't post the same question in different places.
Okay it's easy for you to answer that why
system
(system)
Closed
April 17, 2019, 4:54pm
19
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.