[英]How to do multi facets search in thinking sphinx rails
Using thinking-sphinx
for facets in my app and define two facets price
and year
.在我的应用程序中使用thinking-sphinx
作为构面并定义两个构面price
和year
。
I want to fetch facets nested results.我想获取方面嵌套的结果。
Now I am getting facets by year and it give count against year.现在我逐年获得方面,并且逐年计算。
years = records[:year]
Along with the count of records along with year I also want to get min price in that year.除了记录的数量以及年份,我还想获得那一年的最低价格。
Now I getting it like现在我明白了
years.map do |year,count|
price = Data.where(:year=>year).minimum(:price)
{count: count,cheapest_price: price}
end
But its like LazyLoading as N+1 against year.但它就像 LazyLoading 作为 N+1 对年。 I want to fetch prices along within year facets.我想在年内获取价格。
Is there any way to do it?有什么办法吗?
I think this code does what you want - though you may need to tweak things to get exactly what you're after:我认为这段代码可以满足您的需求-尽管您可能需要进行一些调整才能获得您想要的东西:
Data.search(
:select => "groupby() AS year, MIN(price) as min_price, count(DISTINCT sphinx_internal_id) as count",
:group_by => :year,
:max_matches => 1000,
:middleware => ThinkingSphinx::Middlewares::RAW_ONLY
)
Each option explained:每个选项解释:
select
specifies the appropriate aggregations you're after, and groupby()
is a function that refers to the attribute this query is grouping by. select
指定您所追求的适当聚合,而groupby()
是一个 function 引用此查询分组依据的属性。group_by
specifies the attribute you want the query to group by. group_by
指定您希望查询分组的属性。 Sphinx's equivalent of GROUP BY
in SQL, though in the past it has been more limited. Sphinx 在 SQL 中相当于GROUP BY
,尽管在过去它受到了更多限制。max_matches
should be set to 1000, unless you've configured it to be a higher value in your Sphinx configuration (via config/thinking_sphinx.yml
). max_matches
应该设置为 1000,除非您在 Sphinx 配置中将其配置为更高的值(通过config/thinking_sphinx.yml
)。 Using the highest possible value ensures the facet results scan as many documents as possible.使用尽可能高的值可确保分面结果扫描尽可能多的文档。 You could also set :limit
to the same value for the specific number of rows being returned here.对于此处返回的特定行数,您还可以将:limit
设置为相同的值。middleware
uses a limited set of Thinking Sphinx middleware classes, to avoid translating results into ActiveRecord objects - instead, you'll get an array of hashes with the Sphinx query data. middleware
使用一组有限的 Thinking Sphinx 中间件类,以避免将结果转换为 ActiveRecord 对象 - 相反,您将获得一个包含 Sphinx 查询数据的哈希数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.