Search code examples
solrlucenesolr-multy-valued-fields

Sorting on multivalued field in Solr


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?


Solution

  • 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.