0

I am trying to query on ElasticSearch using Java API, my query is:

curl -XGET 'http://localhost:9200/logstash-*/_search?search_type=count' -d '
{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
               "and" : [
               {
                  "range": {
                     "timestamp": {
                          "gte": "2015-08-20",
                          "lt": "2015-08-21",
                          "format": "yyyy-MM-dd",
                          "time_zone": "+8:00"
                     }
                  }
                  },
                  {"query": { 
                      "match": { 
                         "request": {
                            "query": "/v2/brand"
                          }
                       }
                     }
                  },
                  {"term": { "response" : "200"}
                  }
               ]
            }
        }
    },
    "aggs": {
        "group_by_device_id": {
            "terms": {
                "field": "clientip"
            }
        }
    }
}'

The similar sql logic is:

select distinct(clientip) from table where timestamp between '2015-08-20' and '2015-08-21' and request like '/v2/brand%' and response = '200'

How to implement it using Java API? Please guide I am new to ElasticSearch. Thanks in advance!

1

1 Answer 1

1

I have resolved the problem, below is my codes:

SearchResponse scrollResp1 = client.prepareSearch("logstash-*").setSearchType(SearchType.SCAN).
                    setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
                            FilterBuilders.andFilter(FilterBuilders.termFilter("response", "200")
                                    , FilterBuilders.rangeFilter("timestamp").gte(startDate).lt
                                            (endDate), FilterBuilders.queryFilter
                                            (QueryBuilders.matchQuery("request", "signup"))
                            )))
                    .addAggregation(AggregationBuilders.terms
                            ("group_by_client_ip").size(0).field("clientip")).get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.