zoukankan      html  css  js  c++  java
  • Elasticsearch 6.2.3版本 string 类型字段 排序 报错 Fielddata is disabled on text fields by default

    背景说明

    最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能。

    但是执行结果却报错,报错信息如下:

    {
      "error": {
        "root_cause": [
          {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
          }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
          {
            "shard": 0,
            "index": "alarm",
            "node": "hdLJanxRTbmF52eK6-FFgg",
            "reason": {
              "type": "illegal_argument_exception",
              "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
          }
        ]
      },
      "status": 400
    }

    原因分析

    查询语句如下:

    GET alarm/_search           // index为 alarm
    {
      "query" : {
        "bool" : {
          "must" : [
            {
              "match_phrase" : {
                "state" : {
                  "query" : "confirmed",
                  "slop" : 0,
                  "boost" : 1.0
                }
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      "from": 1,   // 分页,第几页开始
      "size": 5,    // 分页,每页显示多少条
      "sort": {    // 排序,按照  state 字段降序排序
        "state": {
          "order": "desc"
          
        }
      }
    }
    View Code

    测试分析:

    1)去除排序语句,分页查询是OK的,问题出在了排序字段;

    2)按照 integer 类型 或者 date 类型的字段排序都是OK的,但是 string 类型排序报错(示例中的state字段为 string 类型)

    解决方案

    其实在报错信息里已经提供了解决方案:

    需要对 string类型的字段,单独设置加载到内存中,才能排序。

    Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. 

    具体设置操作如下:

    PUT alarm/_mapping/alarmInfoHistory/
    {
        "properties":{
            "state":{
                "type":"text",
                "fielddata":true
            }
        }
    }

    执行结果:

    {
      "acknowledged": true
    }

    再次执行排序查询查询语句,就OK了。

    Good Luck~

    PS:

    如果想按照多个字段排序(按照 statealarmGrade 降序排序),SQL参考如下:

    {
        "query":{
            "bool":{
                "must":[
                    {
                        "match_phrase":{
                            "state":{
                                "query":"confirmed",
                                "slop":0,
                                "boost":1
                            }
                        }
                    }
                ],
                "disable_coord":false,
                "adjust_pure_negative":true,
                "boost":1
            }
        },
        "from":1,
        "size":10,
        "sort":[
            {
                "state":{
                    "order":"desc"
                }
            },
            {
                "alarmGrade":{
                    "order":"desc"
                }
            }
        ]
    }
  • 相关阅读:
    随笔之过账模版
    随笔之转移日记账
    模式窗口的处理方式
    生产领料的问题
    询问对话框,缓存用户设置
    AX2009 连接外部Orcal与SQL区别
    AX2012全新的批处理方式
    AX调用.dll
    AX在query中添加自己的函数
    Java通过代理上传文件到Azure blob
  • 原文地址:https://www.cnblogs.com/miracle-luna/p/11063313.html
Copyright © 2011-2022 走看看