Example usage for org.apache.lucene.search Sort Sort

List of usage examples for org.apache.lucene.search Sort Sort

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort Sort.

Prototype

public Sort(SortField... fields) 

Source Link

Document

Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc.

Usage

From source file:org.animotron.graph.index.Order.java

License:Open Source License

private static QueryContext sort(String key, String... additionalKeys) {
    QueryContext q = new QueryContext("*");
    SortField firstSortField = new SortField(key, SortField.LONG);
    if (additionalKeys.length == 0) {
        return q.sort(new Sort(firstSortField));
    }//w w  w .  jav  a 2s . com
    SortField[] sortFields = new SortField[1 + additionalKeys.length];
    sortFields[0] = firstSortField;
    for (int i = 0; i < additionalKeys.length; i++) {
        sortFields[1 + i] = new SortField(additionalKeys[i], SortField.LONG);
    }
    return q.sort(new Sort(sortFields));
}

From source file:org.animotron.OrderIndexTest.java

License:Open Source License

public QueryContext sort(String key, String... additionalKeys) {
    QueryContext q = new QueryContext("*");

    SortField firstSortField = new SortField(key, SortField.LONG);
    if (additionalKeys.length == 0) {
        return q.sort(new Sort(firstSortField));
    }/*from w  ww.  j  a v a  2  s.  co  m*/

    SortField[] sortFields = new SortField[1 + additionalKeys.length];
    sortFields[0] = firstSortField;
    for (int i = 0; i < additionalKeys.length; i++) {
        sortFields[1 + i] = new SortField(additionalKeys[i], SortField.LONG);
    }
    return q.sort(new Sort(sortFields));
}

From source file:org.apache.blur.lucene.search.TestingPagingCollector.java

License:Apache License

@Test
public void testSimpleSearchPagingWithSorting() throws Exception {
    IndexReader reader = getReaderFlatScore(length);
    IndexSearcherCloseable searcher = getSearcher(reader);

    TotalHitsRef totalHitsRef = new TotalHitsRef();
    ProgressRef progressRef = new ProgressRef();

    printHeapSize();//from   w w w .  j av a2 s . c  o  m
    TermQuery query = new TermQuery(new Term("f1", "value"));
    Sort sort = new Sort(new SortField("index", Type.INT, true));
    IterablePaging paging = new IterablePaging(new AtomicBoolean(true), searcher, query, 100, null, null, false,
            sort, new DeepPagingCache());
    IterablePaging itPaging = paging.skipTo(90).gather(20).totalHits(totalHitsRef).progress(progressRef);
    BlurIterator<ScoreDoc, BlurException> iterator = itPaging.iterator();
    int position = 90;
    int searches = 1;
    while (iterator.hasNext()) {
        ScoreDoc sd = iterator.next();
        assertEquals(position, progressRef.currentHitPosition());
        assertEquals(searches, progressRef.searchesPerformed());
        System.out.println("time [" + progressRef.queryTime() + "] " + "total hits [" + totalHitsRef.totalHits()
                + "] " + "searches [" + progressRef.searchesPerformed() + "] " + "position ["
                + progressRef.currentHitPosition() + "] " + "doc id [" + sd.doc + "] " + "score [" + sd.score
                + "]");
        position++;
        if (position == 100) {
            searches++;
        }
    }
    printHeapSize();
}

From source file:org.apache.blur.manager.IndexManager.java

License:Apache License

private Sort getSort(BlurQuery blurQuery, FieldManager fieldManager) throws IOException, BlurException {
    List<org.apache.blur.thrift.generated.SortField> sortFields = blurQuery.getSortFields();
    if (sortFields == null || sortFields.isEmpty()) {
        return null;
    }/*from  w  w  w  .j av a2  s.  c  o m*/
    SortField[] fields = new SortField[sortFields.size()];
    int i = 0;
    for (org.apache.blur.thrift.generated.SortField sortField : sortFields) {
        if (sortField == null) {
            throw new BException("Sortfields [{0}] can not contain a null.", sortFields);
        }
        String fieldName = getFieldName(sortField);
        SortField field = fieldManager.getSortField(fieldName, sortField.reverse);
        fields[i++] = field;
    }
    return new Sort(fields);
}

