Elasticsearch Java Rest Api output

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

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()

List<Object> llist = new ArrayList<Object>();
SearchResponse response = client.search(new SearchRequest("test"), RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
    llist.add(hit.getSourceAsMap());
}

Thank you

I need All match not only matching "test" for that?

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?

What do you mean?

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?

I don't understand what's wrong with your code.

it gives all details
but I need to get only SourceAsMap what to do?

I'm sorry but I don't understand. I think I gave you how to access the sourceAsMap().

It's okay, no problem

In this we can take specific fields under SourceAsMap like computer_name and type like this two instead of showing all?
Any idea

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

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.