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

The following examples show how to use org.apache.lucene.document.NumericDocValuesField. 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: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testMixedTypesAfterReopenAppend1() throws Exception {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", 0));
  w.addDocument(doc);
  w.close();

  IndexWriter w2 = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("foo", new BytesRef("hello")));
  expectThrows(IllegalArgumentException.class, () -> {
    w2.addDocument(doc2);
  });

  w2.close();
  dir.close();
}
 
Example 2
Source Project: dremio-oss   Source File: SimpleDocumentWriter.java    License: Apache License 2.0 6 votes vote down vote up
void addToDoc(IndexKey key, Integer value){
  Preconditions.checkArgument(key.getValueType() == Integer.class);
  if(value == null){
    return;
  }

  checkIfMultiValueField(key);

  final String indexFieldName = key.getIndexFieldName();
  doc.add(new IntPoint(indexFieldName, value));
  if (key.isStored()) {
    doc.add(new StoredField(indexFieldName, value));
  }
  if (key.isSorted()) {
    Preconditions.checkArgument(key.getSortedValueType() == SearchFieldSorting.FieldType.INTEGER);
    doc.add(new NumericDocValuesField(indexFieldName, value));
  }
}
 
Example 3
Source Project: lucene-solr   Source File: RangeFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** Build the example index. */
public void index() throws IOException {
  IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
      new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));

  // Add documents with a fake timestamp, 1000 sec before
  // "now", 2000 sec before "now", ...:
  for(int i=0;i<100;i++) {
    Document doc = new Document();
    long then = nowSec - i * 1000;
    // Add as doc values field, so we can compute range facets:
    doc.add(new NumericDocValuesField("timestamp", then));
    // Add as numeric field so we can drill-down:
    doc.add(new LongPoint("timestamp", then));
    indexWriter.addDocument(doc);
  }

  // Open near-real-time searcher
  searcher = new IndexSearcher(DirectoryReader.open(indexWriter));
  indexWriter.close();
}
 
Example 4
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 5
Source Project: lucene-solr   Source File: TestFunctionScoreQuery.java    License: Apache License 2.0 6 votes vote down vote up
public void testNaN() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", Double.doubleToLongBits(Double.NaN)));
  w.addDocument(doc);
  IndexReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = newSearcher(reader);
  Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.fromDoubleField("foo"));
  QueryUtils.check(random(), q, searcher);
  Explanation expl = searcher.explain(q, 0);
  assertEquals(0, expl.getValue().doubleValue(), 0f);
  assertTrue(expl.toString(), expl.getDetails()[0].getDescription().contains("NaN is an illegal score"));
  reader.close();
  dir.close();
}
 
Example 6
Source Project: lucene-solr   Source File: TestExpressionSorts.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  int numDocs = atLeast(500);
  for (int i = 0; i < numDocs; i++) {
    Document document = new Document();
    document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO));
    document.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO));
    document.add(new NumericDocValuesField("int", random().nextInt()));
    document.add(new NumericDocValuesField("long", random().nextLong()));
    document.add(new FloatDocValuesField("float", random().nextFloat()));
    document.add(new DoubleDocValuesField("double", random().nextDouble()));
    iw.addDocument(document);
  }
  reader = iw.getReader();
  iw.close();
  searcher = newSearcher(reader);
}
 
Example 7
Source Project: lucene-solr   Source File: BlockGroupingTest.java    License: Apache License 2.0 6 votes vote down vote up
private static List<Document> createRandomBlock(int book) {
  List<Document> block = new ArrayList<>();
  String bookName = "book" + book;
  int chapterCount = atLeast(10);
  for (int j = 0; j < chapterCount; j++) {
    Document doc = new Document();
    String chapterName = "chapter" + j;
    String chapterText = randomText();
    doc.add(new TextField("book", bookName, Field.Store.YES));
    doc.add(new TextField("chapter", chapterName, Field.Store.YES));
    doc.add(new TextField("text", chapterText, Field.Store.NO));
    doc.add(new NumericDocValuesField("length", chapterText.length()));
    doc.add(new SortedDocValuesField("book", new BytesRef(bookName)));
    if (j == chapterCount - 1) {
      doc.add(new TextField("blockEnd", "true", Field.Store.NO));
    }
    block.add(doc);
  }
  return block;
}
 
Example 8
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testTypeChangeViaAddIndexes2() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter writer = new IndexWriter(dir, conf);
  Document doc = new Document();
  doc.add(new NumericDocValuesField("dv", 0L));
  writer.addDocument(doc);
  writer.close();

  Directory dir2 = newDirectory();
  conf = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter writer2 = new IndexWriter(dir2, conf);
  writer2.addIndexes(dir);
  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("dv", new BytesRef("foo")));
  expectThrows(IllegalArgumentException.class, () -> {
    writer2.addDocument(doc2);
  });

  writer2.close();
  dir2.close();
  dir.close();
}
 
Example 9
Source Project: lucene-solr   Source File: TestDocValuesStatsCollector.java    License: Apache License 2.0 6 votes vote down vote up
public void testOneDoc() throws IOException {
  try (Directory dir = newDirectory();
      IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {
    String field = "numeric";
    Document doc = new Document();
    doc.add(new NumericDocValuesField(field, 1));
    doc.add(new StringField("id", "doc1", Store.NO));
    indexWriter.addDocument(doc);

    try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
      IndexSearcher searcher = new IndexSearcher(reader);
      LongDocValuesStats stats = new LongDocValuesStats(field);
      searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));

      assertEquals(1, stats.count());
      assertEquals(0, stats.missing());
      assertEquals(1, stats.max().longValue());
      assertEquals(1, stats.min().longValue());
      assertEquals(1, stats.sum().longValue());
      assertEquals(1, stats.mean(), 0.0001);
      assertEquals(0, stats.variance(), 0.0001);
      assertEquals(0, stats.stdev(), 0.0001);
    }
  }
}
 
Example 10
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testMixedTypesDifferentDocuments() throws Exception {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", 0));
  w.addDocument(doc);

  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("foo", new BytesRef("hello")));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc2);
  });

  IndexReader ir = w.getReader();
  assertEquals(1, ir.numDocs());
  ir.close();
  w.close();
  dir.close();
}
 
Example 11
public void testNoScore() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  for (int i = 0; i < 4; i++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("price", (i+1)));
    doc.add(new FacetField("a", Integer.toString(i % 2)));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  FacetsCollector sfc = new FacetsCollector();
  newSearcher(r).search(new MatchAllDocsQuery(), sfc);
  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, DoubleValuesSource.fromLongField("price"));
  assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 12
private Iterable<IndexableField> getDoc(int i) {
  Document document = new Document();
  document.add(new StringField("test", "test", Store.YES));
  document.add(new StringField("info", "info", Store.YES));
  if (i == 3) {
    document.add(new StringField("shouldnotsee", "shouldnotsee", Store.YES));
  }
  if (i == 5) {
    document.add(new StringField("termmask", "term", Store.YES));
  }
  document.add(new NumericDocValuesField("number", i));
  document.add(new BinaryDocValuesField("bin", new BytesRef(Integer.toString(i).getBytes())));
  document.add(new SortedDocValuesField("sorted", new BytesRef(Integer.toString(i).getBytes())));
  document.add(new SortedSetDocValuesField("sortedset", new BytesRef(Integer.toString(i).getBytes())));
  document.add(new SortedSetDocValuesField("sortedset", new BytesRef(("0" + Integer.toString(i)).getBytes())));
  return document;
}
 
Example 13
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testIllegalTypeChangeAcrossSegments() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter writer = new IndexWriter(dir, conf);
  Document doc = new Document();
  doc.add(new NumericDocValuesField("dv", 0L));
  writer.addDocument(doc);
  writer.close();

  conf = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter writer2 = new IndexWriter(dir, conf);
  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("dv", new BytesRef("foo")));
  expectThrows(IllegalArgumentException.class, () -> {
    writer2.addDocument(doc2);
  });

  writer2.close();
  dir.close();
}
 
