Java Code Examples for org.apache.lucene.document.SortedNumericDocValuesField

The following examples show how to use org.apache.lucene.document.SortedNumericDocValuesField. These examples are extracted from open source projects. 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 Project: Elasticsearch   Source File: BooleanFieldMapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
        return;
    }

    Boolean value = context.parseExternalValue(Boolean.class);
    if (value == null) {
        XContentParser.Token token = context.parser().currentToken();
        if (token == XContentParser.Token.VALUE_NULL) {
            if (fieldType().nullValue() != null) {
                value = fieldType().nullValue();
            }
        } else {
            value = context.parser().booleanValue();
        }
    }

    if (value == null) {
        return;
    }
    fields.add(new Field(fieldType().names().indexName(), value ? "T" : "F", fieldType()));
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().names().indexName(), value ? 1 : 0));
    }
}
 
Example 2
Source Project: lucene-solr   Source File: DocValuesAdapterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void createIndex() throws IOException {
  indexDir = createTempDir("testIndex");

  Directory dir = newFSDirectory(indexDir);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockAnalyzer(random()));

  Document doc = new Document();
  doc.add(new BinaryDocValuesField("dv_binary", new BytesRef("lucene")));
  doc.add(new SortedDocValuesField("dv_sorted", new BytesRef("abc")));
  doc.add(new SortedSetDocValuesField("dv_sortedset", new BytesRef("python")));
  doc.add(new SortedSetDocValuesField("dv_sortedset", new BytesRef("java")));
  doc.add(new NumericDocValuesField("dv_numeric", 42L));
  doc.add(new SortedNumericDocValuesField("dv_sortednumeric", 22L));
  doc.add(new SortedNumericDocValuesField("dv_sortednumeric", 11L));
  doc.add(newStringField("no_dv", "aaa", Field.Store.NO));
  writer.addDocument(doc);

  writer.commit();
  writer.close();
  dir.close();
}
 
Example 3
/**
 * Test that the index sort optimization is not activated when some documents
 * have multiple values.
 */
public void testMultiDocValues() throws Exception {
  Directory dir = newDirectory();

  IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
  Sort indexSort = new Sort(new SortedNumericSortField("field", SortField.Type.LONG));
  iwc.setIndexSort(indexSort);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);

  Document doc = new Document();
  doc.add(new SortedNumericDocValuesField("field", 0));
  doc.add(new SortedNumericDocValuesField("field", 10));
  writer.addDocument(doc);

  testIndexSortOptimizationDeactivated(writer);

  writer.close();
  dir.close();
}
 
Example 4
Source Project: lucene-solr   Source File: BaseDocValuesFormatTestCase.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void addRandomFields(Document doc) {
  if (usually()) {
    doc.add(new NumericDocValuesField("ndv", random().nextInt(1 << 12)));
    doc.add(new BinaryDocValuesField("bdv", new BytesRef(TestUtil.randomSimpleString(random()))));
    doc.add(new SortedDocValuesField("sdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
  }
  int numValues = random().nextInt(5);
  for (int i = 0; i < numValues; ++i) {
    doc.add(new SortedSetDocValuesField("ssdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
  }
  numValues = random().nextInt(5);
  for (int i = 0; i < numValues; ++i) {
    doc.add(new SortedNumericDocValuesField("sndv", TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE)));
  }
}
 
Example 5
Source Project: lucene-solr   Source File: TestDocValuesQueries.java    License: Apache License 2.0 6 votes vote down vote up
public void testToString() {
  Query q1 = SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5);
  assertEquals("foo:[3 TO 5]", q1.toString());
  assertEquals("[3 TO 5]", q1.toString("foo"));
  assertEquals("foo:[3 TO 5]", q1.toString("bar"));

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  assertEquals("foo:[[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, true);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, false);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), null, true, true);
  assertEquals("foo:[[62 61 72] TO *}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", null, new BytesRef("baz"), true, true);
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString());
  assertEquals("{* TO [62 61 7a]]", q2.toString("foo"));
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString("bar"));
}
 
Example 6
Source Project: lucene-solr   Source File: TestDocValuesQueries.java    License: Apache License 2.0 6 votes vote down vote up
public void testMissingField() throws IOException {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  iw.addDocument(new Document());
  IndexReader reader = iw.getReader();
  iw.close();
  IndexSearcher searcher = newSearcher(reader);
  for (Query query : Arrays.asList(
      NumericDocValuesField.newSlowRangeQuery("foo", 2, 4),
      SortedNumericDocValuesField.newSlowRangeQuery("foo", 2, 4),
      SortedDocValuesField.newSlowRangeQuery("foo", new BytesRef("abc"), new BytesRef("bcd"), random().nextBoolean(), random().nextBoolean()),
      SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("abc"), new BytesRef("bcd"), random().nextBoolean(), random().nextBoolean()))) {
    Weight w = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE, 1);
    assertNull(w.scorer(searcher.getIndexReader().leaves().get(0)));
  }
  reader.close();
  dir.close();
}
 
