简体   繁体   English

Spring Elasticsearch 按带空格的字段排序

[英]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 和标题。 Uid in document looks like 'AAA-100' and title could be just a text with spaces.文档中的 Uid 看起来像“AAA-100”,标题可能只是带有空格的文本。 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:例如,当我尝试按 ASC 顺序按标题排序时,我得到了这个:

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

and when order is DESC当 order 是 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对于 uid ASC 订单看起来不错,但 DESC 看起来像

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

This is fields from my entity class for my document:这是来自我的实体 class 的字段,用于我的文档:

@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:其中 INDEX_ANALYZER 和 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?为什么排序不能正常工作?

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.我在越南语字符串上使用这种方式并且仍然获得成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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