# Need some help on to query ES using java Transport client

**URL:** <https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785>\
**Category:** Elasticsearch\
**Created:** [September 26, 2017, 6:46am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785 "2017-09-26T06:46:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 26, 2017, 6:46am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/1 "2017-09-26T06:46:26Z")

</div>

Need some help on to query ES using java Transport client. I need to apply few filter conditions and also select only few fields from the ES.

Example: Let's say, I have many fields in the documents as below and I have @timestamp which is also a field in the response { .............., @timestamp=Fri Jul 07 01:36:00 IST 2017, ..............}, Here how can I get the only field @timestamp from all the documents for my index ? and also how can I apply filter on this Timestamp field ?

Please let me know. Many Thanks! ~KD

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 26, 2017, 7:20am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/2 "2017-09-26T07:20:58Z")

</div>

> Here how can I get the only field @timestamp from all the documents for my index ?

If in the mapping you explicitly stored the `@timestamp` field, then you can run a Search Request and set the `storedField` to get as `@timestamp` instead of `_source` (default).

[https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java#L728-L730](https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java#L728-L730)

> and also how can I apply filter on this Timestamp field ?

You can probably use a Range Query. See [Term level queries | Java Transport Client (deprecated) [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-term-level-queries.html#java-query-dsl-range-query)

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 26, 2017, 7:36am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/3 "2017-09-26T07:36:03Z")

</div>

**Thanks a alot for your reply.**

> [@dadoonet](#):
>
> > > Here how can I get the only field @timestamp from all the documents for my index ?
> 
> > If in the mapping you explicitly stored the `@timestamp` field, then you can run a Search Request and set the `storedField` to get as `@timestamp` instead of `_source` (default).
> 
> @timestamp is the time when record is pushed to ES from Logstash. adding code as `client.prepareSearch(searchIndex).storedFields("@timestamp")`n is not helping, it is returning everything do we have any other way to do it ?  
> [https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java#L728-L730](https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java#L728-L730)
> 
> > > and also how can I apply filter on this Timestamp field ?
> 
> > You can probably use a Range Query. See [Term level queries | Java Transport Client (deprecated) [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-term-level-queries.html#java-query-dsl-range-query)

Can we use range query and select only few fields ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 26, 2017, 7:38am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/4 "2017-09-26T07:38:52Z")

</div>

> Can we use range query and select only few fields ?

Yes. I don't understand why you are asking this question. I'm missing something may be.

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 26, 2017, 7:46am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/5 "2017-09-26T07:46:35Z")

</div>

I am new to ES, and I have written some code as below:

```
                SearchResponse response = client.prepareSearch(searchIndex + "*").storedFields("@timestamp")
                    .setScroll(new TimeValue(6000))
                    .setSize(100).get();
                    // max of 100 hits will be returned for each scroll

                // Scroll until no hits are returned
                do {
                    for (SearchHit searchHit : response.getHits().getHits()) {
                        // Handle the hit...
                        get response= searchHit.getSource();
                    }

                    response = client.prepareSearchScroll(response.getScrollId())
                        .setScroll(new TimeValue(3600))
                        .execute().actionGet();
                } while (response.getHits().getHits().length != 0);
            }

```

Now as I explained earlier I need to add the code to apply fielter based on @timestamp field and select only @timestamp here. Could you please help ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 26, 2017, 9:02am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/6 "2017-09-26T09:02:08Z")

</div>

The `prepareSearch()` method allows to pass a query as explained in:

[https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html)

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 26, 2017, 9:41am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/7 "2017-09-26T09:41:47Z")

</div>

Thanks a lot David. It helped me a lot. Bravo!  
Just one question I have, If my filter node is present at second level then how to apply it.

eg: if I have inner node to query, as  
{\_SOURCE:  
{ level1:  
{ "inner ": "123"}  
}  
}  
then, how can we do a query here for inner field?

Many Thanks, ~KD

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 26, 2017, 9:54am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/8 "2017-09-26T09:54:05Z")

</div>

The field name is then `level1.inner`. You can filter on that field name.

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 27, 2017, 6:39am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/9 "2017-09-27T06:39:22Z")

</div>

Thanks @dadoonet. It is helpful.

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 27, 2017, 9:45am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/10 "2017-09-27T09:45:46Z")

</div>

Hi @dadoonet,

Thanks a lot for all your perfect replies, Apologies to bother you further.

In ES how to apply select conditions on multiple fields.

Eg, Say If i have data as:  
{ ... "\_source":  
{  
"time1":"2017-09-06T13:30:40.014Z",  
"time2":"2017-09-06T13:20:40.014Z"  
}  
....}

Now, I need to select documents who has time1 \>="2017-09-06T13:30:40.014Z" and time2 \<="2017-09-06T13:30:40.014Z", How can we do this ?

Many Thanks!  
~Kedar

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 27, 2017, 9:57am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/11 "2017-09-27T09:57:42Z")

</div>

Can we do this by using **BoolQueryBuilder** is this the way possible ? Many Thanks,Kedar.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 27, 2017, 10:06am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/12 "2017-09-27T10:06:02Z")

</div>

Yes. That's the way to go.

Bool + 2 `filter` clauses.

See [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-bool-query.html)

---

<div class="post-metadata">

**Author:** ![kedarsdixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kedarsdixit/32/20502_2.png) [@kedarsdixit](https://discuss.elastic.co/u/kedarsdixit)\
**Post date:** [September 27, 2017, 10:10am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/13 "2017-09-27T10:10:14Z")

</div>

Cool! Thank you @dadoonet for reverting.

Thanks much! ~Kedar

---

<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 25, 2017, 10:10am UTC](https://discuss.elastic.co/t/need-some-help-on-to-query-es-using-java-transport-client/101785/14 "2017-10-25T10:10:15Z")

</div>

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