From source file:org.apache.clerezza.rdf.cris.GraphIndexer.java

License:Apache License

/**
 * Find resources using conditions and collect facets and specify a sort
 * order./*  w ww. j  a v a 2s .com*/
 *
 * This method allows to specify the indices of the query results to return
 * (e.g. for pagination).
 *
 * @param conditions a list of conditions to construct a query from.
 * @param facetCollectors Facet collectors to apply to the query result. Can
 * be {@link Collections#EMPTY_LIST}, if not used.
 * @param sortSpecification Specifies the sort order. Can be null, if not
 * used.
 * @param from return results starting from this index (inclusive).
 * @param to return results until this index (exclusive).
 * @return a list of resources that match the query.
 *
 * @throws ParseException when the resulting query is illegal.
 */
public List<NonLiteral> findResources(List<? extends Condition> conditions, SortSpecification sortSpecification,
        List<FacetCollector> facetCollectors, int from, int to) throws ParseException {

    if (from < 0) {
        from = 0;
    }

    if (to < from) {
        to = from + 1;
    }

    if (facetCollectors == null) {
        facetCollectors = Collections.EMPTY_LIST;
    }

    BooleanQuery booleanQuery = new BooleanQuery();
    for (Condition c : conditions) {
        booleanQuery.add(c.query(), BooleanClause.Occur.MUST);
    }

    IndexSearcher searcher = luceneTools.getIndexSearcher();
    ScoreDoc[] hits = null;
    try {
        if (sortSpecification != null) {
            SortFieldArrayWrapper fieldKey = new SortFieldArrayWrapper(sortSpecification.getSortFields());
            Sort sort = sortCache.get(fieldKey);
            if (sort == null) {
                sort = new Sort(sortSpecification.getSortFields());
                sortCache.put(fieldKey, sort);
            }
            //searcher.setDefaultFieldSortScoring(true, true);
            TopFieldDocs topFieldDocs = searcher.search(booleanQuery, null, to, sort);
            hits = topFieldDocs.scoreDocs;
        } else {
            TopScoreDocCollector collector = TopScoreDocCollector.create(to, true);
            searcher.search(booleanQuery, collector);
            hits = collector.topDocs().scoreDocs;
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

    List<NonLiteral> result = new ArrayList<NonLiteral>();

    for (int i = from; i < hits.length; ++i) {
        int docId = hits[i].doc;
        Document d;
        try {
            d = searcher.doc(docId);
            collectFacets(facetCollectors, d);
            result.add(getResource(d));
        } catch (IOException ex) {
            logger.error("CRIS Error: ", ex);
        }
    }

    for (FacetCollector facetCollector : facetCollectors) {
        facetCollector.postProcess();
    }

    return result;
}

From source file:org.apache.jackrabbit.core.query.lucene.SearchIndex.java

License:Apache License

/**
 * Executes the query on the search index.
 *
 * @param session         the session that executes the query.
 * @param queryImpl       the query impl.
 * @param query           the lucene query.
 * @param orderProps      name of the properties for sort order.
 * @param orderSpecs      the order specs for the sort order properties.
 *                        <code>true</code> indicates ascending order,
 *                        <code>false</code> indicates descending.
 * @param resultFetchHint a hint on how many results should be fetched.
 * @return the query hits./*w  ww .j a  v a  2s.  co m*/
 * @throws IOException if an error occurs while searching the index.
 */
public MultiColumnQueryHits executeQuery(SessionImpl session, AbstractQueryImpl queryImpl, Query query,
        Path[] orderProps, boolean[] orderSpecs, long resultFetchHint) throws IOException {
    checkOpen();

    Sort sort = new Sort(createSortFields(orderProps, orderSpecs));

    final IndexReader reader = getIndexReader(queryImpl.needsSystemTree());
    JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader,
            getContext().getItemStateManager());
    searcher.setSimilarity(getSimilarity());
    return new FilterMultiColumnQueryHits(
            searcher.execute(query, sort, resultFetchHint, QueryImpl.DEFAULT_SELECTOR_NAME)) {
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                PerQueryCache.getInstance().dispose();
                Util.closeOrRelease(reader);
            }
        }
    };
}

