弹性搜索排序错误 - search_phase_execution_exception

Elastic search sort error - search_phase_execution_exception

我在弹性搜索查询中遇到排序值的问题。我正在使用排序进行简单搜索,但出现以下错误。查询在没有排序参数的情况下工作。

Elasticsearch客户端版本:Version 7.6.1(因为我用的是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.

代码示例:

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))
    })

映射:

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

我尝试在映射中添加fielddata=true,但问题没有解决。非常感谢您的帮助。

谢谢。

正如您在评论中提到的映射,您的 revhostname 字段被定义为 textkeyword 两种类型的字段和 Elasticsearch 不允许按 text 类型排序领域。

如果您的映射仍然与您在评论中提到的相同,那么您需要使用像 revhostname.keyword 这样的字段名称,这将解决问题。

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))
    })