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:com.stratio.cassandra.index.RowMapperWide.java

License:Apache License

/**
 * Returns the Lucene {@link Sort} to get {@link Document}s in the same order that is used in Cassandra.
 *
 * @return The Lucene {@link Sort} to get {@link Document}s in the same order that is used in Cassandra.
 *//*from www.j  ava 2 s  .  c  o m*/
public Sort sort() {
    SortField[] partitionKeySort = tokenMapper.sortFields();
    SortField[] clusteringKeySort = clusteringKeyMapper.sortFields();
    return new Sort(ArrayUtils.addAll(partitionKeySort, clusteringKeySort));
}

From source file:com.stratio.cassandra.lucene.index.FSIndexTest.java

License:Apache License

@Test
public void testCRUD() throws IOException, InterruptedException {

    FSIndex index = new FSIndex("test_index", "com.stratio.cassandra.lucene:type=LuceneIndexes",
            Paths.get(folder.newFolder("directory" + UUID.randomUUID()).getPath()), new StandardAnalyzer(),
            REFRESH_SECONDS, IndexOptions.DEFAULT_RAM_BUFFER_MB, IndexOptions.DEFAULT_MAX_MERGE_MB,
            IndexOptions.DEFAULT_MAX_CACHED_MB, null);

    Sort sort = new Sort(new SortField("field", SortField.Type.STRING));
    assertEquals("Index must be empty", 0, index.getNumDocs());

    Term term1 = new Term("field", "value1");
    Document document1 = new Document();
    document1.add(new StringField("field", "value1", Field.Store.NO));
    document1.add(new SortedDocValuesField("field", new BytesRef("value1")));
    index.upsert(term1, document1);//from   w ww .  ja va2s.  co m

    Term term2 = new Term("field", "value2");
    Document document2 = new Document();
    document2.add(new StringField("field", "value2", Field.Store.NO));
    document2.add(new SortedDocValuesField("field", new BytesRef("value2")));
    index.upsert(term2, document2);

    index.commit();
    Thread.sleep(REFRESH_MILLISECONDS);
    assertEquals("Expected 2 documents", 2, index.getNumDocs());

    Query query = new WildcardQuery(new Term("field", "value*"));
    Set<String> fields = Sets.newHashSet("field");

    // Search
    //        Iterator<Document> iterator;
    //            iterator = index.search( query, sort, null, 1, fields);
    //            assertEquals("Expected 1 document", 1, results.size());
    //            ScoreDoc last3 = results.values().iterator().next();
    //            results = index.search(searcher, query, sort, last3, 1, fields);
    //            assertEquals("Expected 1 document", 1, results.size());

    // Delete by term
    index.delete(term1);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals("Expected 1 document", 1, index.getNumDocs());

    // Delete by query
    index.upsert(term1, document1);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals("Expected 2 documents", 2, index.getNumDocs());
    index.delete(new TermQuery(term1));
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals("Expected 1 document", 1, index.getNumDocs());

    // Upsert
    index.upsert(term1, document1);
    index.upsert(term2, document2);
    index.upsert(term2, document2);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals("Expected 2 documents", 2, index.getNumDocs());

    // Truncate
    index.truncate();
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals("Expected 0 documents", 0, index.getNumDocs());

    // Delete
    index.delete();

    // Cleanup
    folder.delete();
}

From source file:com.stratio.cassandra.lucene.IndexService.java

License:Apache License

/**
 * Returns the Lucene {@link Sort} with the specified {@link Search} sorting requirements followed by the
 * Cassandra's natural ordering based on partitioning token and cell name.
 *
 * @param search the {@link Search} containing sorting requirements
 * @return a Lucene sort according to {@code search}
 *///  w w  w .  j a  v a  2s .c om
private Sort sort(Search search) {
    List<SortField> sortFields = new ArrayList<>();
    if (search.usesSorting()) {
        sortFields.addAll(search.sortFields(schema));
    }
    if (search.usesRelevance()) {
        sortFields.add(FIELD_SCORE);
    }
    sortFields.addAll(keySortFields());
    return new Sort(sortFields.toArray(new SortField[sortFields.size()]));
}

