[JAVA API] Sorting on field return error "active fielddata"

Hey,

I'm using the Java API to do some search on an Elastic base. I have differents functions of listing, filtering etc,.. (all working). But, when I want to sort by a field, I have this error :

`Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [language] 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.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [language] 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.`

This error is caused by this part of the code :

public ArrayList<T> listOrdered(String field, int direction) {
		SearchRequest searchRequest = new SearchRequest(EnvSkeleton.ELASTIC_INDEX);
		searchRequest.types(tClazz.getSimpleName());
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

		searchSourceBuilder.size(5000);
		//TODO : correct this line (ask to activate fielddata)
		//searchSourceBuilder.sort(field, getOrder(direction));
		FieldSortBuilder sort = new FieldSortBuilder(field);
		searchSourceBuilder.sort(sort);

		
		searchRequest.source(searchSourceBuilder);
		searchSourceBuilder.query(QueryBuilders.matchAllQuery());
		searchRequest.source(searchSourceBuilder);
		SearchResponse searchResponse = null;
		try {
			searchResponse = client.search(searchRequest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return resultToList(searchResponse.getHits());
	}

You can't sort on language as it's not a keyword data type but a text.
Change your mapping

For those who like me can't modify the mapping, I just added ".keyword" to my field in order to not use th text fild.

Thanks @dadoonet !

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.