Need some help on to query ES using java Transport client


(Kedar Dixit) #1

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


(David Pilato) #2

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).

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

You can probably use a Range Query. See https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-term-level-queries.html#java-query-dsl-range-query


(Kedar Dixit) #3

Thanks a alot for your reply.

Can we use range query and select only few fields ?


(David Pilato) #4

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.


(Kedar Dixit) #5

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 ?


(David Pilato) #6

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


(Kedar Dixit) #7

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


(David Pilato) #8

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


(Kedar Dixit) #9

Thanks @dadoonet. It is helpful.


(Kedar Dixit) #10

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


(Kedar Dixit) #11

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


(David Pilato) #12

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


(Kedar Dixit) #13

Cool! Thank you @dadoonet for reverting.

Thanks much! ~Kedar


(system) #14

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