Use a enum field. Those are designed to map arbitrary values to a sort order. The standard example is to apply an order to something that isn’t alpha like: Error, Warning, Info.
https://lucene.apache.org/solr/guide/6_6/working-with-enum-fields.html Enums convert an ordered set into a numeric sort order. wunder Walter Underwood wun...@wunderwood.org http://observer.wunderwood.org/ (my blog) > On Sep 1, 2018, at 2:10 PM, Shawn Heisey <apa...@elyograg.org> wrote: > > On 9/1/2018 12:36 AM, Salvo Bonanno wrote: >> I need to sort a results set in a particolar way... the documents >> looks like this: > > There are two problems with what you want and your setup. > >> I need to sort them by profile_txt value (it's a multiValue field but >> actually it contains just a single value), but since the possible >> values are just 5, I'd like to tokenize them for deciding in wich >> order the should came. > > You can't sort on a multivalued field. The fact that your documents only > contain one value in that field makes no difference. Solr will *refuse* to > sort on the field. > >> the order should follows a simple schema: >> >> 1. profile_txt = "Gold" >> 2. profile_txt = "Super" >> 3. profile_txt = "Smart" >> 4. profile_txt = "Base" >> 5. profile_txt = "Essential" > > This is the other problem. Solr only sorts on two criteria -- numeric, or > alphanumeric. The sort order you want doesn't fit either of these. > > Since you can't change the schema, I'm not entirely sure what you can do. I > think it's probably possible to provide a custom sort plugin for Solr, but I > have absolutely no idea how to do it. > > I don't know if it's possible to devise a function query that would result in > this ordering or not. Clever usage of the function query feature in Solr can > do a lot of things. This function, ascending, MIGHT do it: > > if(exists(profile_txt),if(eq(profile_txt,Gold),5,if(eq(profile_txt,Super),10,if(eq(profile_txt,Smart),15,if(eq(profile_txt,Base),20,if(eq(profile_txt,Essential),25,1000)))))) > > I do not know if the string values need to be wrapped in quotes for that to > work. I'm also not entirely sure that I've placed the parentheses correctly. > > If you do end up being able to change the schema, then you can add a numeric > field and have your indexing program populate that field with a number based > on the value of profile_txt, then you could sort on that field. Solr would > not be able to do this for you unless you wrote a custom UpdateProcessor and > activated it in your solrconfig.xml file. > > Thanks, > Shawn >