获取 illegal_argument_exception", "reason": "默认情况下弹性搜索在文本字段上禁用字段数据

Getting illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default elastic search

当我尝试运行下面来自 Postman

的查询时,我收到了这个查询

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

这是请求

{

    "size": 11,

    "query": {

        "bool": {

            "filter": [

                {

                    "bool": {

                        "must": [

                            {

                                "term": {

                                    "search.doc.TypeId": {

                                        "value": 1,

                                        "boost": 1.0

                                    }

                                }

                            }

                        ],

                        "adjust_negative": true,

                        "boost": 1.0

                    }

                }

            ],

            "adjust_negative": true,

            "boost": 1.0

        }

    },

    "sort": [

        {

            "ID": {

                "order": "desc"

            }

        }

    ]

}

根据错误,objectID 字段似乎是 text 类型。默认情况下,文本字段上的字段数据是禁用的。

所以,根据错误,首先,您需要修改索引映射,使文本字段启用字段数据。修改你的索引映射,如下图

PUT <index-name>/_mapping
{
  "properties": {
    "objectID": {
      "type": "text",
      "fielddata": true
    }
  }
}

现在使用问题中给出的相同搜索查询,以获得所需的结果。