title: Elasticsearch High Level Rest Client Java sorting not working properly
body: <p>I am pretty new with Elasticsearch High Level Rest Client (Java).  </p>
  
  <p>I have a very simple query which lists all records, but it seems sorting is not working properly.  </p>
  
  <p>Some of the fields are text types, so I needed to set fielddata to true.  </p>
  
  <p><strong>Update:</strong></p>
  
  <p><em>Thanks Andrei for the solution.</em></p>
  
  <p>I need to add another field which is an array of objects <code>user.groups</code></p>
  
  <p>I added <code>.keyword</code> to the mapping fields (text) which are sortable.  </p>
  
  <p>But I am getting unexpected results.  </p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>...
  "groups": [
      {"name", "ECPay", ... },
      {"name", "Abangers", ... }
  ]
  ...
  </code></pre>
  
  <p>Based from the output (below), if there are two group items <code>ECPay</code> and <code>Abangers</code>... using <code>user.groups.name.keyword</code> sorting will consider Abangers.</p>
  
  <p>I would like to consider the first element of <code>user.groups</code> array as a basis of the sort.</p>
  
  <p>Based from the example (above), tt will base the sort with <code>ECPay</code> since it is the first element of the array.</p>
  
  <p><em>To understand the problem (updated), let us check the search result (sort) below.</em></p>
  
  <p><strong>Search Result (Output):</strong></p>
  
  <pre><code>{
      "took": 18,
      "timed_out": false,
      "_shards": {
          "total": 5,
          "successful": 5,
          "skipped": 0,
          "failed": 0
      },
      "hits": {
          "total": 74,
          "max_score": null,
          "hits": [
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "5",
                  "_score": null,
                  "_source": {
                      "name": "Ericsson Joseph Sultan Atutuli",
                      "country": "",
                      "uuid": "5",
                      "userId": 5,
                      "email": "ejsultanatutuli@gmail.com",
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "ECPay",
                              "id": 2
                          },
                          {
                              "name": "Abangers",
                              "id": 4
                          }
                      ],
                      "company": ""
                  },
                  "sort": [
                      "Abangers"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "54",
                  "_score": null,
                  "_source": {
                      "name": "Florentina Atutuli",
                      "country": null,
                      "uuid": "54",
                      "userId": 54,
                      "email": "florentina.atutuli@gmail.com",
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "Abangers",
                              "id": 4
                          },
                          {
                              "name": "Test Group",
                              "id": 5
                          }
                      ],
                      "company": null
                  },
                  "sort": [
                      "Abangers"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "37",
                  "_score": null,
                  "_source": {
                      "name": "dsfsdfsdf",
                      "country": null,
                      "uuid": "37",
                      "userId": 37,
                      "email": "asdf@sdf.com",
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "Abangers",
                              "id": 4
                          },
                          {
                              "name": "Test Group",
                              "id": 5
                          }
                      ],
                      "company": null
                  },
                  "sort": [
                      "Abangers"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "3",
                  "_score": null,
                  "_source": {
                      "name": "Erick Atutuli",
                      "country": "Philippines",
                      "email": "erickatutuli@pakyas.com",
                      "userId": 3,
                      "uuid": "d8f4ab43-d33e-4a82-a08b-eb73342a0546",
                      "groups": [
                          {
                              "name": "ECPay",
                              "id": 2
                          },
                          {
                              "name": "Abangers",
                              "id": 4
                          }
                      ],
                      "deletedInd": false,
                      "company": "Hotlegs Incorporated"
                  },
                  "sort": [
                      "Abangers"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "34",
                  "_score": null,
                  "_source": {
                      "name": "Chun-Li",
                      "country": null,
                      "email": "chunli@pakyas.com",
                      "uuid": "34",
                      "userId": 34,
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "Customers AU",
                              "id": 1
                          }
                      ],
                      "company": null
                  },
                  "sort": [
                      "Customers AU"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "57",
                  "_score": null,
                  "_source": {
                      "name": "Eddy Bear",
                      "country": "US",
                      "email": "eddybear@pakyas.com",
                      "uuid": "57",
                      "userId": 57,
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "Customers AU",
                              "id": 1
                          }
                      ],
                      "company": "Jollibee"
                  },
                  "sort": [
                      "Customers AU"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "42",
                  "_score": null,
                  "_source": {
                      "name": "Alfredo Pitik Mingaw",
                      "country": "",
                      "email": "akomykel@gmail.com",
                      "userId": 42,
                      "uuid": "42",
                      "deletedInd": false,
                      "groups": [
                          {
                              "name": "ECPay",
                              "id": 2
                          }
                      ],
                      "company": ""
                  },
                  "sort": [
                      "ECPay"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "20",
                  "_score": null,
                  "_source": {
                      "name": "test",
                      "country": "Åland Islands",
                      "userId": 20,
                      "email": "test102@email.com",
                      "uuid": "20",
                      "groups": [
                          {
                              "name": "ECPay",
                              "id": 2
                          }
                      ],
                      "deletedInd": false,
                      "company": "test"
                  },
                  "sort": [
                      "ECPay"
                  ]
              },
  
              ...
  
          ]
      }
  }
  </code></pre>
  
  <p>Now I added <code>keyword</code> field in the user mapping:</p>
  
  <pre><code>...
  "groups": {
      "properties": {
          "name": {
              "type": "text",
              "fields": {
                  "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                  }
              }
          }
      }
  },
  ...
  </code></pre>
  
  <p><strong>Make text fields sortable, removed fielddata and added keywords</strong>  </p>
  
  <pre><code>http://localhost:9200/acme_users/_mapping/user  
  
  {
    "properties": {
          "name": {
              "type": "text",
              "fields": {
                  "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                  }
              }
          },
          "company": {
              "type": "text",
              "fielddata": true
          },
          "country": {
              "type": "text",
              "fielddata": true
          },
          "groups": {
              "properties": {
                  "name": {
                      "type": "text",
                      "fields": {
                          "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                          }
                      }
                  }
              }
          },
          "email": {
              "type": "text",
              "fields": {
                  "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                  }
              }
          }
    }
  }
  </code></pre>
  
  <p>The query below is generated from my Java application (Spring).<br>
  This is a very simple case wherein I just sort the results into <code>name</code> <code>ascending</code> order.</p>
  
  <p><strong>Query</strong></p>
  
  <p><em>Added <code>.keyword</code> to the sorting field. In the sample below... it will sort using <code>groups.name.keyword</code> which was originally <code>groups.name</code></em>  </p>
  
  <pre><code>http://localhost:9200/acme_users/user/_search  
  
  {
    "from" : 0,
    "size" : 15,
    "query" : {
      "match_all" : {
        "boost" : 1.0
      }
    },
    "sort" : [
      {
        "groups.name.keyword" : {
          "order" : "asc"
        }
      }
    ]
  }
  </code></pre>
  
  <p><strong>Original Problem:</strong></p>
  
  <p>But my problem now is, it seems ES sort does not work properly. The results changes when I change the order from <code>asc</code> to <code>desc</code> (vice versa).<br>
  Though the results changes, it seems that the names (result) is not sorting properly (a-z) or (z-a).<br>
  Seems that it is getting the last part of <code>name</code> and using it as the basis for the sort. I would like to base it on the first character value of <code>name</code>.</p>
  
  <p><strong>Original Search Result:</strong></p>
  
  <pre><code>{
      "took": 4,
      "timed_out": false,
      "_shards": {
          "total": 5,
          "successful": 5,
          "skipped": 0,
          "failed": 0
      },
      "hits": {
          "total": 73,
          "max_score": null,
          "hits": [
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "22",
                  "_score": null,
                  "_source": {
                      "name": "Popeye Partner 01",
                      "country": null,
                      "userId": 22,
                      "email": "popeye_partner_01@kugmo.com",
                      "uuid": "22",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "01"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "24",
                  "_score": null,
                  "_source": {
                      "name": "Dummy User 01",
                      "country": null,
                      "userId": 24,
                      "email": "dummy@dummy.com",
                      "uuid": "24",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "01"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "23",
                  "_score": null,
                  "_source": {
                      "name": "Popeye Partner 02",
                      "country": null,
                      "userId": 23,
                      "email": "popeye_partner_02@kugmo.com",
                      "uuid": "23",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "02"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "25",
                  "_score": null,
                  "_source": {
                      "name": "RT Administrator",
                      "country": null,
                      "userId": 25,
                      "email": "rt_administrator@kugmo.com",
                      "uuid": "25",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "administrator"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "48",
                  "_score": null,
                  "_source": {
                      "name": "John Patrick Abnoy",
                      "country": null,
                      "userId": 48,
                      "email": "patrickabnoy@gmail.com",
                      "uuid": "48",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "abnoy"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "5",
                  "_score": null,
                  "_source": {
                      "name": "Ericsson John Santol Atutuli",
                      "country": "",
                      "uuid": "5",
                      "userId": 5,
                      "email": "ejsantolatutuli@gmail.com",
                      "deletedInd": false,
                      "company": ""
                  },
                  "sort": [
                      "atutuli"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "54",
                  "_score": null,
                  "_source": {
                      "name": "Florentina Atutuli",
                      "country": null,
                      "uuid": "54",
                      "userId": 54,
                      "email": "florentina.atutuli@gmail.com",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "atutuli"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "49",
                  "_score": null,
                  "_source": {
                      "name": "Laarnie Santol Atutuli",
                      "country": "",
                      "email": "lmsantolatutuli@gmail.com",
                      "userId": 49,
                      "uuid": "49",
                      "deletedInd": false,
                      "company": ""
                  },
                  "sort": [
                      "atutuli"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "3",
                  "_score": null,
                  "_source": {
                      "name": "Eric Atutuli",
                      "country": "Philippines",
                      "uuid": "d8f4ab43-d33e-4a82-a08b-eb73342a0546",
                      "userId": 3,
                      "email": "ericatutuli@kugmo.com",
                      "deletedInd": false,
                      "company": "Hotlegs Incorporated"
                  },
                  "sort": [
                      "atutuli"
                  ]
              },
              {
                  "_index": "acme_users",
                  "_type": "user",
                  "_id": "29",
                  "_score": null,
                  "_source": {
                      "name": "Auberto Matulis",
                      "country": null,
                      "userId": 29,
                      "email": "bert.matulis@gmail.com",
                      "uuid": "29",
                      "deletedInd": false,
                      "company": null
                  },
                  "sort": [
                      "auberto"
                  ]
              }
          ]
      }
  }
  </code></pre>
  
  <p>Thanks!</p>