This is my Entity:
public class RecentTransactionBo {
    @JsonProperty("timestamp")
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time)
    @Id
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    private List<String> transactionId;
}
And I'm doing Sort operation on it:
Iterable<RecentTransactionBo> recentTransactionBoIterable = recentTransactionDao.findAll(Sort.by(Sort.Direction.DESC, "creationDate"));
I'm getting the following error:
2020-09-08 02:42:29.116 ERROR 7116 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]];] with root cause
    org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]
My DAO looks like this.
@Repository
public interface RecentTransactionDao extends ElasticsearchRepository<RecentTransactionBo, Date> {
}
How do I resolve this and what's the mistake I'm doing? I'm using version 7.8.1
To correctly resolve this you should create a correct mapping and set required fields to be of keyword type.
Other solution is to run the command:
PUT <your_index_name>/_mapping
{
  "properties": {
    "creationDate": { 
      "type":     "text",
      "fielddata": true
    },
    "host.name": { 
      "type":     "text",
      "fielddata": true
    }
  }
}
Above I am setting the fielddata properties on 2 fields - creationDate and host.name as an example how to set it on multiple fields in one call. Elastic will give you only one name at a time, so you may need to run the command multiple times, unless you know all the problematic fields beforehand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With