From source file:com.stratio.cassandra.lucene.service.LuceneIndexTest.java

License:Apache License

@Test
public void testCRUD() throws IOException, InterruptedException {

    Path path = Paths.get(folder.newFolder("directory" + UUID.randomUUID()).getPath());
    LuceneIndex index = new LuceneIndex("ks", "cf", "idx", path, IndexConfig.DEFAULT_RAM_BUFFER_MB,
            IndexConfig.DEFAULT_MAX_MERGE_MB, IndexConfig.DEFAULT_MAX_CACHED_MB, new StandardAnalyzer(),
            REFRESH_SECONDS, null);//from   w  ww . j ava 2s .c o  m
    Sort sort = new Sort(new SortField("field", SortField.Type.STRING));
    assertEquals(0, index.getNumDocs());

    Term term1 = new Term("field", "value1");
    Document document1 = new Document();
    document1.add(new StringField("field", "value1", Field.Store.NO));
    document1.add(new SortedDocValuesField("field", new BytesRef("value1")));
    index.upsert(term1, document1);

    Term term2 = new Term("field", "value2");
    Document document2 = new Document();
    document2.add(new StringField("field", "value2", Field.Store.NO));
    document2.add(new SortedDocValuesField("field", new BytesRef("value2")));
    index.upsert(term2, document2);

    index.commit();
    Thread.sleep(REFRESH_MILLISECONDS);
    assertEquals(2, index.getNumDocs());

    Query query = new WildcardQuery(new Term("field", "value*"));
    Set<String> fields = Sets.newHashSet("field");
    Map<Document, ScoreDoc> results;

    // Search
    SearcherManager searcherManager = index.getSearcherManager();
    IndexSearcher searcher = searcherManager.acquire();

    try {
        results = index.search(searcher, query, null, null, 1, fields);
        assertEquals(1, results.size());
        ScoreDoc last1 = results.values().iterator().next();
        results = index.search(searcher, query, null, last1, 1, fields);
        assertEquals(1, results.size());

        results = index.search(searcher, query, null, null, 1, fields);
        assertEquals(1, results.size());
        ScoreDoc last2 = results.values().iterator().next();
        results = index.search(searcher, query, null, last2, 1, fields);
        assertEquals(1, results.size());

        results = index.search(searcher, query, sort, null, 1, fields);
        assertEquals(1, results.size());
        ScoreDoc last3 = results.values().iterator().next();
        results = index.search(searcher, query, sort, last3, 1, fields);
        assertEquals(1, results.size());
    } finally {
        searcherManager.release(searcher);
    }

    // Delete by term
    index.delete(term1);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals(1, index.getNumDocs());

    // Delete by query
    index.upsert(term1, document1);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals(2, index.getNumDocs());
    index.delete(new TermQuery(term1));
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals(1, index.getNumDocs());

    // Upsert
    index.upsert(term1, document1);
    index.upsert(term2, document2);
    index.upsert(term2, document2);
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals(2, index.getNumDocs());

    // Truncate
    index.truncate();
    index.commit();
    Thread.sleep(WAIT_MILLISECONDS);
    assertEquals(0, index.getNumDocs());

    // Delete
    index.delete();

    // Cleanup
    folder.delete();
}

From source file:com.stratio.cassandra.lucene.service.RowMapperSkinny.java

License:Apache License

/**
 * {@inheritDoc}
 */
@Override
public Sort sort() {
    return new Sort(tokenMapper.sortFields());
}

From source file:com.stratio.cassandra.lucene.service.RowMapperWide.java

License:Apache License

/**
 * {@inheritDoc}/*w  ww  . j  a va2  s. c o  m*/
 */
@Override
public Sort sort() {
    SortField[] partitionKeySort = tokenMapper.sortFields();
    SortField[] clusteringKeySort = clusteringKeyMapper.sortFields();
    return new Sort(ArrayUtils.addAll(partitionKeySort, clusteringKeySort));
}

From source file:com.thoughtworks.studios.journey.jql.JourneyQuery.java

License:Open Source License