Example 7
Source Project: lucene-solr   Source File: EnumFieldType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<IndexableField> createFields(SchemaField sf, Object value) {
  if ( ! sf.hasDocValues()) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, 
        getClass().getSimpleName() + " requires docValues=\"true\".");
  }
  final IndexableField field = createField(sf, value);
  final List<IndexableField> fields = new ArrayList<>();
  fields.add(field);
  final long longValue = field.numericValue().longValue();
  if (sf.multiValued()) {
    fields.add(new SortedNumericDocValuesField(sf.getName(), longValue));
  } else {
    fields.add(new NumericDocValuesField(sf.getName(), longValue));
  }
  return fields;
}
 
Example 8
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new FloatPoint(name, value.floatValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name,
            NumericUtils.floatToSortableInt(value.floatValue())));
    }
    if (stored) {
        fields.add(new StoredField(name, value.floatValue()));
    }
    return fields;
}
 
Example 9
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new DoublePoint(name, value.doubleValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name,
            NumericUtils.doubleToSortableLong(value.doubleValue())));
    }
    if (stored) {
        fields.add(new StoredField(name, value.doubleValue()));
    }
    return fields;
}
 
Example 10
Source Project: Elasticsearch   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
protected final void addDocValue(ParseContext context, List<Field> fields, long value) {
    if (useSortedNumericDocValues) {
        fields.add(new SortedNumericDocValuesField(fieldType().names().indexName(), value));
    } else {
        CustomLongNumericDocValuesField field = (CustomLongNumericDocValuesField) context.doc().getByKey(fieldType().names().indexName());
        if (field != null) {
            field.add(value);
        } else {
            field = new CustomLongNumericDocValuesField(fieldType().names().indexName(), value);
            context.doc().addWithKey(fieldType().names().indexName(), field);
        }
    }
}
 
Example 11
Source Project: lucene-solr   Source File: TestMemoryIndex.java    License: Apache License 2.0 5 votes vote down vote up
public void testDocValues_resetIterator() throws Exception {
  Document doc = new Document();

  doc.add(new SortedSetDocValuesField("sorted_set", new BytesRef("f")));
  doc.add(new SortedSetDocValuesField("sorted_set", new BytesRef("d")));
  doc.add(new SortedSetDocValuesField("sorted_set", new BytesRef("d")));
  doc.add(new SortedSetDocValuesField("sorted_set", new BytesRef("c")));

  doc.add(new SortedNumericDocValuesField("sorted_numeric", 33L));
  doc.add(new SortedNumericDocValuesField("sorted_numeric", 32L));
  doc.add(new SortedNumericDocValuesField("sorted_numeric", 32L));
  doc.add(new SortedNumericDocValuesField("sorted_numeric", 31L));
  doc.add(new SortedNumericDocValuesField("sorted_numeric", 30L));

  MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
  LeafReader leafReader = mi.createSearcher().getIndexReader().leaves().get(0).reader();

  SortedSetDocValues sortedSetDocValues = leafReader.getSortedSetDocValues("sorted_set");
  assertEquals(3, sortedSetDocValues.getValueCount());
  for (int times = 0; times < 3; times++) {
    assertTrue(sortedSetDocValues.advanceExact(0));
    assertEquals(0L, sortedSetDocValues.nextOrd());
    assertEquals(1L, sortedSetDocValues.nextOrd());
    assertEquals(2L, sortedSetDocValues.nextOrd());
    assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSetDocValues.nextOrd());
  }

  SortedNumericDocValues sortedNumericDocValues = leafReader.getSortedNumericDocValues("sorted_numeric");
  for (int times = 0; times < 3; times++) {
    assertTrue(sortedNumericDocValues.advanceExact(0));
    assertEquals(5, sortedNumericDocValues.docValueCount());
    assertEquals(30L, sortedNumericDocValues.nextValue());
    assertEquals(31L, sortedNumericDocValues.nextValue());
    assertEquals(32L, sortedNumericDocValues.nextValue());
    assertEquals(32L, sortedNumericDocValues.nextValue());
    assertEquals(33L, sortedNumericDocValues.nextValue());
  }

}
 
