I have recently encountered this issue but in my case, I faced this issue when I was performing the Sorting.
An Excerpt from the link
Most fields are indexed by default, which makes them searchable. Sorting, aggregations, and accessing field values in scripts, however, requires a different access pattern from search.
Fielddata is disabled on text fields by default.
we can solve this problem with two approaches
1. Updating the mappings by enabling fielddata
on text
fields as "fielddata": true
PUT your_index/_mapping
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
2. Appending .keyword
to the field as your_field.keyword
for aggregations, sorting.
In the previous approach, we have to perform reindexing which is sometimes a cumbersome process for large indexes. For such cases, the below solution would be an easier solution to implement.
ES Query:
GET /your_index/_search
{
"query": {
"match_all": {}
},
"sort":[
{
"your_field.keyword":{
"order":"asc"
}
}
]
}
Java Code
SearchRequest searchRequest = new SearchRequest("your_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//Appending the .keyword to the sorting field
FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("your_field"+".keyword");
fieldSortBuilder.order(SortOrder.ASC);
searchSourceBuilder.sort(fieldSortBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);