# Hibernate search non case sensitive order

**URL:** <https://discourse.hibernate.org/t/hibernate-search-non-case-sensitive-order/1995>\
**Category:** Hibernate Search\
**Created:** [January 9, 2019, 1:12am UTC](https://discourse.hibernate.org/t/hibernate-search-non-case-sensitive-order/1995 "2019-01-09T01:12:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Sun](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@Michael\_Sun](https://discourse.hibernate.org/u/Michael_Sun)\
**Post date:** [January 9, 2019, 1:12am UTC](https://discourse.hibernate.org/t/hibernate-search-non-case-sensitive-order/1995/1 "2019-01-09T01:12:30Z")

</div>

When I try to use Hibernate search(5.9.1.Final) to sort the results, for example, name, the results was shown up with Uppercase first and Lowercase Second, like

![image](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/hibernate/original/1X/2ee9fbbdb9cf6e2baeecf4bf6d82c2f55c219598.png)

which does not make sense.

Code for entity annotation:

```auto
@Column(nullable = false, length = 50)
    @Fields({
            @Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer")),
            @Field(name = "dummyFieldToAddQueryAnalyzer", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer_query"), bridge = @FieldBridge(impl = DummyStringBridge.class)),
            @Field(name = "firstName_sorted", analyze = Analyze.NO, store = Store.NO)
    })
    @SortableField(forField = "firstName_sorted")
    private String firstName;

```

The sort method, I use query builder like

```auto
    return qb.sort().byField(sortBy).desc().createSort();

```

Is there a way to change the sort order so that the lowercase letter has an equivalent sort order value than the uppercase letter.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [January 9, 2019, 7:55am UTC](https://discourse.hibernate.org/t/hibernate-search-non-case-sensitive-order/1995/2 "2019-01-09T07:55:34Z")

</div>

You need to use a normalizer on your “firstName\_sorted” field, and convert the name to lowercase in that normalizer.

Something like this:

```auto
// ==================================================
// HERE, add a normalizer definition, either like this or using programmatic definitions (see the links I give below)
// ==================================================
@NormalizerDef(
		name = "sortNormalizer",
		filters = {
			@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class), // To handle diacritics such as "é"
			@TokenFilterDef(factory = LowerCaseFilterFactory.class)
		}
)
public class MyEntity {

 // ...

    @Column(nullable = false, length = 50)
    @Fields({
            @Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer")),
            @Field(name = "dummyFieldToAddQueryAnalyzer", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer_query"), bridge = @FieldBridge(impl = DummyStringBridge.class)),
            // ===================================================
            // HERE, assign the normalizer and don't forget to enable analysis
            // ===================================================
            @Field(name = "firstName_sorted", analyze = Analyze.YES, normalizer = @Normalizer(name = "sortNormalizer"), store = Store.NO)
    })
    @SortableField(forField = "firstName_sorted")
    private String firstName;

 // ...
}

```

You can find all the necessary information in the documentation:

- [sorts](https://docs.jboss.org/hibernate/search/5.9/reference/en-US/html_single/#query-sorting)
- [normalizers](https://docs.jboss.org/hibernate/search/5.9/reference/en-US/html_single/#section-normalizers)
- [analyzer definition through annotations](https://docs.jboss.org/hibernate/search/5.9/reference/en-US/html_single/#_analyzer_definition_through_annotations) (normalizer definition works the same, just replace the word “analyzer” with “normalizer” everywhere)
- [programmatic analyzer definition](https://docs.jboss.org/hibernate/search/5.9/reference/en-US/html_single/#section-programmatic-analyzer-definition) (normalizer definition works the same, just replace the word “analyzer” with “normalizer” everywhere)

EDIT: You will need to reindex in order for these changes to be taken into account.

---

<div class="post-metadata">

**Author:** ![Michael\_Sun](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@Michael\_Sun](https://discourse.hibernate.org/u/Michael_Sun)\
**Post date:** [January 9, 2019, 11:14pm UTC](https://discourse.hibernate.org/t/hibernate-search-non-case-sensitive-order/1995/3 "2019-01-09T23:14:23Z")

</div>

Thanks you for your help!!!  
It works
