Unable to fetch the data with help of QueryBuilders.termQuery


(Arun S P) #1

I am a newcomer to elastic search I was trying to work with high-level Rest Client

I am able to work with CRUD operations, With Search functionality, I got stuck up.

My objective to bring all the data based on the book id start with E106 search criteria

http://localhost:5918/book-elastic/books/book/E106

I added the part of the code below

I am able to get all the data using

QueryBuilders.matchAllQuery()

But I couldn't get a specific field value

QueryBuilders.termQuery("_id",bookId)

I have also shared the screenshot of the both results

Can somebody help me out on the query?

Kindly revert in case of any further information needed.

Code

public Page<BookEntity> findByBookId(String bookId, Pageable pageable) throws IOException{

        SearchRequest searchRequest = new SearchRequest(INDEX); 
        searchRequest.types(TYPE);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.fetchSource(false);
        //searchSourceBuilder.fetchSource(null, new String[]{"excludedProperty"});

        /*MatchQueryBuilder matchQueryBuilder =  new MatchQueryBuilder("id",bookId);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO); 
        matchQueryBuilder.prefixLength(3); 
        matchQueryBuilder.maxExpansions(7);*/ 

        searchSourceBuilder.from((int)pageable.getOffset());
        searchSourceBuilder.size(pageable.getPageSize());

        //searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //searchSourceBuilder.query(QueryBuilders.termQuery("_id",bookId)); 
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] objectHits = hits.getHits();
        for (SearchHit searchHit : objectHits) {
            System.out.println("***************************");
            System.out.println("Search Hit :: "+searchHit);
            System.out.println("***************************");
        }


        return null;

    }

Result Screenshot

matchAllQuery

Input : {"from":0,"size":20,"query":{"match_all":{"boost":1.0}},"_source":false}

Response

Response Value: {"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"bookdata","_type":"books","_id":"E106401","_score":1.0},{"_index":"bookdata","_type":"books","_id":"E106403","_score":1.0},{"_index":"bookdata","_type":"books","_id":"E10640","_score":1.0}]}}

  1. Input Values: QueryBuilders.termQuery("_id",bookId)

{"from":0,"size":20,"query":{"term":{"_id":{"value":"E106","boost":1.0}}},"_source":false}

Response

The Response is Null

Thanks in Advance
Arun


(Arun S P) #2

matchPhrasePrefixQuery from QueryBuilders solved my issues


(system) #3

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