You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Salvo Bonanno <sa...@gmail.com> on 2018/09/01 06:36:56 UTC

Sorting by custom order

Hello

I need to sort a results set in a particolar way... the documents
looks like this:

{
    "customer_id":28998,
    "name_txt":["Equal Corp"],
    "address_txt":["Austin Ring Center"],
    "municipality_txt":["Austin"],
    "province_txt":["Austin"],
    "region_txt":["TX"],
    "profile_txt":["Base"],
    "visibility_weight_txt":["2"]
},
{
    "customer_id":28997,
    "name_txt":["Mustard Ltd"],
    "address_txt":["Telegraph Road"],
    "municipality_txt":["London"],
    "province_txt":["London"],
    "region_txt":["UK"],
    "profile_txt":["Gold"],
    "visibility_weight_txt":["2"]
}

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.

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"

Then an additional sort by visibility_weight_txt should be done

Is it possible to do this someway using FunctionQueries? Unfortunally
I can't modify the schema.

Thanks for reading

Re: Sorting by custom order

Posted by Walter Underwood <wu...@wunderwood.org>.
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
wunder@wunderwood.org
http://observer.wunderwood.org/  (my blog)

> On Sep 1, 2018, at 2:10 PM, Shawn Heisey <ap...@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
> 


Re: Sorting by custom order

Posted by Shawn Heisey <ap...@elyograg.org>.
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