1

I am facing an issue with sorting values in an elastic search query. I am doing a simple search with sort but getting the following error. The query works without a sort parameter.

Elastic search client version: Version 7.6.1(Using this version because I am using opensearch)

search_phase_execution_exception: [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 [subtype] in order to load field data by uninverting the inverted index. Note that this can use significant memory.

Code Sample:

const {Client} = require('@elastic/elasticsearch') // Version 7.6.1
var connectionString = 'https://admin:admin@localhost:9200'
const client = new Client({
    node: connectionString,
    ssl: {
        rejectUnauthorized: false
    }
})
client.info()
    .then(async response => {
        console.log('success', response.statusCode)
        var query = {
            "query": {
                "match": {
                    "revhostname": {
                        "query": "ten.tsacmoc.ac.1dsh.631-651-14-37-c",
                    },
                },
            },
            "sort": [
                {
                    "revhostname": {"order": "asc"},
                    "subtype": {"order": "asc"},
                    "value": {"order": "asc"},
                }
            ],
        };
        var response = await client.search({
            index: 'r7',
            body: query,
        });
        console.log("Search results:", JSON.stringify(response));
    })
    .catch(error => {
        console.error('error', JSON.stringify(error))
    })

Mapping:

{
     "properties": {
       "revhostname": {
         "type" : "keyword"
       },
       "value": {
         "type" : "keyword"
       },
       "subtype": {
         "type" : "keyword"
       },
       "timestamp": {
         "type" : "long"
       },
       "ip": {
         "type" : "ip"
       }
     }
   }

I tried adding fielddata=true in mapping but the issue was not solved. Your help is much appreciated.

Thank you.

2
  • 1
    Have you reindexed all the docs after updating the mapping? Also, is this the actual mapping that you're getting from GET /INDEX/_mapping?
    – ilvar
    Commented May 25, 2022 at 19:23
  • Not reindexed after mapping. I am getting mapping like following GET /INDEX/_mapping "revhostname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } },
    – Dibish
    Commented May 26, 2022 at 3:50

1 Answer 1

2

As you mentioned mapping in a comment, your revhostname field is defined as text and keyword both type of field and Elasticsearch dont allow sorting on text type of field.

If your mapping is still same as you mentioned in comment then you need to use the field name like revhostname.keyword which will resolved issue.

const {Client} = require('@elastic/elasticsearch') // Version 7.6.1
var connectionString = 'https://admin:admin@localhost:9200'
const client = new Client({
    node: connectionString,
    ssl: {
        rejectUnauthorized: false
    }
})
client.info()
    .then(async response => {
        console.log('success', response.statusCode)
        var query = {
            "query": {
                "match": {
                    "revhostname": {
                        "query": "ten.tsacmoc.ac.1dsh.631-651-14-37-c",
                    },
                },
            },
            "sort": [
                {
                    "revhostname.keyword": {"order": "asc"},
                    "subtype.keyword": {"order": "asc"},
                    "value.keyword": {"order": "asc"},
                }
            ],
        };
        var response = await client.search({
            index: 'r7',
            body: query,
        });
        console.log("Search results:", JSON.stringify(response));
    })
    .catch(error => {
        console.error('error', JSON.stringify(error))
    })
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.