Home > OS >  Enable ascending and descending sorting of numbers that are of the keyword type (Elasticsearch)
Enable ascending and descending sorting of numbers that are of the keyword type (Elasticsearch)

Time:05-06

My task is to sort documents in ascending and descending order, but 'number' must remain of the keyword type. I read other posts on a similar topic, and tried to add an 'number' of type integer, but I didn't succeed and the index crashes. I am attaching the current configuration in the esMapping.js file.

Is there a way to fix this esMapping.js file so that ascending and descending sorting works?

"settings": {
    "analysis": {
        "analyzer": {
            "document_number_analyzer": {
                "type": "custom",
                "tokenizer": "document_number_tokenizer"
            }
        },
        "tokenizer": {
            "document_number_tokenizer": {
                "type": "pattern",
                "pattern": "-0*([1-9][0-9]*)\/",
                "group": 1
            }
        },
    }
}

Mapping:

"number": {
            "type": "keyword",
            "copy_to": [
                "_summary"
            ],
            "fields": {
                "sequenceNumber": {
                    "type": "text",
                    "analyzer": "document_number_analyzer"
                }
            }
        }

enter image description here

CodePudding user response:

Your mapping needs to be like this:

    "number": {
        "type": "keyword",
        "copy_to": [
            "_summary"
        ],
        "fields": {
            "sequenceNumber": {
                "type": "integer"
            }
        }
    }

And then you can simply sort by number.sequenceNumber

  • Related