Example 12
Source Project: lucene-solr   Source File: TestValueSources.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  dir = newDirectory();
  analyzer = new MockAnalyzer(random());
  IndexWriterConfig iwConfig = newIndexWriterConfig(analyzer);
  iwConfig.setMergePolicy(newLogMergePolicy());
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig);
  for (String [] doc : documents) {
    Document document = new Document();
    document.add(new StringField("id", doc[0], Field.Store.NO));
    document.add(new SortedDocValuesField("id", new BytesRef(doc[0])));
    document.add(new NumericDocValuesField("double", Double.doubleToRawLongBits(Double.parseDouble(doc[1]))));
    document.add(new NumericDocValuesField("float", Float.floatToRawIntBits(Float.parseFloat(doc[2]))));
    document.add(new NumericDocValuesField("int", Integer.parseInt(doc[3])));
    document.add(new NumericDocValuesField("long", Long.parseLong(doc[4])));
    document.add(new StringField("string", doc[5], Field.Store.NO));
    document.add(new SortedDocValuesField("string", new BytesRef(doc[5])));
    document.add(new TextField("text", doc[6], Field.Store.NO));
    document.add(new SortedNumericDocValuesField("floatMv", NumericUtils.floatToSortableInt(Float.parseFloat(doc[7]))));
    document.add(new SortedNumericDocValuesField("floatMv", NumericUtils.floatToSortableInt(Float.parseFloat(doc[8]))));
    document.add(new SortedNumericDocValuesField("floatMv", NumericUtils.floatToSortableInt(Float.parseFloat(doc[9]))));
    document.add(new SortedNumericDocValuesField("doubleMv", NumericUtils.doubleToSortableLong(Double.parseDouble(doc[7]))));
    document.add(new SortedNumericDocValuesField("doubleMv", NumericUtils.doubleToSortableLong(Double.parseDouble(doc[8]))));
    document.add(new SortedNumericDocValuesField("doubleMv", NumericUtils.doubleToSortableLong(Double.parseDouble(doc[9]))));
    document.add(new SortedNumericDocValuesField("intMv", Long.parseLong(doc[10])));
    document.add(new SortedNumericDocValuesField("intMv", Long.parseLong(doc[11])));
    document.add(new SortedNumericDocValuesField("intMv", Long.parseLong(doc[12])));
    document.add(new SortedNumericDocValuesField("longMv", Long.parseLong(doc[10])));
    document.add(new SortedNumericDocValuesField("longMv", Long.parseLong(doc[11])));
    document.add(new SortedNumericDocValuesField("longMv", Long.parseLong(doc[12])));
    iw.addDocument(document);
  }
  
  reader = iw.getReader();
  searcher = newSearcher(reader);
  iw.close();
}
 
