I know multivalued field sorting is not supported in Solr . But Is there any way we can sort multivalued field in Solr. I have two documents with field custom_code and values are as below,
Doc 1 : 11, 78, 45, 22
Doc 2 : 56, 74, 62, 10
When I sort it in ascending order the order should be ,
Doc 2 : 56, 74, 62, 10
Doc 1 : 11, 78, 45, 22
Here Doc 2 will come first because it has smallest element 10 (which is greater that 11 of doc 1).
How can we achieve this in Solr. What is the easiest way?
Create a copyfield to copy the content of multivalued data into a sorted concatenated single value without commas and use it for sorting.
For Ex :
Doc 1 :
multiValuedData : 11, 78, 45, 22
sortedConcatenatedSingleValue : 11224578
Doc 2 :
multiValuedData : 56, 74, 62, 10
sortedConcatenatedSingleValue : 10566274
If you cannot create this singlvalue field at the time of generating the data from source then you can use a script transformer (https://wiki.apache.org/solr/DataImportHandler#ScriptTransformer) during the index time to create this sortedConcatenatedSingleValue field using a javascript function.
You can get out of box in 5.3 - https://issues.apache.org/jira/browse/SOLR-2522
A good description is here - https://lucidworks.com/blog/2015/09/10/minmax-on-multivalued-field/
if this issue was implemented you could just use that, as in the description of the ticket. But it is not as of yet.
So, one other thing you can do is to index an additional field
min_code
, non multivalued and put the minimum value ofcustom_code
in there. You can do that in the client side or in Solr in a UpdateRequestProcessor. Then you just sort onmin_code