From source file:org.apache.jackrabbit.core.query.lucene.SearchIndex.java

License:Apache License

/**
 * Executes the query on the search index.
 *
 * @param session         the session that executes the query.
 * @param query           the query.//from  w  ww.j av a 2 s  .co m
 * @param orderings       the order specs for the sort order.
 * @param resultFetchHint a hint on how many results should be fetched.
 * @return the query hits.
 * @throws IOException if an error occurs while searching the index.
 */
public MultiColumnQueryHits executeQuery(SessionImpl session, MultiColumnQuery query, OrderingImpl[] orderings,
        long resultFetchHint) throws IOException {
    checkOpen();

    Sort sort = new Sort(createSortFields(orderings));

    final IndexReader reader = getIndexReader();
    JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader,
            getContext().getItemStateManager());
    searcher.setSimilarity(getSimilarity());
    return new FilterMultiColumnQueryHits(query.execute(searcher, sort, resultFetchHint)) {
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                PerQueryCache.getInstance().dispose();
                Util.closeOrRelease(reader);
            }
        }
    };
}

From source file:org.apache.jackrabbit.core.query.lucene.SearchIndex.java

License:Apache License

/**
 * Executes the query on the search index.
 *
 * @param session         the session that executes the query.
 * @param query           the query./*from ww  w. ja v  a  2  s.c  om*/
 * @param orderProps      name of the properties for sort order.
 * @param orderSpecs      the order specs for the sort order properties.
 *                        <code>true</code> indicates ascending order,
 *                        <code>false</code> indicates descending.
 * @param resultFetchHint a hint on how many results should be fetched.
 * @return the query hits.
 * @throws IOException if an error occurs while searching the index.
 */
public MultiColumnQueryHits executeQuery(SessionImpl session, MultiColumnQuery query, Path[] orderProps,
        boolean[] orderSpecs, long resultFetchHint) throws IOException {
    checkOpen();

    Sort sort = new Sort(createSortFields(orderProps, orderSpecs));

    final IndexReader reader = getIndexReader();
    JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader,
            getContext().getItemStateManager());
    searcher.setSimilarity(getSimilarity());
    return new FilterMultiColumnQueryHits(query.execute(searcher, sort, resultFetchHint)) {
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                PerQueryCache.getInstance().dispose();
                Util.closeOrRelease(reader);
            }
        }
    };
}

From source file:org.apache.jackrabbit.core.query.lucene.SearchIndex.java

License:Apache License

/**
 * Executes the query on the search index.
 * @param queryImpl the query impl./*ww  w. j  ava  2s.  c om*/
 * @param query the lucene query.
 * @param orderProps name of the properties for sort order.
 * @param orderSpecs the order specs for the sort order properties.
 * <code>true</code> indicates ascending order, <code>false</code> indicates
 * descending.
 * @return the lucene Hits object.
 * @throws IOException if an error occurs while searching the index.
 */
public QueryHits executeQuery(AbstractQueryImpl queryImpl, Query query, Name[] orderProps, boolean[] orderSpecs)
        throws IOException {
    checkOpen();
    SortField[] sortFields = createSortFields(orderProps, orderSpecs);

    IndexReader reader = getIndexReader(queryImpl.needsSystemTree());
    IndexSearcher searcher = new IndexSearcher(reader);
    Hits hits;
    if (sortFields.length > 0) {
        hits = searcher.search(query, new Sort(sortFields));
    } else {
        hits = searcher.search(query);
    }
    return new QueryHits(hits, reader);
}

From source file:org.apache.jackrabbit.core.query.lucene.MultiColumnQueryAdapter.java

License:Apache License

/**
 * {@inheritDoc}/*w  ww .j av a  2  s  .c om*/
 */
public MultiColumnQueryHits execute(JackrabbitIndexSearcher searcher, Ordering[] orderings,
        long resultFetchHint) throws IOException {
    SortField[] fields = new SortField[orderings.length];
    for (int i = 0; i < orderings.length; i++) {
        fields[i] = orderings[i].getSortField();
    }
    return searcher.execute(query, new Sort(fields), resultFetchHint, selectorName);
}