说明
text 类型的字段 不支持 排序、聚合
如果对该类型的字段进行排序,会报错
执行下面代码:
GET /toutiao_v1/_search
{
"query":{
"match_all":{
}
},
"sort":[
{
"title":"asc"
}
]
}
报错如下:
{
"error": {
"root_cause": [
{
"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 [title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "toutiao",
"node": "z00SZ73eTciBsgXbxATuQA",
"reason": {
"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 [title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
}
],
"caused_by": {
"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 [title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.",
"caused_by": {
"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 [title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
}
},
"status": 400
}
解决
为字段添加子类型 keyword,如下:
PUT /toutiao_v1
{
"mappings": {
"properties": {
"content": {
"type": "text"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
根据其子类型查询:
GET /toutiao_v1/_search
{
"query":{
"match_all":{
}
},
"sort":[
{
"title.keyword":"asc"
}
]
}
或者:
GET /toutiao_v1/_search
{
"query":{
"match_all":{
}
},
"sort":[
{
"title.keyword":{
"order":"asc"
}
}
]
}