0

I am using elastichsearch in java 1.7.5 and after console query I want to tranform the code below to java code. It is a query with mutiple sub-aggregation and result in my confusion.

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "rawlog.auAid": {
              "from": "3007145536"
            }
          }
        },
        {
          "term": {
            "rawlog.ip": "118.70.204.171"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "aggs": {
    "articles_over_time": {
      "date_histogram": {
        "field": "loggedTime",
        "interval": "second"
      },
      "aggs": {
        "id": {
          "terms": {
            "field": "auAid"
          }
        },
        "url": {
          "terms": {
            "field": "urlId1"
          }
        },
        "devVerId": {
          "terms": {
            "field": "devVerId"
          }
        },
        "devTypeId": {
          "terms": {
            "field": "devTypeId"
          }
        },
        "osVerId": {
          "terms": {
            "field": "osVerId"
          }
        },
        "browserId": {
          "terms": {
            "field": "browserId"
          }
        }
      }
    }
  }
}

Can anyone help me to perform it ? Thanks so much

Val
  • 207,596
  • 13
  • 358
  • 360
VanThaoNguyen
  • 792
  • 9
  • 20
  • Are you using the java transport to java rest client for elasticsearch? Can you include the java code that you have from your attempt at converting it? – Phil Sep 29 '16 at 17:24
  • I am using Transport Client in elastichSearch 1.7.5 - – VanThaoNguyen Sep 29 '16 at 17:57
  • Check out http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramBuilder.html – Phil Sep 29 '16 at 20:07

1 Answers1

0

You have everything you need in the documentation here and here, but it basically goes like this:

// 1. build the query
QueryBuilder qb = boolQuery()
    .must(rangeQuery("rawlog.auAid").from(3007145536))    
    .must(termQuery("rawlog.ip", "118.70.204.171"));

// 2. build the aggregations
AggregationBuilder articlesOverTime =
    AggregationBuilders
            .dateHistogram("articles_over_time")
            .field("loggedTime")
            .interval(DateHistogramInterval.SECOND);
articlesOverTime.subAggregation(AggregationBuilders.terms("id").field("auAid"));
articlesOverTime.subAggregation(AggregationBuilders.terms("url").field("urlId1"));
articlesOverTime.subAggregation(AggregationBuilders.terms("devVerId").field("devVerId"));
articlesOverTime.subAggregation(AggregationBuilders.terms("devTypeId").field("devTypeId"));
articlesOverTime.subAggregation(AggregationBuilders.terms("osVerId").field("osVerId"));
articlesOverTime.subAggregation(AggregationBuilders.terms("browserId").field("browserId"));

// 3. make the query
SearchResponse sr = node.client().prepareSearch()
    .setQuery(qb)
    .addAggregation(articlesOverTime)
    .execute().actionGet();
Val
  • 207,596
  • 13
  • 358
  • 360
  • Can you have a look at this https://stackoverflow.com/questions/45953306/how-to-covert-this-elastic-search-functional-score-query-to-java-api – Praveen Kumar Sep 01 '17 at 20:17