Need some help on to query ES using java Transport client

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

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

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

Thanks a alot for your reply.

Can we use range query and select only few fields ?

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.

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 ?

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

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

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

Thanks @dadoonet. It is helpful.

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

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

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

Cool! Thank you @dadoonet for reverting.

Thanks much! ~Kedar

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