Steemit首发:如何实现Solr中的Multi-select facting

in cn •  7 years ago 

这种多选的faceting是什么? What is multi-select facting?

Multi-select faceting means that users can see and select multiple facet items (facet values) for particular facets. Thanks to Solr's filter tagging and excluding features to support this type of faceting.

说白了就是可以在同一个facet中可以选择多个值。

举个电脑超市的例子Online PC shop example

例如,这里有两个facet,一个是电脑的内存(memory),另一个是电脑品牌(brand)。用户在搜索的时候可能会需要进行多选来筛选搜索结果。比如:可能用户想要搜索内存4GB-8GB,Lenovo和HP这两个品牌的电脑。
For example, we have 2 facets: memory, and brand. We want to support multi-select faceting because while searching, users may want to choose a range of memory size e.g. 4GB-8GB and a list of interested brands, e.g. Lenovo and HP.

用户在输入初始关键字,比如:"Home desktop PC"后,结果如下:
From the initial search of keyword "Home desktop PC", the result looks like:

------memory------     ------brand------      
[  ]  2GB (30)             [  ] Acer (13)
[  ]  4GB (50)             [  ] HP (20)
[  ]  8GB (25)             [  ] Lenovo (33)
[  ]  16GB (10)            [  ] Toshiba (20)

这是用户可能想要通过选择内存为4GB和8GB来筛选搜索结果。
Then user may want to narrow down the search results by selecting 4GB and 8GB in memory facet, also HP and Lenovo in the brand facet.

选择内存大小后搜索结果变为如下:
Ideally, after selecting 4GB and 8GB, users expect to see:

------memory------     ------brand------      
[ ]  2GB (30)             [  ] Acer (5)
[x]  4GB (50)             [  ] HP (10)
[x]  8GB (25)             [  ] Lenovo (13)
[ ]  16GB (10)            [  ] Toshiba (10)

你可能会注意到brand对应的各个值后面的数字也发生了变化,其实这是很正常的。因为用户这时想要看的只是内存为4GB/8GB的电脑。
You may notice that the numbers after each brand facet item is also changed. That is just because the search results have been narrowed down after first select of "4GB" and "8GB" in memory facet.

Solr中的实现Solr query to support multi-select faceting

Solr官方文档中提到的tagging和excluding就可以实现这样的功能。
According to solr's documentation, tagging and excluding filters can be used to perform the multi-select faceting.

The solr query is:

q=Home%20desktop%20PC&facet=on&facet.field={!ex=tagmem}&fq={!tag=tagmem} memory:%28%224GB%208GB%22%29&facet.field={!ex=tagbrand}&fq={!tag=tagbrand}brand:%28%22Lenobo%20HP%22%29

给大家解码后,更容易读的版本
Human readable version (should not send it to Solr in your application):

q=Home desktop PC&facet=on&facet.field={!ex=tagmem}&fq={!tag=tagmem}memory:("4GB" "8GB")&facet.field={!ex=tagbrand}&fq={!tag=tagbrand}brand:("Lenobo" "HP")

使用solrj在程序中的实现Solrj implementation

For facet:

SolrQuery query = new SolrQuery();
......
query.addFacetField("{!ex=tagmem}memory";
......
query.addFacetField("{!ex=tagbrand}brand";

For facet items:

query.addFilterQuery("{!tag=tagbrand}brand:"+YOUR_BRAND_FACET_VALUES_FROM_FRONTEND)



Originally posted on https://steemit.com. Thank you for reading my post, feel free to FOLLOW and Upvote @yuxi, which will motivate me to create more quality posts.

https://steemit.com 首发。非常感谢阅读,欢迎FOLLOW和Upvote @yuxi 激励我创作更多更好的内容。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!