elasticsearch text字段排序报错解决
使用elasticsearch 进行排序的时候,我们一般都会排序数字、日期。但是在排序text类型的时候就会出现错误。

GET xytest/sutdent/_search
{
  "sort":[
      {"region": {"order": "asc"}}
    ]
  , "from": 0
  , "size": 2
}

结果如下:
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "xytest",
        "node": "6h-7wPJmQqWGfz6nbgqjjQ",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
    }
  },
  "status": 400
}

主要原因是:"Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."

参考官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

方案一:我们可以用region.keyword进行聚合,排序。用region用来查询。那么我们对我们的查询进行修改
GET xytest/sutdent/_search
{
  "sort":[
      {"region.keyword": {"order": "asc"}}
    ]
  , "from": 0
  , "size": 2
}

修改后查询结果:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": null,
    "hits": [
      {
        "_index": "xytest",
        "_type": "sutdent",
        "_id": "6",
        "_score": null,
        "_source": {
          "sid": "00006",
          "name": "牧琢杭",
          "age": "26",
          "region": "广东省 湛江市 赤坎区",
          "grade": [
            {
              "数学": 80
            },
            {
              "语文": 63
            },
            {
              "英语": 90
            }
          ]
        },
        "sort": [
          "广东省 湛江市 赤坎区"
        ]
      },
      {
        "_index": "xytest",
        "_type": "sutdent",
        "_id": "5",
        "_score": null,
        "_source": {
          "sid": "00005",
          "name": "范旭",
          "age": "30",
          "region": "新疆维吾尔自治区 和田地区 和田县",
          "grade": [
            {
              "数学": 90
            },
            {
              "语文": 69
            },
            {
              "英语": 60
            }
          ]
        },
        "sort": [
          "新疆维吾尔自治区 和田地区 和田县"
        ]
      }
    ]
  }
}

方案二:这是region这个排序字段的fileddata为true。
PUT xytest/_mapping/sutdent
{
  "properties": {
    "region":{
      "type": "text",
      "fielddata": true
    }
  }
}

设置结果:
{
  "acknowledged": true
}

我们再次运行最开始的查询:
GET xytest/sutdent/_search
{
  "sort":[
      {"region": {"order": "asc"}}
    ]
  , "from": 0
  , "size": 2
}

返回结果:
{
  "took": 130,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": null,
    "hits": [
      {
        "_index": "xytest",
        "_type": "sutdent",
        "_id": "6",
        "_score": null,
        "_source": {
          "sid": "00006",
          "name": "牧琢杭",
          "age": "26",
          "region": "广东省 湛江市 赤坎区",
          "grade": [
            {
              "数学": 80
            },
            {
              "语文": 63
            },
            {
              "英语": 90
            }
          ]
        },
        "sort": [
          "东"
        ]
      },
      {
        "_index": "xytest",
        "_type": "sutdent",
        "_id": "4",
        "_score": null,
        "_source": {
          "sid": "00004",
          "name": "符龙",
          "age": "37",
          "region": "河北省 保定市 清苑县",
          "grade": [
            {
              "数学": 80
            },
            {
              "语文": 79
            },
            {
              "英语": 69
            }
          ]
        },
        "sort": [
          "保"
        ]
      }
    ]
  }
}