Example 14
Source Project: lucene-solr   Source File: TestLongValueFacetCounts.java    License: Apache License 2.0 6 votes vote down vote up
public void testOnlyBigLongs() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  for (long l = 0; l < 3; l++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("field", Long.MAX_VALUE - l));
    w.addDocument(doc);
  }

  IndexReader r = w.getReader();
  w.close();

  FacetsCollector fc = new FacetsCollector();
  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);

  LongValueFacetCounts facets = new LongValueFacetCounts("field", fc, false);

  FacetResult result = facets.getAllChildrenSortByValue();
  assertEquals("dim=field path=[] value=3 childCount=3\n  9223372036854775805 (1)\n  " +
               "9223372036854775806 (1)\n  9223372036854775807 (1)\n",
               result.toString());
  r.close();
  d.close();
}
 
Example 15
public void testSoftDeleteWithTryUpdateDocValue() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig config = newIndexWriterConfig().setSoftDeletesField("soft_delete")
      .setMergePolicy(new SoftDeletesRetentionMergePolicy("soft_delete", MatchAllDocsQuery::new, newLogMergePolicy()));
  IndexWriter writer = new IndexWriter(dir, config);
  SearcherManager sm = new SearcherManager(writer, new SearcherFactory());
  Document d = new Document();
  d.add(new StringField("id", "0", Field.Store.YES));
  writer.addDocument(d);
  sm.maybeRefreshBlocking();
  doUpdate(new Term("id", "0"), writer,
      new NumericDocValuesField("soft_delete", 1), new NumericDocValuesField("other-field", 1));
  sm.maybeRefreshBlocking();
  assertEquals(1, writer.cloneSegmentInfos().size());
  SegmentCommitInfo si = writer.cloneSegmentInfos().info(0);
  assertEquals(1, si.getSoftDelCount());
  assertEquals(1, si.info.maxDoc());
  IOUtils.close(sm, writer, dir);
}
 
Example 16
public void testSingleBigValueCompression() throws IOException {
  try (final Directory dir = new ByteBuffersDirectory()) {
    final IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    final IndexWriter iwriter = new IndexWriter(dir, iwc);

    final Document doc = new Document();
    final NumericDocValuesField dvf = new NumericDocValuesField("dv", 0);
    doc.add(dvf);
    for (int i = 0; i < 20000; ++i) {
      dvf.setLongValue(i & 1023);
      iwriter.addDocument(doc);
    }
    iwriter.forceMerge(1);
    final long size1 = dirSize(dir);
    dvf.setLongValue(Long.MAX_VALUE);
    iwriter.addDocument(doc);
    iwriter.forceMerge(1);
    final long size2 = dirSize(dir);
    // make sure the new value did not grow the bpv for every other value
    assertTrue(size2 < size1 + (20000 * (63 - 10)) / 8);
  }
}
 
Example 17
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 18
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 19
Source Project: lucene-solr   Source File: BaseDocValuesFormatTestCase.java    License: Apache License 2.0 6 votes vote down vote up
public void testRandomAdvanceNumeric() throws IOException {
  final long longRange;
  if (random().nextBoolean()) {
    longRange = TestUtil.nextInt(random(), 1, 1024);
  } else {
    longRange = TestUtil.nextLong(random(), 1, Long.MAX_VALUE);
  }
  doTestRandomAdvance(new FieldCreator() {
      @Override
      public Field next() {
        return new NumericDocValuesField("field", TestUtil.nextLong(random(), 0, longRange));
      }

      @Override
      public DocIdSetIterator iterator(IndexReader r) throws IOException {
        return MultiDocValues.getNumericValues(r, "field");
      }
    });
}
 
Example 20
Source Project: lucene-solr   Source File: TestParallelLeafReader.java    License: Apache License 2.0 6 votes vote down vote up
public void testWithDocValuesUpdates() throws Exception {
  Directory dir1 = newDirectory();
  IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter w1 = new IndexWriter(dir1, iwc1);
  Document d = new Document();
  d.add(newTextField("name", "billy", Field.Store.NO));
  d.add(new NumericDocValuesField("age", 21));
  w1.addDocument(d);
  w1.commit();
  w1.updateNumericDocValue(new Term("name", "billy"), "age", 22);
  w1.close();

  IndexReader r1 = DirectoryReader.open(dir1);
  LeafReader lr = new ParallelLeafReader(false, getOnlyLeafReader(r1));

  NumericDocValues dv = lr.getNumericDocValues("age");
  assertEquals(0, dv.nextDoc());
  assertEquals(22, dv.longValue());

  assertEquals(1, lr.getFieldInfos().fieldInfo("age").getDocValuesGen());

  IOUtils.close(lr, r1, dir1);

}
 
Example 21
Source Project: lucene-solr   Source File: TestLongValuesSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  int numDocs = TestUtil.nextInt(random(), 2049, 4000);
  int leastValue = 45;
  for (int i = 0; i < numDocs; i++) {
    Document document = new Document();
    document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO));
    document.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO));
    document.add(new NumericDocValuesField("int", random().nextInt()));
    document.add(new NumericDocValuesField("long", random().nextLong()));
    if (i == 545)
      document.add(new NumericDocValuesField("onefield", LEAST_LONG_VALUE));
    iw.addDocument(document);
  }
  reader = iw.getReader();
  iw.close();
  searcher = newSearcher(reader);
}
 
Example 22
Source Project: lucene-solr   Source File: TestDoubleValuesSource.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  final int numDocs;
  if (TEST_NIGHTLY) {
    numDocs = TestUtil.nextInt(random(), 2049, 4000);
  } else {
    numDocs = atLeast(546);
  }
  for (int i = 0; i < numDocs; i++) {
    Document document = new Document();
    document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO));
    document.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO));
    document.add(new NumericDocValuesField("int", random().nextInt()));
    document.add(new NumericDocValuesField("long", random().nextLong()));
    document.add(new FloatDocValuesField("float", random().nextFloat()));
    document.add(new DoubleDocValuesField("double", random().nextDouble()));
    if (i == 545)
      document.add(new DoubleDocValuesField("onefield", LEAST_DOUBLE_VALUE));
    iw.addDocument(document);
  }
  reader = iw.getReader();
  iw.close();
  searcher = newSearcher(reader);
}
 
Example 23
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testIllegalTypeChange() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  IndexWriter writer = new IndexWriter(dir, conf);
  Document doc = new Document();
  doc.add(new NumericDocValuesField("dv", 0L));
  writer.addDocument(doc);
  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("dv", new BytesRef("foo")));
  expectThrows(IllegalArgumentException.class, () -> {
    writer.addDocument(doc2);
  });

  IndexReader ir = writer.getReader();
  assertEquals(1, ir.numDocs());
  ir.close();
  writer.close();
  dir.close();
}
 
Example 24
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testMixedTypesDifferentSegments() throws Exception {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", 0));
  w.addDocument(doc);
  w.commit();

  Document doc2 = new Document();
  doc2.add(new SortedDocValuesField("foo", new BytesRef("hello")));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc2);
  });

  w.close();
  dir.close();
}
 
Example 25
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
      newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
  for (int i = 0; i < docFields.length; i++) {
    Document doc = new Document();
    doc.add(newField(FIELD_WITH_OFFSETS, docFields[i], OFFSETS));
    doc.add(newField(FIELD_NO_OFFSETS, docFields[i], TextField.TYPE_STORED));
    doc.add(newField(FIELD_DOCS_ONLY, docFields[i], DOCS));
    doc.add(newField(FIELD_FREQS, docFields[i], DOCS_AND_FREQS));
    doc.add(new IntPoint(FIELD_POINT, 10));
    doc.add(new NumericDocValuesField(FIELD_POINT, 10));
    doc.add(new NumericDocValuesField("id", i));
    doc.add(newField("id", Integer.toString(i), TextField.TYPE_STORED));
    writer.addDocument(doc);
  }
  writer.forceMerge(1);
  reader = writer.getReader();
  writer.close();
  searcher = newSearcher(getOnlyLeafReader(reader));
}
 
