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.infoglue.cms.controllers.kernel.impl.simple.LuceneUsersController.java

License:Open Source License

/**
 * A query api for getting all users sorted and paged
 *///from   w w  w .  j a  v a2 s .  co  m

public List<Document> queryDocuments(Integer offset, Integer limit, String sortProperty, String direction)
        throws Exception {
    IndexSearcher searcher = getIndexSearcher();
    List<Document> docs = new ArrayList<Document>();

    if (offset == null)
        offset = 0;

    if (limit == null)
        limit = 10;

    if (sortProperty == null)
        sortProperty = "userName";

    boolean reverse = false;
    if (direction != null && direction.equalsIgnoreCase("desc"))
        reverse = true;

    logger.info("sortProperty:" + sortProperty);
    logger.info("reverse:" + reverse);
    SortField sf = new SortField(sortProperty, SortField.STRING, reverse);

    Query query = new MatchAllDocsQuery();

    TopFieldDocs topDocs = searcher.search(query, (Filter) null, 1000000, new Sort(sf));
    logger.info("offset:" + offset);
    logger.info("limit:" + limit);
    logger.info("topDocs.totalHits:" + topDocs.totalHits);

    int start = offset;
    int end = offset + limit;
    if (end > topDocs.totalHits)
        end = topDocs.totalHits;

    logger.info("start:" + start);
    logger.info("end:" + end);

    for (int i = start; i < end; i++) {
        ScoreDoc scoreDoc = topDocs.scoreDocs[i];
        Document doc = searcher.doc(scoreDoc.doc);
        docs.add(doc);
    }

    searcher.close();
    return docs;
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.LuceneUsersController.java

License:Open Source License

/**
 * A query api for getting all users sorted and paged filtered on search text
 *//* www  .j a  v a 2 s  .  c  o  m*/

public List<Document> queryDocuments(Integer offset, Integer limit, String sortProperty, String direction,
        String searchText) throws Exception {
    Analyzer analyzer = getAnalyzer();
    IndexSearcher searcher = getIndexSearcher();
    List<Document> docs = new ArrayList<Document>();

    if (offset == null)
        offset = 0;

    if (limit == null)
        limit = 10;

    if (sortProperty == null)
        sortProperty = "userName";

    boolean reverse = false;
    if (direction != null && direction.equalsIgnoreCase("desc"))
        reverse = true;

    SortField sf = new SortField(sortProperty, SortField.STRING, reverse);
    logger.info("searchText:" + searchText);
    Query query = new QueryParser(Version.LUCENE_34, "contents", analyzer).parse(searchText + "*");

    TopFieldDocs topDocs = searcher.search(query, (Filter) null, 1000000, new Sort(sf));
    logger.info("offset:" + offset);
    logger.info("limit:" + limit);
    logger.info("topDocs.totalHits:" + topDocs.totalHits);

    int start = offset;
    int end = offset + limit;
    if (end > topDocs.totalHits)
        end = topDocs.totalHits;

    logger.info("start:" + start);
    logger.info("end:" + end);

    for (int i = start; i < end; i++) {
        ScoreDoc scoreDoc = topDocs.scoreDocs[i];
        Document doc = searcher.doc(scoreDoc.doc);
        //System.out.println("doc:" + doc);
        docs.add(doc);
    }

    searcher.close();
    return docs;
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.LuceneUsersController.java

License:Open Source License

/**
 * A query api for getting user docs found by role or group mainly sorted and paged and optinally filtered on search text
 *//*from ww w. j  ava 2 s .  c o  m*/

public List<Document> getDocuments(String field, String entityName, Integer offset, Integer limit,
        String sortProperty, String direction, String searchText) throws Exception {
    Analyzer analyzer = getAnalyzer();
    IndexSearcher searcher = getIndexSearcher();
    List<Document> docs = new ArrayList<Document>();

    if (offset == null)
        offset = 0;

    if (limit == null)
        limit = 10;

    if (sortProperty == null)
        sortProperty = "userName";

    boolean reverse = false;
    if (direction != null && direction.equalsIgnoreCase("desc"))
        reverse = true;

    SortField sf = new SortField(sortProperty, SortField.STRING, reverse);
    logger.info("searchText:" + searchText);
    Query query = new QueryParser(Version.LUCENE_34, field, analyzer).parse(entityName);
    if (searchText != null && !searchText.equals("")) {
        MultiFieldQueryParser mfqp = new MultiFieldQueryParser(Version.LUCENE_34,
                new String[] { field, "contents" }, analyzer);
        mfqp.setDefaultOperator(MultiFieldQueryParser.AND_OPERATOR);
        query = mfqp.parse(entityName + " " + searchText + "*");
    }

    logger.info("query" + query);

    TopFieldDocs topDocs = searcher.search(query, (Filter) null, 1000000, new Sort(sf));
    logger.info("offset:" + offset);
    logger.info("limit:" + limit);
    logger.info("topDocs.totalHits:" + topDocs.totalHits);

    int start = offset;
    int end = offset + limit;
    if (end > topDocs.totalHits)
        end = topDocs.totalHits;

    logger.info("start:" + start);
    logger.info("end:" + end);

    for (int i = start; i < end; i++) {
        ScoreDoc scoreDoc = topDocs.scoreDocs[i];
        Document doc = searcher.doc(scoreDoc.doc);
        docs.add(doc);
    }

    searcher.close();
    return docs;
}

From source file:org.infoglue.deliver.externalsearch.SearchRequest.java

License:Open Source License

public Sort getOrdering() {
    if (sortFields.size() == 0) {
        logger.debug("SearchRequest has not sort fields. Cannot sort");
        return null;
    } else {//  www .j a  v  a  2  s. com
        SortField[] sorts = new SortField[sortFields.size()];

        int i = 0;
        for (Map.Entry<String, Boolean> entry : sortFields.entrySet()) {
            sorts[i++] = new SortField(entry.getKey(), !entry.getValue());
        }

        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Generating sort with fields: " + Arrays.toString(sorts) + " for language: " + language);
        }

        return new Sort(sorts);
    }
}

From source file:org.jboss.as.quickstarts.kitchensink.data.FullTextMemberRepository.java

License:Open Source License

@Override
public List<Member> findAllOrderedByName() {
    Query luceneQuery = queryBuilder.all().createQuery();
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
    List resultList = fullTextEntityManager.createFullTextQuery(luceneQuery)
            .initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID)
            .setSort(new Sort(new SortField("sortableStoredName", SortField.STRING_VAL))).getResultList();
    return resultList;
}

