Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch sorting with whitespace

I have elastic search index with name clear and type positionTemplate. It contains field called title. I had created this index/type using river jdbc plugin. I had changed mapping of title field using following api.

 PUT clear/positionTemplate/_mapping
{
    "mappings": {
        "positionTemplate": {
            "properties": {
                "title": {
                    "type": "string", 
                    "index": "not_analyzed"
                }
            }
        }
    }
}

title filed contains following values :

"Java Developer"

"C Developer"

"Ruby Developer"

"Java QA"

"Ruby QA"

"Java Dev"

while running following query sorting is not proper.

{
  "sort" : [{
    "title" : {"order":"desc"}
  }], 
  "fields" : [ "title"]
}

output is :

{
    "_shards": {
        "failed": 0,
        "successful": 5,
        "total": 5
    },
    "hits": {
        "hits": [
            {
                "_id": "3",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Ruby Developer"
                    ]
                },
                "sort": [
                    "ruby"
                ]
            },
            {
                "_id": "5",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Ruby QA"
                    ]
                },
                "sort": [
                    "ruby"
                ]
            },
            {
                "_id": "4",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java QA"
                    ]
                },
                "sort": [
                    "qa"
                ]
            },
            {
                "_id": "15",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java Dev"
                    ]
                },
                "sort": [
                    "java"
                ]
            },
            {
                "_id": "1",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java Developer"
                    ]
                },
                "sort": [
                    "java"
                ]
            },
            {
                "_id": "2",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "C Developer"
                    ]
                },
                "sort": [
                    "developer"
                ]
            }
        ],
        "max_score": null,
        "total": 6
    },
    "timed_out": false,
    "took": 2
}

This is wrong. Can somebody guide me how to correct it ?

like image 420
Silent Warrior Avatar asked Dec 03 '25 01:12

Silent Warrior


1 Answers

you need to re map the field.

Example:

{
  "<field name>": {
    "type" "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}

Then on sort propriety, you will need to specify the "keyword".

Example:

GET <index>/_search
{
  "sort": [
  {
    "<field name>.keyword": {
      "order": "asc"
    }
  }
 ]
}
like image 61
Tiago Mateus Avatar answered Dec 06 '25 17:12

Tiago Mateus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!