Elasticsearch API SourceAsMap

I use this take only 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());
}

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

hit.getSourceAsMap().get("computer_name")?

I'm sorry but I think it's a Java question now and not related to elasticsearch anymore. Am I right?

At the end, what do you want to do?

I want only specific to show specific field instead of more fields

I will try and say wait

But can only one fields but i need more than one fields? when i more than one fields it throws error

Share the code you wrote.

public @ResponseBody List datesearch2() throws IOException
{

	 SearchRequest searchRequest = new SearchRequest();
	 searchRequest.indices("winlogbeat-6.5.3-2019.03.18");
	 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
	 searchSourceBuilder.query(QueryBuilders.rangeQuery("@timestamp").from("2019-03-18T07:00:04.874Z").to("2019-03-18T09:52:04.641Z")); 
	 searchRequest.source(searchSourceBuilder); 
	 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
	
	
	 List<Object> llist = new ArrayList<Object>();
	
	 for (SearchHit hit : response.getHits()) 
	  {
	     llist.add(hit.getSourceAsMap().get("computer_name"));
	  }
	 return llist;
	
  }

What is failing here?

I need more than one field like "computer_name" and "type"

And? What is failing when you do that? What is the code you are using to do that?

llist.add(hit.getSourceAsMap().get("conputer_name","type"));

it shows error like this:
The method get(Object) in the type Map<String,Object> is not applicable for the arguments (String, String)

Ok. That's a Java question. Do you know Java?

okay I will try and get it

This:

llist.add(hit.getSourceAsMap().get("computer_name"));
llist.add(hit.getSourceAsMap().get("type"));

But I'm still unsure that fits your needs.

okay i will try

you are great!!!!!! it's working thank you

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