简体   繁体   中英

Solrj: how to sort by a function

I want to be able to sort my collection by a field called "severity" where its possible values are:

  • "fair"
  • "critical"
  • "warning"

Sorting by ascending severity should bring fair, warning, then critical. Yet, the sort is done alphabetically .

How can I implement a custom sort through Solr's client for Java, Solrj?

how about creating a new field with int data type and have it as 0 for fair, 1 for warning and 2 for critical. Then you can sort on this field which is an int

Also, have a look at the documentation about sorting http://wiki.apache.org/solr/CommonQueryParameters#sort

Sorting can be done on the "score" of the document, or on any multiValued="false" indexed="true" field provided that field is either non-tokenized (ie: has no Analyzer) or uses an Analyzer that only produces a single Term (ie: uses the KeywordTokenizer)

The common situation for sorting on a field that you do want to be tokenized for searching is to use a to clone your field. Sort on one, search on the other.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM