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

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

Introduction

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

Prototype

public SortField[] getSort() 

Source Link

Document

Representation of the sort criteria.

Usage

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilderTest.java

License:Apache License

/**
 * Test getSort() by name, descending.//  ww w.  j  a  v  a  2 s  .  c  o  m
 */
@Test
public void testGetSortByNameDescending() {
    List<ResourceSortCriterion> sortCrit = new ArrayList<ResourceSortCriterion>();
    sortCrit.add(new ResourceSortCriterion(SortField.NAME, SortDirection.DESCENDING));
    ResourceSortCriteria criteria = new ResourceSortCriteria(sortCrit);

    Sort sort = sut.getSort(criteria);
    assertEquals(1, sort.getSort().length);
    assertEquals("byName", sort.getSort()[0].getField());
    assertEquals(true, sort.getSort()[0].getReverse());
}

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilderTest.java

License:Apache License

/**
 * Test getSort() by updates count, ascending.
 *///from ww  w.  jav a2s  .co m
@Test
public void testGetSortByUpdatesCount() {
    List<ResourceSortCriterion> sortCrit = new ArrayList<ResourceSortCriterion>();
    sortCrit.add(new ResourceSortCriterion(SortField.UPDATES_COUNT, SortDirection.ASCENDING));
    ResourceSortCriteria criteria = new ResourceSortCriteria(sortCrit);

    Sort sort = sut.getSort(criteria);
    assertEquals(1, sort.getSort().length);
    assertEquals("updatesCount", sort.getSort()[0].getField());
    assertEquals(false, sort.getSort()[0].getReverse());
}

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilderTest.java

License:Apache License

/**
 * Test getSort() by date updates count, descending.
 */// w w  w.j a v  a2 s.  c  o m
@Test
public void testGetSortByUpdatesCountDescending() {
    List<ResourceSortCriterion> sortCrit = new ArrayList<ResourceSortCriterion>();
    sortCrit.add(new ResourceSortCriterion(SortField.UPDATES_COUNT, SortDirection.DESCENDING));
    ResourceSortCriteria criteria = new ResourceSortCriteria(sortCrit);

    Sort sort = sut.getSort(criteria);
    assertEquals(1, sort.getSort().length);
    assertEquals("updatesCount", sort.getSort()[0].getField());
    assertEquals(true, sort.getSort()[0].getReverse());
}

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilderTest.java

License:Apache License

/**
 * Test getSort() by name ascending, updates descending.
 *///from w w  w .  j  a  v a2 s. c  o  m
@Test
public void testGetSortByNameAscendingAndUpdatesDescending() {
    List<ResourceSortCriterion> sortCrit = new ArrayList<ResourceSortCriterion>();
    sortCrit.add(new ResourceSortCriterion(SortField.NAME, SortDirection.ASCENDING));
    sortCrit.add(new ResourceSortCriterion(SortField.UPDATES_COUNT, SortDirection.DESCENDING));
    ResourceSortCriteria criteria = new ResourceSortCriteria(sortCrit);

    Sort sort = sut.getSort(criteria);
    assertEquals(2, sort.getSort().length);

    // name, ascending
    assertEquals("byName", sort.getSort()[0].getField());
    assertEquals(false, sort.getSort()[0].getReverse());

    // updates count, descending
    assertEquals("updatesCount", sort.getSort()[1].getField());
    assertEquals(true, sort.getSort()[1].getReverse());
}

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilderTest.java

License:Apache License

/**
 * Test overriding the sort method with setOverridingSortCriteria.
 *//*from   ww  w.  ja va 2s  .c om*/