Example 13
Source Project: lucene-solr   Source File: TestJoinUtil.java    License: Apache License 2.0 5 votes vote down vote up
private void addLinkFields(final Random random, Document document, final String fieldName, String linkValue,
    boolean multipleValuesPerDocument, boolean globalOrdinalJoin) {
  document.add(newTextField(random, fieldName, linkValue, Field.Store.NO));

  final int linkInt = Integer.parseUnsignedInt(linkValue,16);
  document.add(new IntPoint(fieldName + "INT", linkInt));
  document.add(new FloatPoint(fieldName + "FLOAT", linkInt));

  final long linkLong = linkInt<<32 | linkInt;
  document.add(new LongPoint(fieldName + "LONG", linkLong));
  document.add(new DoublePoint(fieldName + "DOUBLE", linkLong));

  if (multipleValuesPerDocument) {
    document.add(new SortedSetDocValuesField(fieldName, new BytesRef(linkValue)));
    document.add(new SortedNumericDocValuesField(fieldName+ "INT", linkInt));
    document.add(new SortedNumericDocValuesField(fieldName+ "FLOAT", Float.floatToRawIntBits(linkInt)));
    document.add(new SortedNumericDocValuesField(fieldName+ "LONG", linkLong));
    document.add(new SortedNumericDocValuesField(fieldName+ "DOUBLE", Double.doubleToRawLongBits(linkLong)));
  } else {
    document.add(new SortedDocValuesField(fieldName, new BytesRef(linkValue)));
    document.add(new NumericDocValuesField(fieldName+ "INT", linkInt));
    document.add(new FloatDocValuesField(fieldName+ "FLOAT", linkInt));
    document.add(new NumericDocValuesField(fieldName+ "LONG", linkLong));
    document.add(new DoubleDocValuesField(fieldName+ "DOUBLE", linkLong));
  }
  if (globalOrdinalJoin) {
    document.add(new SortedDocValuesField("join_field", new BytesRef(linkValue)));
  }
}
 
Example 14
Source Project: lucene-solr   Source File: BaseDocValuesFormatTestCase.java    License: Apache License 2.0 5 votes vote down vote up
public void testSortedNumericMergeAwayAllValuesLargeSegment() throws IOException {
  Directory directory = newDirectory();
  Analyzer analyzer = new MockAnalyzer(random());
  IndexWriterConfig iwconfig = newIndexWriterConfig(analyzer);
  iwconfig.setMergePolicy(newLogMergePolicy());
  RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory, iwconfig);

  Document doc = new Document();
  doc.add(new StringField("id", "1", Field.Store.NO));
  doc.add(new SortedNumericDocValuesField("field", 42L));
  iwriter.addDocument(doc);
  final int numEmptyDocs = atLeast(1024);
  for (int i = 0; i < numEmptyDocs; ++i) {
    iwriter.addDocument(new Document());
  }
  iwriter.commit();
  iwriter.deleteDocuments(new Term("id", "1"));
  iwriter.forceMerge(1);

  DirectoryReader ireader = iwriter.getReader();
  iwriter.close();

  SortedNumericDocValues dv = getOnlyLeafReader(ireader).getSortedNumericDocValues("field");
  assertEquals(NO_MORE_DOCS, dv.nextDoc());

  ireader.close();
  directory.close();
}
 
Example 15
Source Project: lucene-solr   Source File: TestFieldValueQuery.java    License: Apache License 2.0 5 votes vote down vote up
public void testRandom() throws IOException {
  final int iters = atLeast(10);
  for (int iter = 0; iter < iters; ++iter) {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(100);
    for (int i = 0; i < numDocs; ++i) {
      Document doc = new Document();
      final boolean hasValue = random().nextBoolean();
      if (hasValue) {
        doc.add(new NumericDocValuesField("dv1", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 2));
        doc.add(new StringField("has_value", "yes", Store.NO));
      }
      doc.add(new StringField("f", random().nextBoolean() ? "yes" : "no", Store.NO));
      iw.addDocument(doc);
    }
    if (random().nextBoolean()) {
      iw.deleteDocuments(new TermQuery(new Term("f", "no")));
    }
    iw.commit();
    final IndexReader reader = iw.getReader();
    final IndexSearcher searcher = newSearcher(reader);
    iw.close();

    assertSameMatches(searcher, new TermQuery(new Term("has_value", "yes")), new DocValuesFieldExistsQuery("dv1"), false);
    assertSameMatches(searcher, new TermQuery(new Term("has_value", "yes")), new DocValuesFieldExistsQuery("dv2"), false);

    reader.close();
    dir.close();
  }
}
 
