Comments (7)
Agreed that JBuilder is far from ideal for building large queries, though it works nicely for simple stuff.
The approach you outline is definitely valid, and allows you to define the builder in the context of your application, which is definitely a good thing.
Two notes:
- Have a look at https://gist.github.com/msonnabaum/b3950b42e5d9d8b37c2a#file-es-query-rb where Mark is using an "immediately called lambda" pattern to be able to execute custom logic in a Hash definition.
- I definitely plan to have an
elasticsearch-dsl
gem, which would expose a DSL for defining the search requests expressively in Ruby. I haven't yet started on it, though, since there are more pressing priorities, such as a "persistence" module forelasticsearch-model
.
from elasticsearch-rails.
@gzigzigzeo Closing this, if you don't mind. Please reopen, ping me, or open another issue if you want to discuss it more.
from elasticsearch-rails.
Thanks for your answer! Sorry, I did not saw the reply notification. Finally, I came to other way of sanitizing queries:
query = {
fields: [:_timestamp, :_source],
sort: { _timestamp: :desc },
size: 0,
query: {
filtered: {
filter: {
and: [
{ term: { lookup_status: options[:lookup_status] } },
{ term: { advert_import_id: options[:advert_import_id] } }
]
}
}
},
aggs: {
queries: {
terms: { field: :addressing_query_id, size: 200 }
}
}
}
Elastic::QuerySanitizer.sanitize(query) # Removes empty nodes
Sanitizer does exactly the same job, but it works post-factum. So, query stays regular hash during the building & execution which is much simplier than DSL especially for complicated queries because large query could be split into smaller chunks without any tricks.
from elasticsearch-rails.
Yeah, that can work as well. I must admit I really become to enjoy the "immediately called lambda" pattern I linked above. See eg. an example application here: https://github.com/elasticsearch/elasticsearch-rails/blob/persistence/elasticsearch-persistence/examples/sinatra/application.rb#L96-L112
Once we have the elasticsearch-dsl
gem, this all should be much more convenient.
from elasticsearch-rails.
Yep, saw it, but 1) this pattern looks much more dirty than just a plain hash. Expressions like "if t && !t.empty?" are okay until you have 2-3 terms or you do not have nested filters. 2) I am sorry, but did not understand what's the advantage of using inplace lambda vs use of instance method returning hash (def query(q, t))?
With sanitization even large queries stays readable: https://gist.github.com/gzigzigzeo/10825032 The only one bad thing about sanitization is that it should know about DSL keywords in some cases :(
from elasticsearch-rails.
There's no problem with a regular def query(q, t) ...
method, of course. I linked the example only to show how it can be done without any "query builder" object.
from elasticsearch-rails.
Ah, yes, now I understood. But unfortunately, overhead on conditions takes more lines of code than the query itself. So, we're waiting for a elasticsearch-dsl gem :) Thank you.
from elasticsearch-rails.
Related Issues (20)
- NPE happens on short prefix query on a field with index_prefixes
- Indexes are not refreshing, even with refresh_interval being set
- Disable automatic callbacks in rspec
- Not getting any records from elasticsearch HOT 1
- FYI gem version 6.1.1 was pushed without any changes HOT 1
- elasticsearch-model 7.2.1 introduces circular dependency with ruby 2.7 and rubygems 3.2.5 HOT 1
- specify an alias near the index_name in an AR model HOT 1
- Illegal argument "type" sent in all requests upgrading to 8.x HOT 1
- Not able to update single record too please help me to here HOT 1
- How to import into two clusters
- Mapping Two Ruby on Rails Models with Different Attribute Names to a Shared Elasticsearch Index using 'elasticsearch-rails'
- update_document not selective enough
- Backward compatibility for older Elasticsearch version HOT 1
- Faraday::ConnectionFailed (end of file reached) HOT 1
- Elasticsearch - Unexpected Character Error (Minus Sign) HOT 1
- Unexpected behavior when searching from a scope
- Feature Suggestion - Get Indexed Document for Instance
- Deprecation warning in Rails 7.1 (support Rails 7.2) HOT 2
- ElasticSearch::Model does not register a subclass HOT 3
- rake elasticsearch:import:model - ArgumentError: unknown keyword: :type (ArgumentError)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elasticsearch-rails.