# Unable to fetch the data with help of QueryBuilders.termQuery

**URL:** <https://discuss.elastic.co/t/unable-to-fetch-the-data-with-help-of-querybuilders-termquery/148051>\
**Category:** Elasticsearch\
**Created:** [September 11, 2018, 6:03am UTC](https://discuss.elastic.co/t/unable-to-fetch-the-data-with-help-of-querybuilders-termquery/148051 "2018-09-11T06:03:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arun\_S.P](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Arun\_S.P](https://discuss.elastic.co/u/Arun_S.P)\
**Post date:** [September 11, 2018, 6:03am UTC](https://discuss.elastic.co/t/unable-to-fetch-the-data-with-help-of-querybuilders-termquery/148051/1 "2018-09-11T06:03:09Z")

</div>

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](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}

 ![MatchAllQuery](https://us1.discourse-cdn.com/elastic/original/3X/e/0/e04057e9c1946f9e3f35df1770e2763204df7c9d.png)

**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}]}}

 ![Response](https://us1.discourse-cdn.com/elastic/original/3X/5/1/5113d2ac1ee1cbcd8ebe5f1a12ff6a43117a7d1a.png)

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

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

 ![Input](https://us1.discourse-cdn.com/elastic/original/3X/d/d/dd8dc1939fb93565ddc872a510909b53a332ee41.png)

**Response**

The Response is Null

 ![ResponseNul](https://us1.discourse-cdn.com/elastic/original/3X/2/8/281647b9a63c7b3a9b053db5736a1eab8e985c5e.png)

Thanks in Advance  
Arun

---

<div class="post-metadata">

**Author:** ![Arun\_S.P](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Arun\_S.P](https://discuss.elastic.co/u/Arun_S.P)\
**Post date:** [September 12, 2018, 2:01am UTC](https://discuss.elastic.co/t/unable-to-fetch-the-data-with-help-of-querybuilders-termquery/148051/2 "2018-09-12T02:01:34Z")

</div>

matchPhrasePrefixQuery from QueryBuilders solved my issues

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 10, 2018, 2:01am UTC](https://discuss.elastic.co/t/unable-to-fetch-the-data-with-help-of-querybuilders-termquery/148051/3 "2018-10-10T02:01:37Z")

</div>

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