ES使用text类型字段排序报错的更多相关文章

  1. Elasticsearch 6.2.3版本 string 类型字段 排序 报错 Fielddata is disabled on text fields by default

    背景说明 最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能. 但是执行结果却报错,报错信息如下: { "error": { " ...

  2. ElasticSearch 6.2 Mapping参数说明及text类型字段聚合查询配置

    背景: 由于本人使用的是6.0以上的版本es,在使用发现很多中文博客对于mapping参数的说明已过时.ES6.0以后有很多参数变化. 现我根据官网总结mapping最新的参数,希望能对大家有用处. ...

  3. Sql 中text类型字段判断是否为空

    用 len关键字,字段=''会报错:数据类型 text 和 varchar 在 equal to 运算符中不兼容. 正确方法: 1. 字段 is null 2. datalength(字段)=0 注: ...

  4. [转]sql中判断text类型字段是否为空

    用 字段=''会报错:数据类型 text 和 varchar 在 equal to 运算符中不兼容. 正确方法: 1. 字段 is null 2. datalength(字段)=0 注:SQL中的DA ...

  5. MSSQL数据库中Text类型字段在PHP中被截断之解 (转)

    在PHP中使用了MSSQL数据库,恰巧数据库中又使用了Text类型字段,于是问题产生了.每次从数据库中查询得到的数据总是被莫名的截断,一开始是以为我使用的PHP框架中对字符串的长度有所限制,后来发现这 ...

  6. MySQL 表与字段编码格式报错

    MySQL 表与字段编码格式报错 一.数据库,表,字段编码格式都为latin1(iso-8859-1) .当数据保存到数据库后,中文显示乱码. 解决办法: 1.在访问数据库连接串中添加编码格式: &l ...

  7. 单元测试时候使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且应采用一个TestContext类型的参数报错的解决办法

    using Microsoft.VisualStudio.TestTools.UnitTesting; 如果该DLL应用的是 C:\Program Files\Microsoft Visual Stu ...

  8. DB2读取CLOB字段-was报错:操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null

    DB2读取CLOB字段-was报错:操作无效:已关闭 Lob. ERRORCODE=-4470, SQLSTATE=null 解决方法,在WAS中要用的数据源里面配置连个定制属性: progressi ...

  9. 使用like查询text类型字段

    使用like查询text类型字段 public bool Exists(GetReadType GRT, ClientMessageGetRead TypeID, string MessageID, ...

随机推荐

  1. 【python学习】字符串相关

    # -*- coding: utf-8 -*- # ========================================================================== ...

  2. 有关sublime的一些使用

    习惯了vs的快捷键,用sublime的时候始终感觉不太跟手. 点击 “首选项”→“按键绑定-默认” 在新的界面中就可以更改你想要的快捷键了. 1.自动格式化对齐: 查找 "command&q ...

  3. How to correctly use preventDefault(), stopPropagation(), or return false; on events

    How to correctly use preventDefault(), stopPropagation(), or return false; on events I’m sure this h ...

  4. [论文理解] Attentional Pooling for Action Recognition

    Attentional Pooling for Action Recognition 简介 这是一篇NIPS的文章,文章亮点是对池化进行矩阵表示,使用二阶池的矩阵表示,并将权重矩阵进行低秩分解,从而使 ...

  5. P5436 【XR-2】缘分

    P5436 [XR-2]缘分 题解 很显然给出一个n,要想使缘分最大,一定要选 n 和 n-1 对吧 但是这里有一个特盘,当 n=1 时,缘分应该为1 而不是0 代码 #include<bits ...

  6. iso-----genisoimage/md5sum命令用法

    命令安装 直接yum安装即可 yum install genisoimage -y 功能说明 可将指定的目录与文件做成ISO 9660格式的映像文件,以供刻录光盘 语法 genisoimage -U ...

  7. SAEJ1757-1-2015(一)

    SURFACE VEHICLE STANDARD           表面车辆标准 Standard Metrology for Vehicular Displays             车载显示 ...

  8. sql四种连接方式

    1.内连接(inner  join)只有两个表相匹配的行才能在结果集中显示出来2.左连接(left  join)以左表为主,左表所有的数据都会在结果集中出现,右表根据左表对应的数据显示,与左表匹配的数 ...

  9. fixture作用范围

    ixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture(scope="f ...

  10. 基于Opencv的自适应中值滤波函数selfAdaptiveMedianBlur()

    7.3.3 自适应滤波器 自适应中值滤波器 对于7.3.2节所讨论的中值滤波器,只要脉冲噪声的空间密度不大,性能还是可以的(根据经验需Pa和Pb小于0.2).本节将证明,自适应中值滤波器可以处理更大概 ...