0

I need to do aggregation by 7 fields in elasticsearch, then retrieve data TopHits and do some calculations Sum and Avg. Is there any possibility to get latest buckets of hits and calculations without many loops/recursive?

Squeez
  • 919
  • 2
  • 12
  • 30

1 Answers1

2

According to Elasticsearch documentation:

"The terms aggregation does not support collecting terms from multiple fields in the same document. The reason is that the termsagg doesn’t collect the string term values themselves, but rather uses global ordinals to produce a list of all of the unique values in the field. Global ordinals results in an important performance boost which would not be possible across multiple fields.

There are two approaches that you can use to perform a terms agg across multiple fields:

Script Use a script to retrieve terms from multiple fields. This disables the global ordinals optimization and will be slower than collecting terms from a single field, but it gives you the flexibility to implement this option at search time.

copy_to field If you know ahead of time that you want to collect the terms from two or more fields, then use copy_to in your mapping to create a new dedicated field at index time which contains the values from both fields. You can aggregate on this single field, which will benefit from the global ordinals optimization."

Source: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_multi_field_terms_aggregation

EDIT: If you use copy_to field, there is not reason to index it since so you do not need to analyze it, for this you only have to change its mapping:

"metaFieldName" => [ 
    "type" => "string",
    "index" => "not_analyzed"
]
Pandawan
  • 2,027
  • 1
  • 18
  • 24
  • great, thank you. but, can I store that field without analyzers as one string? – Squeez Aug 08 '16 at 14:56
  • thanks, I solved this problem. I just doing copy_to manually :) it is cool, because I doing 2 aggregations in 2 types and use bucket key from one aggregation bucket with little differences to get aggregation bucket from second aggregation. – Squeez Aug 12 '16 at 10:26
  • now I am thinking how to do sorting. this http://stackoverflow.com/questions/38894862/elasticsearch-java-api-sorting-aggregation?noredirect=1#comment65172201_38894862 – Squeez Aug 12 '16 at 10:26