Example 26
Source Project: lucene-solr   Source File: TestMixedDocValuesUpdates.java    License: Apache License 2.0 6 votes vote down vote up
public void testUpdateNotExistingFieldDV() throws IOException {
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  try (Directory dir = newDirectory(); IndexWriter writer = new IndexWriter(dir, conf)) {
    Document doc = new Document();
    doc.add(new StringField("id", "1", Store.YES));
    doc.add(new NumericDocValuesField("test", 1));
    writer.addDocument(doc);
    if (random().nextBoolean()) {
      writer.commit();
    }
    writer.updateDocValues(new Term("id", "1"), new NumericDocValuesField("not_existing", 1));

    Document doc1 = new Document();
    doc1.add(new StringField("id", "2", Store.YES));
    doc1.add(new BinaryDocValuesField("not_existing", new BytesRef()));
    IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () ->
        writer.addDocument(doc1)
    );
    assertEquals("cannot change DocValues type from NUMERIC to BINARY for field \"not_existing\"", iae.getMessage());

    iae = expectThrows(IllegalArgumentException.class, () ->
        writer.updateDocValues(new Term("id", "1"), new BinaryDocValuesField("not_existing", new BytesRef()))
    );
    assertEquals("cannot change DocValues type from NUMERIC to BINARY for field \"not_existing\"", iae.getMessage());
  }
}
 
Example 27
Source Project: Elasticsearch   Source File: VersionFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    // see InternalEngine.updateVersion to see where the real version value is set
    final Field version = new NumericDocValuesField(NAME, -1L);
    context.version(version);
    fields.add(version);
}
 
Example 28
Source Project: Elasticsearch   Source File: VersionFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void postParse(ParseContext context) throws IOException {
    // In the case of nested docs, let's fill nested docs with version=1 so that Lucene doesn't write a Bitset for documents
    // that don't have the field. This is consistent with the default value for efficiency.
    for (int i = 1; i < context.docs().size(); i++) {
        final Document doc = context.docs().get(i);
        doc.add(new NumericDocValuesField(NAME, 1L));
    }
}
 
Example 29
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 5 votes vote down vote up
public void testDocValuesUnstored() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwconfig = newIndexWriterConfig(new MockAnalyzer(random()));
  iwconfig.setMergePolicy(newLogMergePolicy());
  IndexWriter writer = new IndexWriter(dir, iwconfig);
  for (int i = 0; i < 50; i++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("dv", i));
    doc.add(new TextField("docId", "" + i, Field.Store.YES));
    writer.addDocument(doc);
  }
  DirectoryReader r = writer.getReader();
  FieldInfos fi = FieldInfos.getMergedFieldInfos(r);
  FieldInfo dvInfo = fi.fieldInfo("dv");
  assertTrue(dvInfo.getDocValuesType() != DocValuesType.NONE);
  NumericDocValues dv = MultiDocValues.getNumericValues(r, "dv");
  for (int i = 0; i < 50; i++) {
    assertEquals(i, dv.nextDoc());
    assertEquals(i, dv.longValue());
    Document d = r.document(i);
    // cannot use d.get("dv") due to another bug!
    assertNull(d.getField("dv"));
    assertEquals(Integer.toString(i), d.get("docId"));
  }
  r.close();
  writer.close();
  dir.close();
}
 
Example 30
Source Project: lucene-solr   Source File: TestTermsEnum.java    License: Apache License 2.0 5 votes vote down vote up
private void addDoc(RandomIndexWriter w, Collection<String> terms, Map<BytesRef,Integer> termToID, int id) throws IOException {
  Document doc = new Document();
  doc.add(new NumericDocValuesField("id", id));
  if (VERBOSE) {
    System.out.println("TEST: addDoc id:" + id + " terms=" + terms);
  }
  for (String s2 : terms) {
    doc.add(newStringField("f", s2, Field.Store.NO));
    termToID.put(new BytesRef(s2), id);
  }
  w.addDocument(doc);
  terms.clear();
}