From source file:org.jboss.capedwarf.datastore.query.Projections.java

License:Open Source License

public static void addDefaultSort(CacheQuery cacheQuery, IndexesXml.Index index) {
    if (index.getProperties().isEmpty()) {
        return;/*www  .j  a  v  a  2s . c  om*/
    }

    List<SortField> sortFields = new ArrayList<>();
    for (IndexesXml.Property property : index.getProperties()) {
        boolean reverse = "desc".equals(property.getDirection());
        sortFields.add(new SortField(property.getName(), SortField.STRING, reverse));
    }
    cacheQuery.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
}

From source file:org.jboss.capedwarf.datastore.query.SortPredicateConverter.java

License:Open Source License

public Sort convert(List<Query.SortPredicate> sortPredicates) {
    return new Sort(toArray(convertToSortFields(sortPredicates)));
}

From source file:org.jboss.capedwarf.log.CapedwarfLogService.java

License:Open Source License

private CacheQuery createRequestLogsQuery(CapedwarfLogQuery logQuery) {
    LogQuery query = logQuery.getQuery();
    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(CapedwarfRequestLogs.class).get();
    List<Query> queries = new ArrayList<Query>();
    if (query.getStartTimeUsec() != null) {
        queries.add(queryBuilder.range().onField(CapedwarfRequestLogs.END_TIME_USEC)
                .above(query.getStartTimeUsec()).createQuery());
    }//from www .ja va  2  s .  c  o  m
    if (query.getEndTimeUsec() != null) {
        queries.add(queryBuilder.range().onField(CapedwarfRequestLogs.END_TIME_USEC)
                .below(query.getEndTimeUsec()).createQuery());
    }
    if (query.getMinLogLevel() != null) {
        queries.add(queryBuilder.range().onField(CapedwarfRequestLogs.MAX_LOG_LEVEL)
                .above(query.getMinLogLevel().ordinal()).createQuery());
    }

    boolean onlyCompleteRequests = !Boolean.TRUE.equals(query.getIncludeIncomplete());
    if (onlyCompleteRequests) {
        queries.add(queryBuilder.keyword().onField(CapedwarfRequestLogs.FINISHED).matching(Boolean.TRUE)
                .createQuery());
    }

    Query luceneQuery = getQuery(queryBuilder, queries);
    CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, CapedwarfRequestLogs.class);
    cacheQuery.sort(new Sort(new SortField(CapedwarfRequestLogs.END_TIME_USEC, SortField.LONG, true)));
    cacheQuery.firstResult(logQuery.getOptions().getOffset());
    if (logQuery.getOptions().getLimit() != null) {
        cacheQuery.maxResults(logQuery.getOptions().getLimit());
    }
    return cacheQuery;
}

From source file:org.jboss.capedwarf.log.CapedwarfLogService.java

License:Open Source License

private CacheQuery createAppLogLinesQuery(RequestLogs requestLogs) {
    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(CapedwarfAppLogLine.class).get();
    Query query = queryBuilder.keyword().onField(CapedwarfAppLogLine.REQUEST_ID)
            .matching(requestLogs.getRequestId()).createQuery();
    CacheQuery cacheQuery = searchManager.getQuery(query, CapedwarfAppLogLine.class);
    cacheQuery.sort(new Sort(new SortField(CapedwarfAppLogLine.SEQUENCE_NUMBER, SortField.LONG)));
    return cacheQuery;
}

From source file:org.kie.workbench.common.dmn.backend.editors.types.query.FindAllDmnAssetsQuery.java

License:Apache License

@Override
public Sort getSortOrder() {
    return new Sort(new SortField(FILE_NAME_FIELD_SORTED, STRING));
}