Iniamudhan
Iniamudhan

Reputation: 508

Elastic search sorting - Non Nested Elements

I want to use sort in elastic search. I tried nested sort suggested in ES - Nested Sorting Example

But later I found that my index is in the way, where the property is not of type "nested".

Image - ES mapping

So what's the best way to sort inner elements which is a non-nested type.

I'm looking to use sort in MDMGlobalData.City field

Upvotes: 0

Views: 166

Answers (1)

Kaushik J
Kaushik J

Reputation: 1072

Okay I tried to replicate ur issue

PUT my_index
{
  "mappings": {
    "properties": {
      "MDMGlobalData": {
        "properties": {
          "City": {
            "type": "text"
          }
        }
      }
    }
  }
}

Inserted docs with these


POST my_index/_doc
{
  "MDMGlobalData.City":  "Chennai"
  
}
POST my_index/_doc
{
  "MDMGlobalData.City":  "Bangalore"
  
}

trying to sort with this

GET /my_index/_search
{
  "sort": {
    "MDMGlobalData.City": {
      "order": "asc"
    }
  }
}

Got the following error

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [MDMGlobalData.City] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
......

Therefore I updated mapping with fielddata enabled

PUT my_index/_mapping
{
  "properties": {
    "MDMGlobalData": {
      "properties": {
        "City": {
          "type": "text",
          "fielddata": true
        }
      }
    }
  }
}

Now I search

GET /my_index/_search
{
  "sort": {
    "MDMGlobalData.City": {
      "order": "asc"
    }
  }
}

And it works

Upvotes: 1

Related Questions