elasticsearch text类型排序

Posted 为什么要做囚徒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"
      
    
  ]

以上是关于elasticsearch text类型排序的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成ElasticSearch,实现模糊查询,批量CRUD,排序,分页,高亮

Elasticsearch 字段类型

〈二〉ElasticSearch的认识:索引类型文档

探索Elasticsearch集群API

ElasticSearch相关概念总结

Elasticsearch:Dynamic field mapping