问题
If I do an advanced search with searchkick for elasticsearch-rails like so:
products = Activity.search body: {query: {bool: {must: [{ range: { min_age: {lte: 5} } }, { range: { max_age: {gte: 3} } } ] } } }, where: { category: "Programs", start_time: '2015-07-22' }
... the where clause is not being applied. Is this to be expected? Must I use body to achieve all of my queries? Is there a way to get around this in searchkick?
slow log output:
[2015-07-29 21:03:53,369][INFO ][index.search.slowlog.query] [ES Dev] [activities_development_20150729165655530][2] took[21.1ms], took_millis[21], types[], stats[], search_type[QUERY_THEN_FETCH], total_shards[5], source[{"query":{"bool":{"must":[{"range":{"min_age":{"lte":5}}},{"range":{"max_age":{"gte":3}}}]}}}], extra_source[],
回答1:
I think you need to try this:
products = Activity.search body: {bool: {must: [{ range: { min_age: {lte: 5} } }, { range: { max_age: {gte: 3} } } ] } }, where: { category: "Programs", start_time: '2015-07-22' }
The difference is that the body
does not contain a query
field at the first level. I think searchkick makes the assumption that body
IS the query
part and it will transform it into a filtered
query if a where
clause is present.
Try it out...
回答2:
conditions = { category: "Programs", start_time: '2015-07-22' }
query = Activity.search params[:query], execute: false, where : conditions
query.body[:query] = {bool: {must: [{ range: { min_age: {lte: 5} } }, { range: { max_age: {gte: 3} } } ] } }
query.body[:size] = 10
query.execute
来源:https://stackoverflow.com/questions/31714273/searchkick-advanced-search-body-clause-nullifies-where-clause