theablefew / stretchy

Elasticsearch Models for Rails with an ActiveRecord-like behavior
https://theablefew.github.io/stretchy/#/
MIT License
2 stars 0 forks source link

Aggregation DSL #27

Open esmarkowski opened 3 months ago

esmarkowski commented 3 months ago

How it's been for years

Model.aggregation(:gender, {terms: {field: :gender}, aggs: {position: {terms: {field: 'position.name'}}}})

How it could be

I'm not sure I even like this. However, it would be nice to be able to do some validations via this approach.

Model.aggregation :gender do
   terms do
       field :gender
       aggregation :position do
            terms field: 'position.name'
       end
   end
end

Validations could be smart, so things like terms would know that field is required.

terms(field: :gender)

or

terms do
    field :gender
    size 5
    show_term_doc_count_error true
end
esmarkowski commented 3 months ago

Probably should just use this ... https://github.com/elastic/elasticsearch-dsl-ruby

esmarkowski commented 3 months ago

I spent yesterday rewriting QueryMethods and QueryBuilder to use Elasticsearch::DSL. I remember now why I didn't use it.

The frustrating part is that it's full of promise and would simplify both QueryMethods and QueryBuilder considerably, with the added bonus of being able to provide the DSL as part of stretchy-model. It also allows KeywordTransformer to execute in QueryMethods, so the values that get passed to DSL don't need additional processing.

I'm going to fork Elasticsearch::DSL and see if I can get to the bottom of it or create a fix. There's no mention of bool and regexp not being able to be defined together. Elasticsearch 8 puts the Regexp inside the query context, but the result is the same in DSL, it just removes regexp if there are any other clauses in bool.