Reputation: 1640
I used logstash to save my mysql table data into elasticsearch. Now i want to get data from elasticsearch using specific field. I can get data using id but i am not able to retrieve data using other fields.
I am using elasticsearch 5.6.12 and spring boot 2.0
searchcontroller.java
@GetMapping("/view/{id}")
public SearchResponse view(@PathVariable final String id) {
SearchResponse response = client.prepareSearch("user_detail").setTypes("user")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("first_name", id)).setFrom(1).setSize(4)
.setExplain(true).execute().actionGet() ;
SearchHit[] results = response.getHits().getHits();
System.out.println("Current results: " + results.length);
for (SearchHit hit : results) {
System.out.println("------------------------------");
Map<String, Object> result = hit.getSource();
System.out.println(result);
}
return response;
}
I want to search using first_name but nothing is showing. what am i doing wrong here?
Upvotes: 1
Views: 96
Reputation: 1640
My problem solved
@GetMapping("/view/{id}")
public Map<String, Object> view(@PathVariable final String id) {
System.out.println("id="+id);
SearchResponse response = client
.prepareSearch("user_detail").setTypes("user").setQuery(QueryBuilders.matchQuery("first_name", id))
.setExplain(true)
.execute().actionGet();
SearchHit[] results = response.getHits().getHits();
System.out.println("Current results: " + results.length);
for (SearchHit hit : results) {
System.out.println("------------------------------");
Map<String, Object> result = hit.getSource();
System.out.println("result="+ result);
return result;
}
return null;
}
Upvotes: 1