rad11
rad11

Reputation: 1561

Elasticsearch bad sorting with UTF8 values

I turn to you with a request to solve the problem. Implements the project elasticsearch. I have here a sort that does not work as it should. I hope that you will help me to solve this problem.

Here is my mapping:

http://pastebin.com/qgiJ9hH1

Here is the body sent to the elastica:

{
    "body": {
        "from", "0",
        "fize": "25"
        "sort": {
            "spolka_text" {
                "order": "desc"
            }
        }
        "query": {
            "bool": {
                "should": [{
                        "term": {
                            "wprowadzajacy_value": 6001
                        }
                    }]
                "must": [{
                        "term": {
                            "deleted": 0
                        }
                    }, {
                        "range": {
                            "data_wprowadzenia_text" {
                                "Gte": "2012-01-01"
                                "Lte", "2017-02-15"
                            }
                        }
                    }, {
                        "terms": {
                            "proces_platnosci_value" [ "4"]
                        }
                    }]
            }
        }
    }
}

And here's the result:

https://ibin.co/3CSkKOqYujF9.png

And so it should be:

https://ibin.co/3CSkikjWU8Tg.png

I`m using elasticsearch in 5.2 version.

Any ideas why I`v got that response?

Ok I solved problem with help and suggest of @Mysterion:

Here is new mapping:

http://pastebin.com/wWkmjGs7

And new query:

{
    "body": {
        "from", "0",
        "fize": "25"
        "sort": {
            "spolka_text.raw" {
                "order": "desc"
            }
        }
        "query": {
            "bool": {
                "should": [{
                        "term": {
                            "wprowadzajacy_value": 6001
                        }
                    }]
                "must": [{
                        "term": {
                            "deleted": 0
                        }
                    }, {
                        "range": {
                            "data_wprowadzenia_text" {
                                "gte": "2012-01-01"
                                "lte", "2017-02-15"
                            }
                        }
                    }, {
                        "terms": {
                            "proces_platnosci_value" [ "4"]
                        }
                    }]
            }
        }
    }
}

Upvotes: 0

Views: 721

Answers (1)

Mysterion
Mysterion

Reputation: 9320

Generally sorting on a text field is a bad idea . So, I will recommend to disable fielddata () and either add additional field of type keyword which will be used for sorting with combination of the copy_to.

E.g.

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "my_field": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

and use my_field for search, and my_field.keyword for sorting/aggregations

Upvotes: 1

Related Questions