首页 > 解决方案 > 带有排序关键字的 Elasticsearch 查询参数

问题描述

我一直在使用 ES 5.4.0 版本并创建了索引和文档(学生数据)。当我根据年龄进行搜索时,_search 端点正在返回值,并且输出符合预期。对 name desc 选项做了同样的事情。我得到以下异常

"error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] 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": "students",
        "node": "MESiRCvSSgqMNWEVwMevMg",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] 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."

可能是什么原因。根据日志,我必须添加fileddata = true。这背后的任何原因。

http://localhost:9200/students/data/_search?q=montana&sort=age:desc - 工作正常。

http://localhost:9200/students/data/_search?q=montana&sort=name:desc - 不工作

http://localhost:9200/students/data/_search?q=montana&sort=name:desc&fielddata=true - 添加字段数据 - 不工作

// http://localhost:9200/students ?

{
  "students": {
    "aliases": {

    },
    "mappings": {
      "data": {
        "properties": {
          "age": {
            "type": "long"
          },
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "company": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "email": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "gender": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "phone": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "street": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1530174012103",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "PrtajDL0SwS-tTX7Gm_YQw",
        "version": {
          "created": "5040099"
        },
        "provided_name": "students"
      }
    }
  }
}

标签: hadoopelasticsearchamazon-elastic-beanstalk

解决方案


您不能对text字段进行排序(可以,但结果没有意义),但是,您可以对keyword字段进行排序,因此以下查询将起作用:

http://localhost:9200/students/data/_search?q=montana&sort=name.keyword:desc
                                                                   ^
                                                                   |
                                                                add this

推荐阅读