我正在尝试获取按updated_at字段排序的索引的所有匹配项。

但是,失败并显示以下消息:"{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [updated_at] in order to load fielddata in memory by uninverting the inverted index.
这是我正在使用的映射:

Map::create($this->getModelType(), function (Blueprint $map) {
            $map->integer('id');
            $map->addField('text', 'title');
            $map->date('created_at')->format('yyyy-MM-dd HH:mm:ss');
            $map->date('updated_at')->format('yyyy-MM-dd HH:mm:ss');
            $map->date('deleted_at')->format('yyyy-MM-dd HH:mm:ss');
}));

这是获取按updated_at字段排序的所有数据的代码:
 $titleData = $this->title->search()->sortBy('updated_at', 'DESC');

有任何想法吗?

最佳答案

似乎在ElasticSearch索引中,updated_at被存储为text。分析文本字段,通常ElasticSearch无法按这些字段排序,因为fielddata is disabled on text fields by default

为了克服它,您可以使用mapping类型创建keyword。它可能看起来像这样:

"updated_at": {
  "type": "text", // assuming you originally have it as text
  "fields": {
    "original": "keyword",
    "ignore_above": 64 // You can skip or change it and ES applies default value.
  }
}

然后,您应该使用映射进行排序。您可以像这样访问它:updated_at.original

10-08 02:40