kouign_amann
kouign_amann

Reputation: 23

Sort strings alphabetically with Solr

Context

I have a string field for 'title' that I want to sort alphabetically. I use Solr 4.10.2 for search and sort. Since strFields are case-sensitive by default, I am noticing that Solr is sorting my titles via ASCII sort (capital letters have priority over lowercase letters) and not alphabetically.

Current behavior (asc sort)

Mathematics: Introduction to Algebra

Mathematics: an introduction

Desire behavior (asc sort)

Mathematics: an introduction

Mathematics: Introduction to Algebra

Code in schema.xml

<fieldType name="string_ci" class="solr.TextField" sortMissingLast="true" omitNorms="true">
  <analyzer type="query">
    <tokenizer class="solr.KeywordTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

<field name="string" stored="false" type="string_ci" multiValued="false" indexed="true"/>

Even after restarting Solr, reindexing, the sort is still ASCII sort

Upvotes: 1

Views: 2390

Answers (1)

EricLavault
EricLavault

Reputation: 16035

The field must be lowercased at index time.

Remove the type attribute in your definition so that it applies for both indexing and queries :

<fieldType name="string_ci" class="solr.TextField" sortMissingLast="true" omitNorms="true">
  <analyzer>
    <tokenizer class="solr.KeywordTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

If you want distinct analyzers for each phase, include two <analyzer> definitions distinguished with the type attribute "index" and "query".

Upvotes: 1

Related Questions