Search in sources :

Example 1 with SortByField

use of com.b2international.index.query.SortBy.SortByField in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method addSort.

private void addSort(DocumentMapping mapping, SearchSourceBuilder reqSource, SortBy sortBy) {
    for (final SortBy item : getSortFields(sortBy)) {
        if (item instanceof SortByField) {
            SortByField sortByField = (SortByField) item;
            String field = sortByField.getField();
            SortBy.Order order = sortByField.getOrder();
            SortOrder sortOrder = order == SortBy.Order.ASC ? SortOrder.ASC : SortOrder.DESC;
            switch(field) {
                case SortBy.FIELD_SCORE:
                    // XXX: default order for scores is *descending*
                    reqSource.sort(SortBuilders.scoreSort().order(sortOrder));
                    break;
                case SortBy.FIELD_DEFAULT:
                    // Replace special field with default sort field from mapping
                    reqSource.sort(SortBuilders.fieldSort(mapping.getDefaultSortField()).order(sortOrder));
                    break;
                default:
                    // Use field name directly otherwise
                    reqSource.sort(SortBuilders.fieldSort(field).order(sortOrder));
                    break;
            }
        } else if (item instanceof SortByScript) {
            SortByScript sortByScript = (SortByScript) item;
            SortBy.Order order = sortByScript.getOrder();
            SortOrder sortOrder = order == SortBy.Order.ASC ? SortOrder.ASC : SortOrder.DESC;
            reqSource.sort(SortBuilders.scriptSort(sortByScript.toEsScript(mapping), sortByScript.getSortType()).order(sortOrder));
        } else {
            throw new UnsupportedOperationException("Unsupported SortBy implementation " + item);
        }
    }
}
Also used : SortOrder(org.elasticsearch.search.sort.SortOrder) SortByScript(com.b2international.index.query.SortBy.SortByScript) SortByField(com.b2international.index.query.SortBy.SortByField) SortBy(com.b2international.index.query.SortBy) MultiSortBy(com.b2international.index.query.SortBy.MultiSortBy) SortOrder(org.elasticsearch.search.sort.SortOrder)

Aggregations

SortBy (com.b2international.index.query.SortBy)1 MultiSortBy (com.b2international.index.query.SortBy.MultiSortBy)1 SortByField (com.b2international.index.query.SortBy.SortByField)1 SortByScript (com.b2international.index.query.SortBy.SortByScript)1 SortOrder (org.elasticsearch.search.sort.SortOrder)1