java - Elastic search sort field containing special characters numbers and alpahbets -
i created case insensitive analyzer
put /dhruv3 { "settings": { "index": { "analysis": { "analyzer": { "analyzer_keyword": { "tokenizer": "keyword", "filter": [ "lowercase", "asciifolding" ] } } } } }, "mappings": { "test": { "properties": { "about": { "type": "string", "analyzer": "analyzer_keyword" }, "firsname": { "type": "string" } } } } }
and used in mapping. field supposed contain aplha numerc , special characters.then inserted values about
field as
1234, `pal, pal, ~pal
. besides searching need result sorted. searching working when try sort them as
get dhruv/test/_search { "sort": [ { "about": { "order": "asc" } } ] }
i results in field
1234,`pal,pal,~pal
. expect them first special characters, numbers , alphabets.
i did home work , came know because of ascii values. searched internet , tried asciifolding
. didn't work out. know there solution can't figure out. please guide me
you're right in sorting behavior seeing due ascii value of special characters bigger ascii value of digits. precise, looking @ ascii table, have following values:
1
has ascii value 49- ` has ascii value 96
p
has ascii value 112~
has ascii value 126
the asciifolding
token filter transforms characters , digits not in ascii table (i.e. first 127 characters) ascii equivalent, if such 1 exists (e.g. é
, è
, ë
, ê
transformed e
). since characters above in ascii table, not you're looking for.
if want special characters come first in search there several ways.
one way achieve negate ascii value come before ascii 0 character , use script sorting:
{ "sort": [ { "_script": { "script": "return doc['about'].value.chars[0].isletterordigit() ? 1 : -1", "type": "number", "order": "asc" } } ] }
Comments
Post a Comment