Question

I have multiple Solr instances with separate schemas.

I need to receive multivalue field in sorted order, e.g. by type: train_station, airport, city_district, and so on:

q=köln&sort=query({!v="type:(airport OR train_station)"}) desc

I would like to see airport type document before train_station type. For now I am always getting train_station type at the top.

How should I write the query?

Was it helpful?

Solution

You are getting train_stations at the top because of the IDF.

A quick hack to fix it would be to use a range query (which has the advantage of having constant scores) and query boosts: q=köln&sort=query({!v="type:([airport TO airport]^3 OR [train_station TO train_station]^2)"}) desc.

This way, documents which have airport in their type field will have a score of 3, documents which have train_station in their type field will have a score of 2 and documents which have airport and train_station in their field type will have a score of 2+3=5 (to a multiplicative constant).

A more elegant (and effective) way of doing this would be to write a custom query parser (or even a function query).

OTHER TIPS

You can sort on a function only if it returns a single value per document. You definitely can't sort on a multiValued field or any field that is tokenized. Seems like you would need a function that returns "airport" if the field contains "airport" (even if it contains "train station") and "train station" if it contains "train station" but not "airport", and then sort on that.

Another option would be to handle this at index time. Add a field called "airport_train_station_sort" that returns 1 if the field contains "airport", 2 if the field contains "train station" but NOT airport, and 3 if it contains neither. Then simply sort on that field.

You cannot solve this problem inside SOLR. Check the documentation, SOLR does not sort multivalued fields. Older versions of SOLR let you try, but the results were undefined and unpredictable.

You either change your schema and put this sort data into single value indexed fields, or you need to make several queries, first for airports, then city districts, then train stations.

To order items within the field itself you have to either index it in order you want, or do post processing. Solr's sort will sort only docs!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top