Example 16
Source Project: lucene-solr   Source File: TestFieldValueQuery.java    License: Apache License 2.0 5 votes vote down vote up
public void testScore() throws IOException {
  final int iters = atLeast(10);
  for (int iter = 0; iter < iters; ++iter) {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(100);
    for (int i = 0; i < numDocs; ++i) {
      Document doc = new Document();
      final boolean hasValue = random().nextBoolean();
      if (hasValue) {
        doc.add(new NumericDocValuesField("dv1", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 2));
        doc.add(new StringField("has_value", "yes", Store.NO));
      }
      doc.add(new StringField("f", random().nextBoolean() ? "yes" : "no", Store.NO));
      iw.addDocument(doc);
    }
    if (random().nextBoolean()) {
      iw.deleteDocuments(new TermQuery(new Term("f", "no")));
    }
    iw.commit();
    final IndexReader reader = iw.getReader();
    final IndexSearcher searcher = newSearcher(reader);
    iw.close();

    final float boost = random().nextFloat() * 10;
    final Query ref = new BoostQuery(new ConstantScoreQuery(new TermQuery(new Term("has_value", "yes"))), boost);

    final Query q1 = new BoostQuery(new DocValuesFieldExistsQuery("dv1"), boost);
    assertSameMatches(searcher, ref, q1, true);

    final Query q2 = new BoostQuery(new DocValuesFieldExistsQuery("dv2"), boost);
    assertSameMatches(searcher, ref, q2, true);

    reader.close();
    dir.close();
  }
}
 
Example 17
Source Project: lucene-solr   Source File: TestDocValuesQueries.java    License: Apache License 2.0 5 votes vote down vote up
public void testEquals() {
  Query q1 = SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5);
  QueryUtils.checkEqual(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 6));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 4, 5));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("bar", 3, 5));

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  QueryUtils.checkEqual(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("baz"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("bar"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("quux", new BytesRef("bar"), new BytesRef("baz"), true, true));
}
 
Example 18
Source Project: lucene-solr   Source File: TestDocValuesQueries.java    License: Apache License 2.0 5 votes vote down vote up
public void testSortedNumericNPE() throws IOException {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  double[] nums = {-1.7147449030215377E-208, -1.6887024655302576E-11, 1.534911516604164E113, 0.0,
      2.6947996404505155E-166, -2.649722021970773E306, 6.138239235731689E-198, 2.3967090122610808E111};
  for (int i = 0; i < nums.length; ++i) {
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("dv", NumericUtils.doubleToSortableLong(nums[i])));
    iw.addDocument(doc);
  }
  iw.commit();
  final IndexReader reader = iw.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  iw.close();

  final long lo = NumericUtils.doubleToSortableLong(8.701032080293731E-226);
  final long hi = NumericUtils.doubleToSortableLong(2.0801416404385346E-41);
  
  Query query = SortedNumericDocValuesField.newSlowRangeQuery("dv", lo, hi);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);

  // swap order, should still work
  query = SortedNumericDocValuesField.newSlowRangeQuery("dv", hi, lo);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);
  
  reader.close();
  dir.close();
}
 
Example 19
Source Project: lucene-solr   Source File: TestDocValues.java    License: Apache License 2.0 5 votes vote down vote up
/** 
 * field with sortednumeric docvalues
 */
