1

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?

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.