Java Code Examples for org.apache.lucene.search.Sort#setSort()
The following examples show how to use
org.apache.lucene.search.Sort#setSort() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Query.java From restcommander with Apache License 2.0 | 6 votes |
private Sort getSort() { Sort sort = new Sort(); if (order.length > 0) { if (reverse) { if (order.length != 1) throw new SearchException("reverse can be used while sorting only one field with oderBy"); sort.setSort(new SortField(order[0], SortField.SCORE, true)); } else { SortField[] fields = new SortField[order.length]; for (int i = 0; i < fields.length; i++) { fields[i] = new SortField(order[i], SortField.SCORE); } sort.setSort(fields); } } return sort; }
Example 2
Source File: TestFieldCacheSort.java From lucene-solr with Apache License 2.0 | 5 votes |
/** test sorts when there's nothing in the index */ public void testEmptyIndex() throws Exception { IndexSearcher empty = newSearcher(new MultiReader()); Query query = new TermQuery(new Term("contents", "foo")); Sort sort = new Sort(); TopDocs td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); sort.setSort(SortField.FIELD_DOC); td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); sort.setSort(new SortField("int", SortField.Type.INT), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); sort.setSort(new SortField("string", SortField.Type.STRING, true), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); sort.setSort(new SortField("string_val", SortField.Type.STRING_VAL, true), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); sort.setSort(new SortField("float", SortField.Type.FLOAT), new SortField("string", SortField.Type.STRING)); td = empty.search(query, 10, sort, true); assertEquals(0, td.totalHits.value); }
Example 3
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
@SuppressWarnings( "unchecked" ) public F clearSorting( ) { this.sortFields = ""; this.sortDirections = ""; sortObj = new Sort( ); sortObj.setSort( new SortField[0] ); updateSorting = updateParamMap = true; return ( F )this; }
Example 4
Source File: BasicStorageTest.java From lumongo with Apache License 2.0 | 5 votes |
private static int runQuery(IndexReader indexReader, int count, Query q) throws IOException { long start = System.currentTimeMillis(); IndexSearcher searcher = new IndexSearcher(indexReader); Sort sort = new Sort(); sort.setSort(new SortedSetSortField("category", false)); TopFieldCollector collector = TopFieldCollector.create(sort, count, null, true, true, true); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; int totalHits = collector.getTotalHits(); @SuppressWarnings("unused") long searchTime = System.currentTimeMillis() - start; start = System.currentTimeMillis(); List<String> ids = new ArrayList<>(); for (ScoreDoc hit : hits) { int docId = hit.doc; Document d = searcher.doc(docId); ids.add(d.get("uid")); } @SuppressWarnings("unused") long fetchTime = System.currentTimeMillis() - start; return totalHits; }
Example 5
Source File: TestLuceneIndexer.java From dremio-oss with Apache License 2.0 | 4 votes |
@Test public void testSearchIndex() throws Exception { try (LuceneSearchIndex index = new LuceneSearchIndex(null, "test", true, CommitWrapper.NO_OP)) { final Document doc1 = new Document(); doc1.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("1".getBytes()), Store.YES)); doc1.add(new StringField("ds", "space1.ds1", Field.Store.NO)); doc1.add(new StringField("job", "job1", Field.Store.YES)); // since we want to sort on version add docvalues doc1.add(new StringField("version", "v1", Field.Store.NO)); doc1.add(new SortedDocValuesField("version", new BytesRef("v1"))); doc1.add(new StringField("foo", "bar1", Store.NO)); doc1.add(new SortedDocValuesField("foo", new BytesRef("bar1"))); final Document doc2 = new Document(); doc2.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("2".getBytes()), Store.YES)); doc2.add(new StringField("ds", "space1.ds1", Field.Store.NO)); doc2.add(new StringField("job", "job3", Field.Store.YES)); doc2.add(new StringField("version", "v2", Field.Store.NO)); doc2.add(new SortedDocValuesField("version", new BytesRef("v2"))); doc2.add(new StringField("foo", "bar2", Store.NO)); doc2.add(new SortedDocValuesField("foo", new BytesRef("bar2"))); final Document doc3 = new Document(); doc3.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("3".getBytes()), Store.YES)); doc3.add(new StringField("ds", "space2.ds2", Field.Store.NO)); doc3.add(new StringField("job", "job2", Field.Store.YES)); doc3.add(new StringField("version", "v1", Field.Store.NO)); doc3.add(new SortedDocValuesField("version", new BytesRef("v1"))); index.add(doc1); assertEquals(1, index.count(new TermQuery(new Term("ds", "space1.ds1")))); BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST)); builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST)); assertEquals(1, index.count(builder.build())); assertEquals(0, index.count(new TermQuery(new Term("ds", "space2.ds2")))); assertEquals(0, index.count(new TermQuery(new Term("version", "v2")))); builder = new BooleanQuery.Builder(); builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST)); builder.add(new BooleanClause(new TermQuery(new Term("version", "v2")), BooleanClause.Occur.MUST)); assertEquals(0, index.count(builder.build())); assertEquals(1, index.count(new TermsQuery(new Term("ds", "space1.ds1"), new Term("version1", "v2")))); index.add(doc2); index.add(doc3); assertEquals(2, index.count(new TermQuery(new Term("ds", "space1.ds1")))); assertEquals(1, index.count(new TermQuery(new Term("ds", "space2.ds2")))); builder = new BooleanQuery.Builder(); builder.add(new BooleanClause(new TermQuery(new Term("ds", "space2.ds2")), BooleanClause.Occur.MUST)); builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST)); assertEquals(1, index.count(builder.build())); builder = new BooleanQuery.Builder(); builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST)); builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST)); assertEquals(1, index.count(builder.build())); Sort sorter = new Sort(); sorter.setSort(new SortField("version", SortField.Type.STRING)); Collection<Document> documents = index.searchForDocuments(new TermQuery(new Term("ds", "space1.ds1")), 1000, sorter); assertEquals(2, documents.size()); // exists queries assertEquals(2, index.count(LuceneQueryConverter.INSTANCE.toLuceneQuery(SearchQueryUtils.newExistsQuery("foo")))); assertEquals(1, index.count(LuceneQueryConverter.INSTANCE.toLuceneQuery(SearchQueryUtils.newDoesNotExistQuery("foo")))); } }