Search in sources :

Example 1 with SortType

use of com.yelp.nrtsearch.server.grpc.SortType in project nrtsearch by Yelp.

the class SortParser method parseSort.

/**
 * Decodes a list of request {@link SortType} into the corresponding {@link Sort}.
 *
 * @param fields list of {@link SortType} from grpc request
 * @param sortFieldNames mutable list which will have all sort field names added in sort order,
 *     may be null
 * @param queryFields collection of all possible fields which may be used to sort
 */
public static Sort parseSort(List<SortType> fields, List<String> sortFieldNames, Map<String, FieldDef> queryFields) throws SearchHandler.SearchHandlerException {
    List<SortField> sortFields = new ArrayList<>();
    for (SortType sub : fields) {
        String fieldName = sub.getFieldName();
        SortField sf;
        if (sortFieldNames != null) {
            sortFieldNames.add(fieldName);
        }
        if (fieldName.equals(DOCID)) {
            if (!sub.getReverse()) {
                sf = SortField.FIELD_DOC;
            } else {
                sf = new SortField(null, SortField.Type.DOC, true);
            }
        } else if (fieldName.equals(SCORE)) {
            if (!sub.getReverse()) {
                sf = SortField.FIELD_SCORE;
            } else {
                sf = new SortField(null, SortField.Type.SCORE, true);
            }
        } else {
            FieldDef fd = queryFields.get(fieldName);
            if (fd == null) {
                throw new SearchHandler.SearchHandlerException(String.format("field: %s was not registered and was not specified as a virtualField", fieldName));
            }
            if (!(fd instanceof Sortable)) {
                throw new IllegalArgumentException(String.format("field: %s does not support sorting", fieldName));
            }
            sf = ((Sortable) fd).getSortField(sub);
        }
        sortFields.add(sf);
    }
    return new Sort(sortFields.toArray(new SortField[0]));
}
Also used : SortType(com.yelp.nrtsearch.server.grpc.SortType) SearchHandler(com.yelp.nrtsearch.server.luceneserver.SearchHandler) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) Sortable(com.yelp.nrtsearch.server.luceneserver.field.properties.Sortable) ArrayList(java.util.ArrayList) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Example 2 with SortType

use of com.yelp.nrtsearch.server.grpc.SortType in project nrtsearch by Yelp.

the class SettingsHandler method parseSort.

/**
 * Decodes a list of SortType into the corresponding Sort.
 */
static Sort parseSort(IndexState state, List<SortType> fields, List<String> sortFieldNames, Map<String, FieldDef> dynamicFields) throws SettingsHandlerException {
    List<SortField> sortFields = new ArrayList<>();
    for (SortType _sub : fields) {
        String fieldName = _sub.getFieldName();
        SortField sf;
        if (sortFieldNames != null) {
            sortFieldNames.add(fieldName);
        }
        if (fieldName.equals("docid")) {
            sf = SortField.FIELD_DOC;
        } else if (fieldName.equals("score")) {
            sf = SortField.FIELD_SCORE;
        } else {
            FieldDef fd;
            if (dynamicFields != null) {
                fd = dynamicFields.get(fieldName);
            } else {
                fd = null;
            }
            if (fd == null) {
                fd = state.getField(fieldName);
            }
            if (fd == null) {
                throw new SettingsHandlerException("field \"" + fieldName + "\" was not registered and was not specified as a dynamicField");
            }
            if (!(fd instanceof Sortable)) {
                throw new SettingsHandlerException(String.format("field: %s does not support sorting", fieldName));
            }
            sf = ((Sortable) fd).getSortField(_sub);
        }
        sortFields.add(sf);
    }
    return new Sort(sortFields.toArray(new SortField[0]));
}
Also used : SortType(com.yelp.nrtsearch.server.grpc.SortType) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) Sortable(com.yelp.nrtsearch.server.luceneserver.field.properties.Sortable) ArrayList(java.util.ArrayList) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Aggregations

SortType (com.yelp.nrtsearch.server.grpc.SortType)2 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)2 Sortable (com.yelp.nrtsearch.server.luceneserver.field.properties.Sortable)2 ArrayList (java.util.ArrayList)2 Sort (org.apache.lucene.search.Sort)2 SortField (org.apache.lucene.search.SortField)2 SearchHandler (com.yelp.nrtsearch.server.luceneserver.SearchHandler)1