2015-09-06 62 views
0

我試圖使用Java API ElasticSearch查詢Elasticsearch複雜的查詢,我的查詢是:使用Java API

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" 
      } 
     } 
    } 
}' 

類似的SQL邏輯是:

選擇不同(clientip)從表其中timestamp '2015-08-20'和'2015-08-21'之間,請求'/ v2/brand%'和響應='200'

如何使用Java API實現它? 請指導我是ElasticSearch的新手。提前致謝!

+0

你爲什麼不開始https://www.elastic.co/guide/en/elasticsearch/client/java-api/ current/search.html並首先想出一些解決方案? – bittusarkar

回答

1

我已經解決了這個問題,以下是我的代碼:

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();