elasticsearch text类型排序

一、text类型字段排序问题

text字段排序会报以下错误

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

翻译如下:
text类型字段没有针对需要每个文档字段数据的操作进行优化,比如聚合和排序,
所以这些操作在默认情况下是禁用的。请使用keyword类型字段代替。
或者,设置text类型字段的属性fielddata=true,以便通过取消倒排索引来加载字段数据。注意,这会占用大量内存

二、解决方案

1、text类型字段通过设置多字段特性(fields)类型为keyword

// 创建索引
PUT /my_test_unnested_2
{
  "mappings": {
    "properties": {
      "age":{"type": "text","fields": {"keyword":{"type":"keyword"}}},
      "name":{"type": "text","fields": {"keyword":{"type":"keyword"}}},
      "birth":{"type": "text","fields": {"keyword":{"type":"keyword"}}}
    }
  }
}
// 导入数据
POST /my_test_unnested_2/_bulk
{"index":{"_id":"1"}}
{"age":"15","name":"zhangsan","birth":"2021-09-03"}
{"index":{"_id":"2"}}
{"age":"12","name":"zhangsan11","birth":"2022-11-03"}
{"index":{"_id":"3"}}
{"age":"18","name":"zhangsan1","birth":"2021-11-03"}
// 查询DSL
GET /my_test_unnested_2/_search
{
  "sort": [
    {
      "birth.keyword": {
        "order": "asc"
      }
    }
  ]
}
// 查询结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my_test_unnested_2",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "age" : "15",
          "name" : "zhangsan",
          "birth" : "2021-09-03"
        },
        "sort" : [
          "2021-09-03"
        ]
      },
      {
        "_index" : "my_test_unnested_2",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "age" : "18",
          "name" : "zhangsan1",
          "birth" : "2021-11-03"
        },
        "sort" : [
          "2021-11-03"
        ]
      },
      {
        "_index" : "my_test_unnested_2",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "age" : "12",
          "name" : "zhangsan11",
          "birth" : "2022-11-03"
        },
        "sort" : [
          "2022-11-03"
        ]
      }
    ]
  }
}

2、text类型字段设置属性fielddata=true

// 创建索引
PUT /my_test_unnested_1
{
  "mappings": {
    "properties": {
      "age":{"type": "text","fielddata": true},
      "name":{"type": "text","fielddata": true},
      "birth":{"type": "text","fielddata": true}
    }
  }
}
// 复制数据
POST _reindex
{
  "source": {
    "index":"my_test_unnested_2"
  },
  "dest": {
    "index": "my_test_unnested_1"
  }
}
// 查询DSL
GET /my_test_unnested_1/_search
{
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}
// 查询结果
{
  "took" : 462,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my_test_unnested_1",
        "_type" : "_doc",
        "_id" : "LoLCK4YBuFZg-3BRkmgI",
        "_score" : null,
        "_source" : {
          "age" : "12",
          "name" : "zhangsan11",
          "birth" : "2022-11-03"
        },
        "sort" : [
          "12"
        ]
      },
      {
        "_index" : "my_test_unnested_1",
        "_type" : "_doc",
        "_id" : "LILCK4YBuFZg-3BRDGij",
        "_score" : null,
        "_source" : {
          "age" : "15",
          "name" : "zhangsan",
          "birth" : "2021-09-03"
        },
        "sort" : [
          "15"
        ]
      },
      {
        "_index" : "my_test_unnested_1",
        "_type" : "_doc",
        "_id" : "LYLCK4YBuFZg-3BRVmhX",
        "_score" : null,
        "_source" : {
          "age" : "18",
          "name" : "zhangsan1",
          "birth" : "2021-11-03"
        },
        "sort" : [
          "18"
        ]
      }
    ]
  }
}

三、nested字段的排序DSL

GET /my_test_sort/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "ms_visit_record.age.keyword": {
        "order": "desc",
        // 区别去非nested字段的排序
        "nested_path": "ms_visit_record"
      }
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值