Skip to content
Advertisement

Spring Elasticsearch sorting by field with whitespace

I need to sort my documents by some fields, one sorting – one field. But every time I’m getting wrong sorting. In document I have uid and title. Uid in document looks like ‘AAA-100’ and title could be just a text with spaces. And when i’m trying to sort by those fields i’m getting wrong order. For example when i’m trying to sort by title in ASC order i’m getting this:

aaa b c
aaa c
aaa b d
aaa b h
bbb f b
test

and when order is DESC

test
aaa b h
bbb f b
aaa b d
aaa b c
aaa c

for uid ASC order looks good but DESC is looking like

AAA-461
AAA-460
AAA-449
AAA-450
AAA-454

This is fields from my entity class for my document:

@MultiField(mainField = @Field(type = FieldType.String, analyzer = INDEX_ANALYZER, searchAnalyzer = SEARCH_ANALYZER), otherFields =
@InnerField(suffix = "raw", type = FieldType.String, index = not_analyzed))
private String uid;

@MultiField(mainField = @Field(type = FieldType.String, analyzer = INDEX_ANALYZER, searchAnalyzer = SEARCH_ANALYZER), otherFields = 
@InnerField(suffix = "raw", type = FieldType.String, index = not_analyzed))
private String title;

where INDEX_ANALYZER and SEARCH_ANALYZER:

    "custom_search_analyzer": {
      "filter": [
        "lowercase",
        "asciifolding"
      ],
      "tokenizer": "keyword"
    },
    "custom_index_analyzer": {
      "filter": [
        "lowercase",
        "asciifolding",
        "custom_nGram"
      ],
      "tokenizer": "keyword"
    }

What is wrong? Why sorting is not working properly?

Advertisement

Answer

Just create one field for sort, new mapping and set type “keyword” for field you want to sort. It seem like

.startObject("field's name")
    .field("type", "keyword")
.endObject()

Dont set another type on field sort. Its may be make wrong sort result. I use this way on vietnamese string and still get success.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement