我在彈性搜索查詢中對值進行排序時遇到問題。我正在使用 sort 進行簡單搜索,但出現以下錯誤。該查詢在沒有排序引數的情況下作業。
彈性搜索客戶端版本:7.6.1版本(因為我用的是opensearch所以用這個版本)
search_phase_execution_exception:[illegal_argument_exception] 原因:文本欄位未針對需要按檔案欄位資料的操作(如聚合和排序)進行優化,因此默認情況下禁用這些操作。請改用關鍵字欄位。或者,在 [subtype] 上設定 fielddata=true 以通過反轉倒排索引來加載欄位資料。請注意,這可能會占用大量記憶體。
代碼示例:
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 但問題沒有解決。非常感謝您的幫助。
謝謝你。
uj5u.com熱心網友回復:
正如您在評論中提到的映射,您的revhostname欄位被定義為欄位型別text和keywordElasticsearch 都不允許對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))
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481744.html
