Hi,
I want to do custom sorting for some of the fields. I am having some issues with it.
In my elasticsearch plugin, I had registered a CustomSortFieldBuilder, which builds a CustomSortFieldComparatorSource. CustomSortFieldComparatorSource is in charge of instantiating CustomSortFieldComparator that does the sorting based on our needs.
The elasticsearch plugin registers the builder this way:
@Override
public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return Collections.singletonList(
new NamedWriteableRegistry.Entry(SortBuilder.class, CustomSortFieldBuilder.NAME, CustomSortFieldBuilder::new)
);
}
In the client code, my requests then uses the customSortFieldBuilder.
new SearchRequest(TEST_OBJECT_INDEX).source().query(new MatchAllQueryBuilder()).sort(new CustomSortFieldBuilder("name.keyword"));
Requests are being sent fine. The problem occurs when the results in the response object are merged and sorted from multiple nodes.
During marshalling and unmarshalling of the response object, we are losing information about the comparator source.
org.elasticsearch.common.lucene.Lucene.writeTopDocs(StreamOutput out, TopDocs topDocs)
if (sortField.getComparatorSource() != null) {
IndexFieldData.XFieldComparatorSource comparatorSource = (IndexFieldData.XFieldComparatorSource) sortField.getComparatorSource();
writeSortType(out, comparatorSource.reducedType());
writeMissingValue(out, comparatorSource.missingValue(sortField.getReverse()));
}
Notice that we are not even writing to stream which comparatorsource to use when marshalling the response object. So, on unmarshalling of the response object, Elasticsearch no longer knows to use the customComparator. And the documents are merged and sorted using one of the default comparators.
Any idea what I am missing here?