public void testSortedNumericField() throws Exception {
  Directory dir = newDirectory();
  IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(null));
  Document doc = new Document();
  doc.add(new SortedNumericDocValuesField("foo", 3));
  iw.addDocument(doc);
  DirectoryReader dr = DirectoryReader.open(iw);
  LeafReader r = getOnlyLeafReader(dr);
  
  // ok
  assertNotNull(DocValues.getSortedNumeric(r, "foo"));
  
  // errors
  expectThrows(IllegalStateException.class, () -> {
      DocValues.getBinary(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getNumeric(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getSorted(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getSortedSet(r, "foo");
  });
  
  dr.close();
  iw.close();
  dir.close();
}
 
Example 20
Source Project: lucene-solr   Source File: TestExitableDirectoryReader.java    License: Apache License 2.0 5 votes vote down vote up
private void addDVs(Document d1, int i) {
  d1.add(new NumericDocValuesField("numeric", i));
  d1.add(new BinaryDocValuesField("binary", new BytesRef(""+i)));
  d1.add(new SortedDocValuesField("sorted", new BytesRef(""+i)));
  d1.add(new SortedNumericDocValuesField("sortednumeric", i));
  d1.add(new SortedSetDocValuesField("sortedset", new BytesRef(""+i)));
}
 
Example 21
Source Project: lucene-solr   Source File: TestIndexSorting.java    License: Apache License 2.0 5 votes vote down vote up
public void testWrongSortFieldType() throws Exception {
  Directory dir = newDirectory();
  List<Field> dvs = new ArrayList<>();
  dvs.add(new SortedDocValuesField("field", new BytesRef("")));
  dvs.add(new SortedSetDocValuesField("field", new BytesRef("")));
  dvs.add(new NumericDocValuesField("field", 42));
  dvs.add(new SortedNumericDocValuesField("field", 42));

  List<SortField> sortFields = new ArrayList<>();
  sortFields.add(new SortField("field", SortField.Type.STRING));
  sortFields.add(new SortedSetSortField("field", false));
  sortFields.add(new SortField("field", SortField.Type.INT));
  sortFields.add(new SortedNumericSortField("field", SortField.Type.INT));

  for (int i = 0; i < sortFields.size(); i++) {
    for (int j = 0; j < dvs.size(); j++) {
      if (i == j) {
        continue;
      }
      Sort indexSort = new Sort(sortFields.get(i));
      IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
      iwc.setIndexSort(indexSort);
      IndexWriter w = new IndexWriter(dir, iwc);
      Document doc = new Document();
      doc.add(dvs.get(j));
      IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("expected field [field] to be "));
      doc.clear();
      doc.add(dvs.get(i));
      w.addDocument(doc);
      doc.add(dvs.get(j));
      exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("cannot change DocValues type"));
      w.rollback();
      IOUtils.close(w);
    }
  }
  IOUtils.close(dir);
}
 
Example 22
Source Project: lucene-solr   Source File: TestDuelingCodecs.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);

  // TODO: we should add other fields that use things like docs&freqs but omit positions,
  // 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();
}
 
Example 23
Source Project: lucene-solr   Source File: NumericFieldType.java    License: Apache License 2.0 5 votes vote down vote up
public static Query numericDocValuesRangeQuery(
    String field,
    Number lowerValue, Number upperValue,
    boolean lowerInclusive, boolean upperInclusive,
    boolean multiValued) {

  long actualLowerValue = Long.MIN_VALUE;
  if (lowerValue != null) {
    actualLowerValue = lowerValue.longValue();
    if (lowerInclusive == false) {
      if (actualLowerValue == Long.MAX_VALUE) {
        return new MatchNoDocsQuery();
      }
      ++actualLowerValue;
    }
  }

  long actualUpperValue = Long.MAX_VALUE;
  if (upperValue != null) {
    actualUpperValue = upperValue.longValue();
    if (upperInclusive == false) {
      if (actualUpperValue == Long.MIN_VALUE) {
        return new MatchNoDocsQuery();
      }
      --actualUpperValue;
    }
  }
  if (multiValued) {
    // In multiValued case use SortedNumericDocValuesField, this won't work for Trie*Fields wince they use BinaryDV in the multiValue case
    return SortedNumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  } else {
    return NumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  }
}
 
Example 24
Source Project: lucene-solr   Source File: TestPointFields.java    License: Apache License 2.0 5 votes vote down vote up
/** 
 * 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;
}
 
Example 25
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    float l = Float.NEGATIVE_INFINITY;
    float u = Float.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = FloatPoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = FloatPoint.nextDown(u);
        }
    }
    Query query = FloatPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.floatToSortableInt(l),
                NumericUtils.floatToSortableInt(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 26
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    double l = Double.NEGATIVE_INFINITY;
    double u = Double.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = DoublePoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = DoublePoint.nextDown(u);
        }
    }
    Query query = DoublePoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.doubleToSortableLong(l),
                NumericUtils.doubleToSortableLong(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 27
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    int l = Integer.MIN_VALUE;
    int u = Integer.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Integer.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Integer.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = IntPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 28
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new IntPoint(name, value.intValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name, value.intValue()));
    }
    if (stored) {
        fields.add(new StoredField(name, value.intValue()));
    }
    return fields;
}
 
Example 29
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    long l = Long.MIN_VALUE;
    long u = Long.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Long.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Long.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 30
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new LongPoint(name, value.longValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name, value.longValue()));
    }
    if (stored) {
        fields.add(new StoredField(name, value.longValue()));
    }
    return fields;
}