Pergunta

Existe uma maneira de recuperar as agregações baldes em uma busca de resposta, com a API java ?

{
  "took" : 185,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 200,
    "max_score" : 1.0,
    "hits" : [...]
  },
  "aggregations" : {
    "agg1" : {
      "buckets" : [...]
    },
    "agg2" : {
      "buckets" : [...]
    }
  }
}

Atualmente, é possível obter as agregações, mas eu não consigo descobrir como obter os baldes.

Atual versão 1.0 do ElasticSearch (v1).0.0.Beta2) ainda é uma versão beta, e talvez esse recurso ainda não foi adicionado, mas não encontrei informações sobre esse ponto também.

Foi útil?

Solução

Olhando para o ES-fonte no Github Eu vejo em seus testes:

SearchResponse response = client().prepareSearch("idx").setTypes("type")
                .setQuery(matchAllQuery())
                .addAggregation(terms("keys").field("key").size(3).order(Terms.Order.count(false)))
                .execute().actionGet();

Terms  terms = response.getAggregations().get("keys");
Collection<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(3));

Outras dicas

Se alguém perguntar sobre o acesso a documentos reais contagem destes baldes de código a seguir pode ajudar.

Terms  terms = response.getAggregations().get("agg1");
Collection<Terms.Bucket> buckets = terms.getBuckets();
for (Bucket bucket : buckets) {
    System.out.println(bucket.getKeyAsText() +" ("+bucket.getDocCount()+")");
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top