Searchkick advanced search body clause nullifies where clause

自古美人都是妖i 提交于 2019-12-25 12:54:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!