List of usage examples for org.apache.lucene.search SortField STRING_LAST
Object STRING_LAST
To view the source code for org.apache.lucene.search SortField STRING_LAST.
Click Source Link
From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java
License:Open Source License
private void setMissingValuesOrder(SortField sf, SortField.Type type, boolean desc) { switch (type) { case STRING:/* w w w.j a va 2 s. com*/ sf.setMissingValue(desc ? SortField.STRING_FIRST : SortField.STRING_LAST); break; case FLOAT: sf.setMissingValue(Float.MIN_VALUE); break; case INT: sf.setMissingValue(Integer.MIN_VALUE); break; default: throw new IllegalArgumentException("Unexpected sort type: " + type); } }
From source file:com.qwazr.search.field.SortUtils.java
License:Apache License
final static void sortStringMissingValue(QueryDefinition.SortEnum sortEnum, SortField sortField) { if (sortEnum == null) return;//from w ww.j av a 2 s . c om switch (sortEnum) { case ascending: case descending: return; case ascending_missing_last: case descending_missing_first: sortField.setMissingValue(SortField.STRING_LAST); case ascending_missing_first: case descending_missing_last: sortField.setMissingValue(SortField.STRING_FIRST); } }
From source file:io.vertigo.dynamo.plugins.collections.lucene.RamLuceneIndex.java
License:Apache License
private static Optional<Sort> createSort(final DtListState dtListState) { if (dtListState.getSortFieldName().isPresent()) { final String sortFieldName = dtListState.getSortFieldName().get(); final boolean sortDesc = dtListState.isSortDesc().get(); final SortField.Type luceneType = SortField.Type.STRING; //TODO : check if other type are necessary final SortField sortField = new SortField(sortFieldName, luceneType, sortDesc); sortField.setMissingValue(SortField.STRING_LAST); return Optional.of(new Sort(sortField)); }//from w ww .j a va 2 s .c o m return Optional.empty(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on type string with a missing * value sorted last *//*from w ww . jav a 2s . c o m*/ private void testStringMissingSortedLast(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType); sf.setMissingValue(SortField.STRING_LAST); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value")); // null comes last assertNull(searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests reverse sorting on type string with a missing * value sorted last *//*from www . j a v a2s. c o m*/ private void testStringMissingSortedLastReverse(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType, true); sf.setMissingValue(SortField.STRING_LAST); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); // null comes first assertNull(searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSortRandom.java
License:Apache License
private void testRandomStringSort(SortField.Type type) throws Exception { Random random = new Random(random().nextLong()); final int NUM_DOCS = atLeast(100); final Directory dir = newDirectory(); final RandomIndexWriter writer = new RandomIndexWriter(random, dir); final boolean allowDups = random.nextBoolean(); final Set<String> seen = new HashSet<>(); final int maxLength = TestUtil.nextInt(random, 5, 100); if (VERBOSE) { System.out// ww w. ja v a2s . c om .println("TEST: NUM_DOCS=" + NUM_DOCS + " maxLength=" + maxLength + " allowDups=" + allowDups); } int numDocs = 0; final List<BytesRef> docValues = new ArrayList<>(); // TODO: deletions while (numDocs < NUM_DOCS) { final Document doc = new Document(); // 10% of the time, the document is missing the value: final BytesRef br; if (random().nextInt(10) != 7) { final String s; if (random.nextBoolean()) { s = TestUtil.randomSimpleString(random, maxLength); } else { s = TestUtil.randomUnicodeString(random, maxLength); } if (!allowDups) { if (seen.contains(s)) { continue; } seen.add(s); } if (VERBOSE) { System.out.println(" " + numDocs + ": s=" + s); } doc.add(new StringField("stringdv", s, Field.Store.NO)); docValues.add(new BytesRef(s)); } else { br = null; if (VERBOSE) { System.out.println(" " + numDocs + ": <missing>"); } docValues.add(null); } doc.add(new IntPoint("id", numDocs)); doc.add(new StoredField("id", numDocs)); writer.addDocument(doc); numDocs++; if (random.nextInt(40) == 17) { // force flush writer.getReader().close(); } } Map<String, UninvertingReader.Type> mapping = new HashMap<>(); mapping.put("stringdv", Type.SORTED); mapping.put("id", Type.INTEGER_POINT); final IndexReader r = UninvertingReader.wrap(writer.getReader(), mapping); writer.close(); if (VERBOSE) { System.out.println(" reader=" + r); } final IndexSearcher s = newSearcher(r, false); final int ITERS = atLeast(100); for (int iter = 0; iter < ITERS; iter++) { final boolean reverse = random.nextBoolean(); final TopFieldDocs hits; final SortField sf; final boolean sortMissingLast; final boolean missingIsNull; sf = new SortField("stringdv", type, reverse); sortMissingLast = random().nextBoolean(); missingIsNull = true; if (sortMissingLast) { sf.setMissingValue(SortField.STRING_LAST); } final Sort sort; if (random.nextBoolean()) { sort = new Sort(sf); } else { sort = new Sort(sf, SortField.FIELD_DOC); } final int hitCount = TestUtil.nextInt(random, 1, r.maxDoc() + 20); final RandomQuery f = new RandomQuery(random.nextLong(), random.nextFloat(), docValues); int queryType = random.nextInt(2); if (queryType == 0) { hits = s.search(new ConstantScoreQuery(f), hitCount, sort, random.nextBoolean(), random.nextBoolean()); } else { hits = s.search(f, hitCount, sort, random.nextBoolean(), random.nextBoolean()); } if (VERBOSE) { System.out.println("\nTEST: iter=" + iter + " " + hits.totalHits + " hits; topN=" + hitCount + "; reverse=" + reverse + "; sortMissingLast=" + sortMissingLast + " sort=" + sort); } // Compute expected results: Collections.sort(f.matchValues, new Comparator<BytesRef>() { @Override public int compare(BytesRef a, BytesRef b) { if (a == null) { if (b == null) { return 0; } if (sortMissingLast) { return 1; } else { return -1; } } else if (b == null) { if (sortMissingLast) { return -1; } else { return 1; } } else { return a.compareTo(b); } } }); if (reverse) { Collections.reverse(f.matchValues); } final List<BytesRef> expected = f.matchValues; if (VERBOSE) { System.out.println(" expected:"); for (int idx = 0; idx < expected.size(); idx++) { BytesRef br = expected.get(idx); if (br == null && missingIsNull == false) { br = new BytesRef(); } System.out.println(" " + idx + ": " + (br == null ? "<missing>" : br.utf8ToString())); if (idx == hitCount - 1) { break; } } } if (VERBOSE) { System.out.println(" actual:"); for (int hitIDX = 0; hitIDX < hits.scoreDocs.length; hitIDX++) { final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX]; BytesRef br = (BytesRef) fd.fields[0]; System.out.println(" " + hitIDX + ": " + (br == null ? "<missing>" : br.utf8ToString()) + " id=" + s.doc(fd.doc).get("id")); } } for (int hitIDX = 0; hitIDX < hits.scoreDocs.length; hitIDX++) { final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX]; BytesRef br = expected.get(hitIDX); if (br == null && missingIsNull == false) { br = new BytesRef(); } // Normally, the old codecs (that don't support // docsWithField via doc values) will always return // an empty BytesRef for the missing case; however, // if all docs in a given segment were missing, in // that case it will return null! So we must map // null here, too: BytesRef br2 = (BytesRef) fd.fields[0]; if (br2 == null && missingIsNull == false) { br2 = new BytesRef(); } assertEquals(br, br2); } } r.close(); dir.close(); }
From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java
License:Apache License
private static void writeMissingValue(StreamOutput out, Object missingValue) throws IOException { if (missingValue == SortField.STRING_FIRST) { out.writeByte((byte) 1); } else if (missingValue == SortField.STRING_LAST) { out.writeByte((byte) 2); } else {// w ww. ja v a 2 s. c o m out.writeByte((byte) 0); out.writeGenericValue(missingValue); } }
From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java
License:Apache License
private static Object readMissingValue(StreamInput in) throws IOException { final byte id = in.readByte(); switch (id) { case 0://ww w. j av a2 s . c o m return in.readGenericValue(); case 1: return SortField.STRING_FIRST; case 2: return SortField.STRING_LAST; default: throw new IOException("Unknown missing value id: " + id); } }
From source file:org.codelibs.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource.java
License:Apache License
@Override public Object missingValue(boolean reversed) { if (sortMissingFirst(missingValue) || sortMissingLast(missingValue)) { if (sortMissingLast(missingValue) ^ reversed) { return SortField.STRING_LAST; } else {// w w w. j av a 2 s.c o m return SortField.STRING_FIRST; } } // otherwise we fill missing values ourselves return null; }
From source file:org.elasticsearch.index.IndexSortIT.java
License:Apache License
public void testIndexSort() { SortField dateSort = new SortedNumericSortField("date", SortField.Type.LONG, false); dateSort.setMissingValue(Long.MAX_VALUE); SortField numericSort = new SortedNumericSortField("numeric_dv", SortField.Type.LONG, false); numericSort.setMissingValue(Long.MAX_VALUE); SortField keywordSort = new SortedSetSortField("keyword_dv", false); keywordSort.setMissingValue(SortField.STRING_LAST); Sort indexSort = new Sort(dateSort, numericSort, keywordSort); prepareCreate("test") .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", "1") .put("index.number_of_replicas", "1") .putList("index.sort.field", "date", "numeric_dv", "keyword_dv")) .addMapping("test", TEST_MAPPING).get(); for (int i = 0; i < 20; i++) { client().prepareIndex("test", "test", Integer.toString(i)) .setSource("numeric_dv", randomInt(), "keyword_dv", randomAlphaOfLengthBetween(10, 20)).get(); }/*from w w w . j av a2 s . c om*/ flushAndRefresh(); ensureYellow(); assertSortedSegments("test", indexSort); }