简体   繁体   中英

Sunspot Solr facet with multiple filters

In sunspot solr we could group records with similar attributes via facets. But is it possible to do a facet filter from two attributes?

I tried doing this on my search:

facet_search = User.search do
  facet :attribute1, :attribute2
end


facet_search.facet(:attribute1, :attribute2)

With this I keep getting nil values and I am sure that there are records with similar values at attribute1 and attribute2.

Let's say that there are two records that have values at attribute1 as "orange". And those two records have values at attribute2 as "eagles".

Is there a feature in sunspot that I could use to group records based on two columns and how do I do it?

Thanks for the help in advance.

Do you really mean you want to filter? Faceting just brings back the top n number of unique values for that attribute. So if attribute1 contained colors, you would get back orange, red, blue, etc. Any unique color that matched your current search. Faceting alone does not filter your search results.

From your question, I think you are wanting to filter by some value in attribute1 AND some value in attribute2. To do this, your search would look more like:

facet_search = User.search do
  # Filter my results...
  with(:attribute1).equal_to("orange")
  with(:attribute2).equal_to("eagle")
end

You may still include the facet :attribute1 if you wanted to get unique values for attribute1 to display in your UI or something. Just note that declaring :attribute1 as a facet does not impose a filter on the search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM