Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map on existing indices for sorting?

I am looking for sorting in Elasticsearch. Before I insert the indices, I execute following

PUT product
{
  "mappings": {
    "properties": {
      "manufacturer": {
        "type": "keyword"
      }
    }
  }
}

After I can make a sort with manufacturer. But now I want to sort by brand.

GET product/_search
{
  "size": 5, 
  "query": {
    "match": {
      "manufacturer": "abc"
    }
  },
  "sort": [
    {
      "brand": {
        "order": "desc"
      }
    }
  ]
}

It gives me following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"type" : "illegal_argument_exception",
        "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [brand] in order to load field data by uninverting the inverted index. Note that this can use significant memory."

Is it possible to make a mapping on already existing indices ?

>Solution :

You can use below command to check current index mapping:

GET product

If your current index mapping is look like below then you can use brand.keyword.

{
  "mappings": {
    "properties": {
      "manufacturer": {
        "type": "keyword"
      },
      "brand": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

You can use below query to sort on brand field if your current index mapping is same as above:

GET product/_search
{
  "size": 5, 
  "query": {
    "match": {
      "manufacturer": "abc"
    }
  },
  "sort": [
    {
      "brand.keyword": {
        "order": "desc"
      }
    }
  ]
}

Is it possible to make a mapping on already existing indices ?

Yes, you can update mapping of existing index using below command but you need to reindex data if you are updating mapping of existing field.

PUT product/_mapping
{
  "properties": {
    "brand": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading