Solr Facet Multiple Words with Comma Separated Val

2019-02-20 03:11发布

问题:

I'm pulling data into solr from mysql. One of the fields is generated using a group_concat function that results in a comma separated field that lists all the bands for an event. At the time I believe this was the best way to store multiple bands for one event. However, I'm finding that I cannot facet this query against all events.

I've set the band field to string and multivalued to true.

<field name="bands" type="string" indexed="true" stored="true" multiValued="true"/>

The result is as expected where the string is faceted as one long string.

"Pearl Jam,Alice,Screaming Trees,Everclear",1, "Primus,Gaga,Bacon Bits",1, "Roosters,Wings,Drumsticks,Tail Feathers",1,

The biggest problem with this approach is when the field type is string it appears to not be searchable. Seems like I need to create a duplicate field that is type text_general for searching and have one for faceting. Yes?

Is there a way to declare a delimiter for the band field to facet this properly, or is my approach wrong?

回答1:

Tokenizing your field will not solve your facet problem, you will be able to search with a single band name and get results, but the facet will be even worse. The basic rule is to not use any tokenization or text enhance for field used to make facets.

It's good to use a multiValued field, but are actually putting into it a single value with a list of bands, because your query return that list as a single column that is mapped to a single value for the related field in Solr.

You can keep the group_concat output and solve your problem with a simple change to your data-config.xml, telling Solr to split those band names using a separator. Have a look at the RegexTransformer and its splitBy parameter:

splitBy : Used to split a String to obtain multiple values, returns a list of values

If you configure the splitBy with the same separator you're using for group_concat the trick is done, you'll have multiple values and your facet will look good.