private Iterable<Node> query() {
    BooleanQuery luceneQuery = new BooleanQuery();
    luceneQuery.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    for (JourneyCondition condition : conditions) {
        if (condition.matchingIndexes()) {
            Query q = condition.indexQuery(app);
            luceneQuery.add(q, BooleanClause.Occur.MUST);
        }//ww  w.  jav  a 2  s .  co m
    }
    Sort sorting = new Sort(new SortField(Journeys.PROP_START_AT, SortField.LONG, descOrder));
    QueryContext queryContext = new QueryContext(luceneQuery).sort(sorting);
    return app.journeys().query(queryContext);
}

From source file:com.tripod.lucene.service.AbstractLuceneService.java

License:Apache License

/**
 * Converts the Tripod Sort clauses to a Lucene Sort instance.
 *
 * @param sorts the Tripod Sorts/*from w ww . jav  a2s.c o  m*/
 * @return the Lucene Sort matching the given Tripod Sorts,
 *              or the Lucene Sort for relevance order if no sorts are specified
 */
protected Sort getSort(List<com.tripod.api.query.Sort<LuceneField>> sorts) {
    if (sorts == null || sorts.isEmpty()) {
        return Sort.RELEVANCE;
    } else {
        List<SortField> luceneSorts = new ArrayList<>();
        for (com.tripod.api.query.Sort<LuceneField> sort : sorts) {
            boolean reverse = (sort.getSortOrder() == SortOrder.DESC);
            luceneSorts.add(new SortField(sort.getField().getName(), sort.getField().getSortType(), reverse));
        }
        return new Sort(luceneSorts.toArray(new SortField[luceneSorts.size()]));
    }
}

From source file:com.vmware.dcp.services.common.LuceneBlobIndexService.java

License:Open Source License

@Override
public void handleStart(final Operation post) {
    this.executor = getHost().allocateExecutor(this, 1);
    super.setMaintenanceIntervalMicros(getHost().getMaintenanceIntervalMicros() * 5);
    File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory);
    this.timeSort = new Sort(new SortField(URI_PARAM_NAME_UPDATE_TIME, SortField.Type.LONG, true));
    try {/* w  w  w .  j av  a  2s  . c  o m*/
        this.writer = createWriter(directory);
    } catch (IOException e) {
        post.fail(e);
        return;
    }
    post.complete();
}

From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java

License:Open Source License

@Override
public void handleStart(final Operation post) {
    super.setMaintenanceIntervalMicros(getHost().getMaintenanceIntervalMicros() * 5);

    File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory);
    this.privateQueryExecutor = Executors.newFixedThreadPool(QUERY_THREAD_COUNT,
            r -> new Thread(r, getUri() + "/queries/" + Utils.getNowMicrosUtc()));
    this.privateIndexingExecutor = Executors.newFixedThreadPool(UPDATE_THREAD_COUNT,
            r -> new Thread(r, getSelfLink() + "/updates/" + Utils.getNowMicrosUtc()));
    this.versionSort = new Sort(new SortField(ServiceDocument.FIELD_NAME_VERSION, SortField.Type.LONG, true));

    this.fieldsToLoadNoExpand = new HashSet<>();
    this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_SELF_LINK);
    this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_VERSION);
    this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS);
    this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_UPDATE_ACTION);
    this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS);
    this.fieldsToLoadWithExpand = new HashSet<>(this.fieldsToLoadNoExpand);
    this.fieldsToLoadWithExpand.add(LUCENE_FIELD_NAME_JSON_SERIALIZED_STATE);
    this.fieldsToLoadWithExpand.add(LUCENE_FIELD_NAME_BINARY_SERIALIZED_STATE);

    // create durable index writer
    for (int retryCount = 0; retryCount < 2; retryCount++) {
        try {//from  w  ww .  ja v a2s.  c om
            createWriter(directory, true);
            // we do not actually know if the index is OK, until we try to query
            doSelfValidationQuery();
            break;
        } catch (Throwable e) {
            adjustStat(STAT_NAME_INDEX_LOAD_RETRY_COUNT, 1);
            logWarning("failure creating index writer: %s", Utils.toString(e));
            if (retryCount < 1) {
                close(this.writer);
                archiveCorruptIndexFiles(directory);
                continue;
            }
            post.fail(e);
            return;
        }
    }

    post.complete();
}