use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestPointFields method callAndCheckCreateFields.
/**
* Calls {@link SchemaField#createFields} on the specified value for the specified field name, and asserts
* that the results match the SchemaField propeties, with an additional check that the <code>pointType</code>
* is included if and only if the SchemaField is "indexed"
*/
private List<IndexableField> callAndCheckCreateFields(final String fieldName, final Class<?> pointType, final Object value) throws Exception {
final SchemaField sf = h.getCore().getLatestSchema().getField(fieldName);
final List<IndexableField> results = sf.createFields(value);
final Set<IndexableField> resultSet = new LinkedHashSet<>(results);
assertEquals("duplicates found in results? " + results.toString(), results.size(), resultSet.size());
final Set<Class<?>> resultClasses = new HashSet<>();
for (IndexableField f : results) {
resultClasses.add(f.getClass());
if (!sf.hasDocValues()) {
assertFalse(f.toString(), (f instanceof NumericDocValuesField) || (f instanceof SortedNumericDocValuesField));
}
}
assertEquals(fieldName + " stored? Result Fields: " + Arrays.toString(results.toArray()), sf.stored(), resultClasses.contains(StoredField.class));
assertEquals(fieldName + " indexed? Result Fields: " + Arrays.toString(results.toArray()), sf.indexed(), resultClasses.contains(pointType));
if (sf.multiValued()) {
assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()), sf.hasDocValues(), resultClasses.contains(SortedNumericDocValuesField.class));
} else {
assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()), sf.hasDocValues(), resultClasses.contains(NumericDocValuesField.class));
}
return results;
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestDuelingCodecs method createRandomIndex.
/**
* populates a writer with random stuff. this must be fully reproducable with the seed!
*/
public static void createRandomIndex(int numdocs, RandomIndexWriter writer, long seed) throws IOException {
Random random = new Random(seed);
// primary source for our data is from linefiledocs, it's realistic.
LineFileDocs lineFileDocs = new LineFileDocs(random);
// because linefiledocs doesn't cover all the possibilities.
for (int i = 0; i < numdocs; i++) {
Document document = lineFileDocs.nextDoc();
// grab the title and add some SortedSet instances for fun
String title = document.get("titleTokenized");
String[] split = title.split("\\s+");
document.removeFields("sortedset");
for (String trash : split) {
document.add(new SortedSetDocValuesField("sortedset", new BytesRef(trash)));
}
// add a numeric dv field sometimes
document.removeFields("sparsenumeric");
if (random.nextInt(4) == 2) {
document.add(new NumericDocValuesField("sparsenumeric", random.nextInt()));
}
// add sortednumeric sometimes
document.removeFields("sparsesortednum");
if (random.nextInt(5) == 1) {
document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
if (random.nextBoolean()) {
document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
}
}
writer.addDocument(document);
}
lineFileDocs.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestSortedNumericSortField method testForward.
public void testForward() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 5));
doc.add(newStringField("id", "2", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 3));
doc.add(new SortedNumericDocValuesField("value", 7));
doc.add(newStringField("id", "1", Field.Store.YES));
writer.addDocument(doc);
IndexReader ir = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(ir);
Sort sort = new Sort(new SortedNumericSortField("value", SortField.Type.INT));
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(2, td.totalHits);
// 3 comes before 5
assertEquals("1", searcher.doc(td.scoreDocs[0].doc).get("id"));
assertEquals("2", searcher.doc(td.scoreDocs[1].doc).get("id"));
ir.close();
dir.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestSortedNumericSortField method testMissingFirst.
public void testMissingFirst() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 5));
doc.add(newStringField("id", "2", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 3));
doc.add(new SortedNumericDocValuesField("value", 7));
doc.add(newStringField("id", "1", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "3", Field.Store.YES));
writer.addDocument(doc);
IndexReader ir = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(ir);
SortField sortField = new SortedNumericSortField("value", SortField.Type.INT);
sortField.setMissingValue(Integer.MIN_VALUE);
Sort sort = new Sort(sortField);
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(3, td.totalHits);
// 3 comes before 5
// null comes first
assertEquals("3", searcher.doc(td.scoreDocs[0].doc).get("id"));
assertEquals("1", searcher.doc(td.scoreDocs[1].doc).get("id"));
assertEquals("2", searcher.doc(td.scoreDocs[2].doc).get("id"));
ir.close();
dir.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestSortedNumericSortField method testDouble.
public void testDouble() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(-3d)));
doc.add(newStringField("id", "2", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(-5d)));
doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(7d)));
doc.add(newStringField("id", "1", Field.Store.YES));
writer.addDocument(doc);
IndexReader ir = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(ir);
Sort sort = new Sort(new SortedNumericSortField("value", SortField.Type.DOUBLE));
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(2, td.totalHits);
// -5 comes before -3
assertEquals("1", searcher.doc(td.scoreDocs[0].doc).get("id"));
assertEquals("2", searcher.doc(td.scoreDocs[1].doc).get("id"));
ir.close();
dir.close();
}
Aggregations