Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to control the order of methods when chaining multiple? #300

Open
teddieliljekvist opened this issue May 12, 2022 · 2 comments

Comments

@teddieliljekvist
Copy link

I am trying to implement something that functions like post_filter, to be able to return aggregations not included in the filter.
When I try to build a query with query, aggregations and filter, in that order, bodybuilder re-orders the methods and generates the filter before everything else.

Is it possible to achieve this result or something that functions the same way?

{
  "query": {
    "match": {
      "rvcategory": "kilim"
    }
  },
  "aggregations": {
    "agg_terms_rvcategory": {
      "terms": {
        "field": "rvcategory"
      }
    },
    "agg_terms_shape": {
      "terms": {
        "field": "shape"
      }
    }
  },
  "filter": {
    "term": {
      "shape": "rectangular"
    }
  },
  "size": 10,
  "from": 0
}
@ferronrsmith
Copy link
Collaborator

bodybuilder()
		.query("match", "rvcategory", "kilim")
        .filter('term', 'shape')
        .agg('terms', 'rvcategory')
		.agg('terms', 'shape')
		.size(10)
		.from(0)
        .build()

@frantvesson101
Copy link

It seems like you're encountering a challenge with the order of operations in your query. Have you considered using a nested aggregation within a filtered aggregation? This way, you can achieve the desired review result of returning aggregations not included in the filter. Here's an example structure you could try:

json
Copy code
{
"query": {
"match": {
"rvcategory": "kilim"
}
},
"aggregations": {
"filtered_agg": {
"filter": {
"term": {
"shape": "rectangular"
}
},
"aggregations": {
"agg_terms_rvcategory": {
"terms": {
"field": "rvcategory"
}
},
"agg_terms_shape": {
"terms": {
"field": "shape"
}
}
}
}
},
"size": 10,
"from": 0
}
This way, the agg_terms_rvcategory and agg_terms_shape aggregations will be computed after applying the filter. Remember to adapt the field names to match your specific mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants