Elasticsearch subaggregation not working as expected - elasticsearch

I am trying to perform aggregation on a term and then perform sub aggregation on the result test to filter the results on a date range. But sub aggregation filter has no affect on the search response. The search response is always returning all the documents without applying filter.
For example:
TermsBuilder aggregationBuilders = AggregationBuilders.terms("form.id").field("form.id").size(0);
aggregationBuilders.subAggregation(AggregationBuilders.filter("indexDate").filter(QueryBuilders.rangeQuery("indexDate").lte(date)));

You need to use filter aggregations the other way around, i.e. as a top aggregation and then you add the terms aggregation as a sub-aggregation.
TermsBuilder formBuckets = AggregationBuilders.terms("form.id")
.field("form.id")
.size(0);
FilterBuilder dateFilter = AggregationBuilders.filter("indexDate")
.filter(QueryBuilders.rangeQuery("indexDate").lte(date))
.subAggregation(formBuckets);
I see in your other question, you have somehow "solved" this issue by moving the filter on indexDate to the query section. That will also work in your case.

Related

How to search result set from a result set in elasticsearch

Im tring to understand the DSL query i needed if i want to make a search from a result set i got. means i have an initial term search, then i want make another query upon the previous result.
lets say i a have 10 documents with a sharing identifier between them, each document has a description field. i want to search first all the documents containing the value 'Hello' in the description and then take their id's, and search the document containing the value 'good by'.
thanks.
No need to execute two queries, you can use filter context that will filter out the results.filter parameter will filter out documents that do not match, and will also not affect the score for matching documents.
Filter context is in effect whenever a query clause is passed to a
filter parameter, such as the filter or must_not parameters in the
bool query, the filter parameter in the constant_score query, or the
filter aggregation.
Refer this to know more about Query and Filter contexts

Elasticsearch java api get average of terms aggregation

I'm using elasticsearch with java api and I'm trying to get average value of lowest record from each bucket of term aggregation. One solution I found is to get results like this
AggregationBuilders.terms("group_by_flights").field("flight_id)
.subAggregation(AggregationBuilders.min("minimum").field("duration")))
and then count average on the code side. The problem is that if there will be lot of result, it will allocate a lot of memory to count it. I would like to do this on elastic side.
I found, that there is something like avg bucket pipeline aggregation, which can be add as sibling aggregation to terms (and others)
"the average": {
"avg_bucket": {
"buckets_path": "some_bucket_path"
}
}
Problem is that in java api you can add pipeline aggregation only as subaggregation. So if we construct our aggregation like this our terms aggregation won't be seen
AggregationBuilders.terms("group_by_flights").field("flight_id")
.subAggregation(PipelineAggregatorBuilders.avgBucket("avg", "group_by_flights.duration" *<- this wont't be seen because its subaggregation*))
I was thinking about making some empty top aggregation and then add all aggregations as subaggregations, but it seems like silly walk-around, and I'm not understanding something correctly.
Any ideas?
The only solution I found so far is to make aggregations as sub aggregation of "empty aggregation"
AggregationBuilders.global("global_aggregation")
.subAggregation((AggregationBuilders.terms("group_by_flights").field("flight_id"))
.subAggregation(AggregationBuilders.min("min").field("duration")))
.subAggregation(PipelineAggregatorBuilders.avgBucket("avg_bucket_aggs","group_by_flights>min"))
My solution is use FilterAggregationBuilder to do it, this one can filtering data.The first sub aggregation to make data bucket, the second sub aggregation to merge bucket data.
AggregationBuilders.filter("global_aggregation", bool)
.subAggregation((AggregationBuilders.terms("group_by_flights").field("flight_id"))
.subAggregation(AggregationBuilders.min("min").field("duration")))
.subAggregation(PipelineAggregatorBuilders.avgBucket("avg_bucket_aggs", "group_by_flights>min"));

How to filter the aggregation results in Kibana (elastic search)?

I want to filter the elastic search aggregation results in Kibana (v6.2). For example, I want to show only sum of hours those that are more than 100 (like HAVING command in SQL). I know that we can filter the results in filter section over other fields, but I don't know how to apply the filter on aggregation functions. I tried to use post_filter in filter section in Kibana, but it didn't work.
Any ideas?
You can augment aggregation query within advanced field
It will be added to request as shown on picture
Another question is what to put into this field. You can check script values for sum aggregation

ElasticSearch number of elements change when using filter and minScore

I am using a query like this:
Select all results for Keyword "X" with min_score = 0.25. Also I am doing aggregations for this results. But when I am clicking on an aggregation, the number of documents becomes different, because this min_score. When I remove the min_score, everything is fine.
What can I do, so I have ever the same count on the aggregations and on the results.
Here is the answer:
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-bool-query.html#_scoring_with_literal_bool_filter_literal
Use filter instead of must

Using matching document original score in filter script for custom filters score query

I want to use "custom filters score" query and use filters to control the score of resulting documents.
I want a way to use the document's original score as computed by ElasticSearch, and then use that score to calculate the final score of the document, which matches the given filters.
Something like "_docScore * 50/100" as a script for a filter, where "_docScore" is the original score of a document that matches the filter.
How to achieve this in ElasticSearch?
Any help is greatly appreciated.
Regards & Thanks,
Aditya.
Documents in a filtered query would be unranked and have the same score.
http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query/
But you can use a custom score query together with a filtered query and use a script to calculate a score based on the document values. This was added in 0.90, I believe.

Resources