@Test
public void testGetSortByFollowersCountDescendingFromOverride() {
    // setup the override as followersCount, descending
    List<ResourceSortCriterion> overridingSortCrit = new ArrayList<ResourceSortCriterion>();
    overridingSortCrit.add(new ResourceSortCriterion(SortField.FOLLOWERS_COUNT, SortDirection.DESCENDING));
    sut.setOverridingSortCriteria(new ResourceSortCriteria(overridingSortCrit));

    // now try different input criteria - name
    List<ResourceSortCriterion> sortCrit = new ArrayList<ResourceSortCriterion>();
    sortCrit.add(new ResourceSortCriterion(SortField.NAME, SortDirection.ASCENDING));
    ResourceSortCriteria criteria = new ResourceSortCriteria(sortCrit);

    // sort should come back as followers count, descending, ignoring the input criteria
    Sort sort = sut.getSort(criteria);
    assertEquals(1, sort.getSort().length);
    assertEquals("followersCount", sort.getSort()[0].getField());
    assertEquals(true, sort.getSort()[0].getReverse());
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.ChildAxisQuery.java

License:Apache License

/**
 * {@inheritDoc}// w  ww .  jav  a2s  .  co  m
 */
public QueryHits execute(JcrIndexSearcher searcher, SessionImpl session, Sort sort) throws IOException {
    if (sort.getSort().length == 0 && matchesAnyChildNode()) {
        Query context = getContextQuery();
        return new ChildNodesQueryHits(searcher.evaluate(context), session, indexConfig);
    } else {
        return null;
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.DescendantSelfAxisQuery.java

License:Apache License

/**
 * {@inheritDoc}//w ww  .ja  va  2  s . co m
 */
public QueryHits execute(final JcrIndexSearcher searcher, final SessionImpl session, final Sort sort)
        throws IOException {
    //       Query  tt = ((BooleanClause)((BooleanQuery)subQuery).clauses().get(0)).getQuery();
    //       searcher.search(((BooleanClause)((BooleanQuery)tt).clauses().get(0)).getQuery())
    //       searcher.search(new TermQuery(new Term(FieldNames.UUID, "f7196dc97f0001015040af77592c3b2f")))
    //       searcher.search(new TermQuery(new Term(FieldNames.FULLTEXT_PREFIX+":"+"title", "jumps")))
    //       searcher.getIndexReader().document(468)
    if (sort.getSort().length == 0 && subQueryMatchesAll()) {
        // maps path String to NodeId
        Map startingPoints = new TreeMap();
        QueryHits result = searcher.evaluate(getContextQuery());
        try {
            // minLevels 0 and 1 are handled with a series of
            // NodeTraversingQueryHits directly on result. For minLevels >= 2
            // intermediate ChildNodesQueryHits are required.
            for (int i = 2; i <= getMinLevels(); i++) {
                result = new ChildNodesQueryHits(result, session, indexConfig);
            }

            ScoreNode sn;
            try {
                while ((sn = result.nextScoreNode()) != null) {
                    //Node node = session.getNodeById(sn.getNodeId());
                    Node node = (Node) session.getTransientNodesManager().getItemByIdentifier(sn.getNodeId(),
                            true);
                    startingPoints.put(node.getPath(), sn);
                }
            } catch (RepositoryException e) {
                throw Util.createIOException(e);
            }
        } finally {
            result.close();
        }

        // prune overlapping starting points
        String previousPath = null;
        for (Iterator it = startingPoints.keySet().iterator(); it.hasNext();) {
            String path = (String) it.next();
            // if the previous path is a prefix of this path then the
            // current path is obsolete
            if (previousPath != null && path.startsWith(previousPath)) {
                it.remove();
            } else {
                previousPath = path;
            }
        }

        final Iterator scoreNodes = startingPoints.values().iterator();
        return new AbstractQueryHits() {

            private NodeTraversingQueryHits currentTraversal;

            private SessionDataManager itemMgr = session.getTransientNodesManager();

            {
                fetchNextTraversal();
            }

            @Override
            public void close() throws IOException {
                if (currentTraversal != null) {
                    currentTraversal.close();
                }
            }

            public ScoreNode nextScoreNode() throws IOException {
                while (currentTraversal != null) {
                    ScoreNode sn = currentTraversal.nextScoreNode();
                    if (sn != null) {
                        return sn;
                    } else {
                        fetchNextTraversal();
                    }
                }
                // if we get here there are no more score nodes
                return null;
            }

            private void fetchNextTraversal() throws IOException {
                if (currentTraversal != null) {
                    currentTraversal.close();
                }
                if (scoreNodes.hasNext()) {
                    ScoreNode sn = (ScoreNode) scoreNodes.next();
                    try {
                        //Node node = session.getNodeById(sn.getNodeId());
                        Node node = (Node) session.getTransientNodesManager()
                                .getItemByIdentifier(sn.getNodeId(), true);
                        currentTraversal = new NodeTraversingQueryHits(node, getMinLevels() == 0, indexConfig);
                    } catch (RepositoryException e) {
                        throw Util.createIOException(e);
                    }
                } else {
                    currentTraversal = null;
                }
            }
        };
    } else {
        return null;
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.JcrIndexSearcher.java

License:Apache License

/**
 * Evaluates the query and returns the hits that match the query.
 *
 * @param query           the query to execute.
 * @param sort            the sort criteria.
 * @param resultFetchHint a hint on how many results should be fetched.
 * @return the query hits.//  w w  w  . jav  a 2s  .c  om
 * @throws IOException if an error occurs while executing the query.
 */
public QueryHits evaluate(final Query query, final Sort sort, final long resultFetchHint) throws IOException {
    return SecurityHelper.doPrivilegedIOExceptionAction(new PrivilegedExceptionAction<QueryHits>() {
        public QueryHits run() throws Exception {
            Query localQuery = query.rewrite(reader);
            QueryHits hits = null;
            if (localQuery instanceof JcrQuery) {
                hits = ((JcrQuery) localQuery).execute(JcrIndexSearcher.this, session, sort);
            }
            if (hits == null) {
                if (sort == null || sort.getSort().length == 0) {
                    hits = new LuceneQueryHits(reader, JcrIndexSearcher.this, query);
                } else {
                    hits = new SortedLuceneQueryHits(reader, JcrIndexSearcher.this, localQuery, sort,
                            resultFetchHint);
                }
            }
            return hits;
        }
    });
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.MatchAllDocsQuery.java

License:Apache License

/**
  * {@inheritDoc}/* ww  w .ja  v a2 s .  co m*/
  */
public QueryHits execute(JcrIndexSearcher searcher, SessionImpl session, Sort sort) throws IOException {
    if (sort.getSort().length == 0) {
        try {
            return new NodeTraversingQueryHits(session.getRootNode(), true, indexConfig);
        } catch (RepositoryException e) {
            throw Util.createIOException(e);
        }
    } else {
        return null;
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.QueryHitsQuery.java

License:Apache License

/**
 * {@inheritDoc}//from  w w  w. j a  v  a  2s  . c  o  m
 */
public QueryHits execute(JcrIndexSearcher searcher, SessionImpl session, Sort sort) throws IOException {
    if (sort.getSort().length == 0) {
        return hits;
    } else {
        return null;
    }
}