public void testGetParentId() throws Exception { ParentFieldMapper fieldMapper = createParentFieldMapper(); Directory directory = newDirectory(); IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig()); Document document = new Document(); document.add(new SortedDocValuesField(fieldMapper.fieldType().name(), new BytesRef("1"))); indexWriter.addDocument(document); indexWriter.close(); IndexReader indexReader = DirectoryReader.open(directory); String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0); assertEquals("1", id); indexReader.close(); directory.close(); }
public void testGetParentIdNoParentField() throws Exception { ParentFieldMapper fieldMapper = createParentFieldMapper(); Directory directory = newDirectory(); IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig()); Document document = new Document(); document.add(new SortedDocValuesField("different_field", new BytesRef("1"))); indexWriter.addDocument(document); indexWriter.close(); IndexReader indexReader = DirectoryReader.open(directory); String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0); assertNull(id); indexReader.close(); directory.close(); }
public void testSortValues() throws Exception { Directory dir = new RAMDirectory(); IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); for (int i = 0; i < 10; i++) { Document document = new Document(); String text = new String(new char[]{(char) (97 + i), (char) (97 + i)}); document.add(new TextField("str", text, Field.Store.YES)); document.add(new SortedDocValuesField("str", new BytesRef(text))); indexWriter.addDocument(document); } IndexReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(indexWriter)); IndexSearcher searcher = new IndexSearcher(reader); TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("str", SortField.Type.STRING))); for (int i = 0; i < 10; i++) { FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[i]; assertThat((BytesRef) fieldDoc.fields[0], equalTo(new BytesRef(new String(new char[]{(char) (97 + i), (char) (97 + i)})))); } }
void addToDoc(Document doc, String... values){ Preconditions.checkArgument(valueType == String.class); if (isSorted()) { Preconditions.checkArgument(values.length < 2, "sorted fields cannot have multiple values"); } // add distinct elements to doc final Iterable<String> nonNull = FluentIterable.from(Arrays.asList(values)) .filter(new Predicate<String>() { @Override public boolean apply(@Nullable final String input) { return input != null; } }); for (final String value : ImmutableSet.copyOf(nonNull)) { final String truncatedValue = StringUtils.abbreviate(value, MAX_STRING_LENGTH); doc.add(new StringField(indexFieldName, truncatedValue, stored ? Store.YES : Store.NO)); } if (isSorted() && values.length == 1) { Preconditions.checkArgument(sortedValueType == SearchFieldSorting.FieldType.STRING); doc.add(new SortedDocValuesField(indexFieldName, new BytesRef(values[0]))); } }
void addToDoc(Document doc, byte[]... values){ Preconditions.checkArgument(valueType == String.class); if (isSorted()) { Preconditions.checkArgument(values.length < 2, "sorted fields cannot have multiple values"); } // add distinct elements to doc final Iterable<byte[]> nonNull = FluentIterable.from(Arrays.asList(values)) .filter(new Predicate<byte[]>() { @Override public boolean apply(@Nullable final byte[] input) { return input != null; } }); for (final byte[] value : ImmutableSet.copyOf(nonNull)) { final BytesRef truncatedValue = new BytesRef(value,0, Math.min(value.length, MAX_STRING_LENGTH)); doc.add(new StringField(indexFieldName, truncatedValue, stored ? Store.YES : Store.NO)); } if (isSorted() && values.length == 1) { Preconditions.checkArgument(sortedValueType == SearchFieldSorting.FieldType.STRING); doc.add(new SortedDocValuesField(indexFieldName, new BytesRef(values[0]))); } }
@Override public void run() { try { for (int i = 0; i < 10000; ++i) { final Document document = new Document(); final String key = "key" + i; final String val = "value" + i; document.add(new StringField(key, val, Field.Store.YES)); document.add(new SortedDocValuesField(key, new BytesRef(val.getBytes()))); index.add(document); data.put(key, val); sleep(1); } } catch (InterruptedException e) { } }
@Override public Document transform(final Example input) throws TransformException { final Document doc = new Document(); doc.add(new Field(ExampleField.ID.getName(), input.getId(), StringField.TYPE_STORED)); doc.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef(input.getId()))); doc.add(new Field(ExampleField.TITLE.getName(), input.getTitle(), TextField.TYPE_STORED)); doc.add(new Field(ExampleField.BODY.getName(), input.getBody(), TextField.TYPE_STORED)); doc.add(new Field(ExampleField.COLOR.getName(), input.getColor(), StringField.TYPE_STORED)); doc.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), input.getColor())); final Date createDate = input.getCreateDate(); doc.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate.getTime())); doc.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate.getTime())); return doc; }
@BeforeClass public static void beforeClassTestExplanations() throws Exception { 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(newStringField(KEY, ""+i, Field.Store.NO)); doc.add(new SortedDocValuesField(KEY, new BytesRef(""+i))); Field f = newTextField(FIELD, docFields[i], Field.Store.NO); f.setBoost(i); doc.add(f); doc.add(newTextField(ALTFIELD, docFields[i], Field.Store.NO)); writer.addDocument(doc); } reader = writer.getReader(); writer.close(); searcher = newSearcher(reader); }
public void testHasUncommittedChangesAfterException() throws IOException { Analyzer analyzer = new MockAnalyzer(random()); assumeTrue("requires doc values", defaultCodecSupportsDocValues()); Directory directory = newDirectory(); // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!! IndexWriterConfig iwc = newIndexWriterConfig(analyzer); iwc.setMergePolicy(newLogMergePolicy()); IndexWriter iwriter = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo!"))); doc.add(new SortedDocValuesField("dv", new BytesRef("bar!"))); try { iwriter.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException expected) { // expected } iwriter.commit(); assertFalse(iwriter.hasUncommittedChanges()); iwriter.close(); directory.close(); }
public void testDifferentTypedDocValuesField2() throws Exception { Directory d = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), d); Document doc = new Document(); doc.add(new NumericDocValuesField("field", 17)); w.addDocument(doc); // Index doc values are single-valued so we should not // be able to add same field more than once: doc.add(new SortedDocValuesField("field", new BytesRef("hello"))); try { w.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException iae) { // expected } DirectoryReader r = w.getReader(); assertEquals(17, getOnlySegmentReader(r).getNumericDocValues("field").get(0)); r.close(); w.close(); d.close(); }
public void testMixedTypesSameDocument() throws Exception { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))); w.addDocument(new Document()); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", 0)); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException iae) { // expected } IndexReader ir = w.getReader(); assertEquals(1, ir.numDocs()); ir.close(); w.close(); dir.close(); }
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); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException iae) { // expected } IndexReader ir = w.getReader(); assertEquals(1, ir.numDocs()); ir.close(); w.close(); dir.close(); }
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(); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); fail("did not get expected exception"); } catch (IllegalArgumentException iae) { // expected } w.close(); dir.close(); }
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(); w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); fail("did not get expected exception"); } catch (IllegalArgumentException iae) { // expected } w.close(); dir.close(); }
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); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } IndexReader ir = writer.getReader(); assertEquals(1, ir.numDocs()); ir.close(); writer.close(); dir.close(); }
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())); writer = new IndexWriter(dir, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir.close(); }
public void testTypeChangeAfterCloseAndDeleteAll() 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())); writer = new IndexWriter(dir, conf); writer.deleteAll(); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); writer.close(); dir.close(); }
public void testTypeChangeAfterOpenCreate() 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())); conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE); writer = new IndexWriter(dir, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); writer.close(); dir.close(); }
public void testTypeChangeViaAddIndexes() 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())); writer = new IndexWriter(dir2, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); try { writer.addIndexes(dir); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir.close(); dir2.close(); }
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())); writer = new IndexWriter(dir2, conf); writer.addIndexes(dir); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir2.close(); dir.close(); }
@Override public List<IndexableField> createFields(SchemaField field, Object value, float boost) { if (field.hasDocValues()) { List<IndexableField> fields = new ArrayList<>(); fields.add(createField(field, value, boost)); final BytesRef bytes = getCollationKey(field.getName(), value.toString()); if (field.multiValued()) { fields.add(new SortedSetDocValuesField(field.getName(), bytes)); } else { fields.add(new SortedDocValuesField(field.getName(), bytes)); } return fields; } else { return Collections.singletonList(createField(field, value, boost)); } }
@Override public List<IndexableField> createFields(SchemaField field, Object value, float boost) { if (field.hasDocValues()) { List<IndexableField> fields = new ArrayList<>(); fields.add(createField(field, value, boost)); final BytesRef bytes = new BytesRef(value.toString()); if (field.multiValued()) { fields.add(new SortedSetDocValuesField(field.getName(), bytes)); } else { fields.add(new SortedDocValuesField(field.getName(), bytes)); } return fields; } else { return Collections.singletonList(createField(field, value, boost)); } }
@Override public List<IndexableField> createFields(SchemaField field, Object value, float boost) { if (field.hasDocValues()) { List<IndexableField> fields = new ArrayList<>(); IndexableField storedField = createField(field, value, boost); fields.add(storedField); ByteBuffer byteBuffer = toObject(storedField); BytesRef bytes = new BytesRef (byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining()); if (field.multiValued()) { fields.add(new SortedSetDocValuesField(field.getName(), bytes)); } else { fields.add(new SortedDocValuesField(field.getName(), bytes)); } return fields; } else { return Collections.singletonList(createField(field, value, boost)); } }
public void testDifferentTypedDocValuesField2() throws Exception { Directory d = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), d); Document doc = new Document(); // Index doc values are single-valued so we should not // be able to add same field more than once: Field f = new NumericDocValuesField("field", 17); doc.add(f); doc.add(new SortedDocValuesField("field", new BytesRef("hello"))); try { w.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException iae) { // expected } doc = new Document(); doc.add(f); w.addDocument(doc); w.forceMerge(1); DirectoryReader r = w.getReader(); assertEquals(17, getOnlySegmentReader(r).getNumericDocValues("field").get(0)); r.close(); w.close(); d.close(); }
public void testMixedTypesDifferentDocuments() throws Exception { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", 0)); w.addDocument(doc); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); } catch (IllegalArgumentException iae) { // expected } w.close(); dir.close(); }
public void testAddSortedTwice() throws IOException { Analyzer analyzer = new MockAnalyzer(random()); Directory directory = newDirectory(); // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1 IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer); iwc.setMergePolicy(newLogMergePolicy()); IndexWriter iwriter = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo!"))); doc.add(new SortedDocValuesField("dv", new BytesRef("bar!"))); try { iwriter.addDocument(doc); fail("didn't hit expected exception"); } catch (IllegalArgumentException expected) { // expected } iwriter.close(); directory.close(); }
public void testTooLargeSortedBytes() throws IOException { Analyzer analyzer = new MockAnalyzer(random()); Directory directory = newDirectory(); // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1 IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer); iwc.setMergePolicy(newLogMergePolicy()); IndexWriter iwriter = new IndexWriter(directory, iwc); Document doc = new Document(); byte bytes[] = new byte[100000]; BytesRef b = new BytesRef(bytes); random().nextBytes(bytes); doc.add(new SortedDocValuesField("dv", b)); try { iwriter.addDocument(doc); fail("did not get expected exception"); } catch (IllegalArgumentException expected) { // expected } iwriter.close(); directory.close(); }
public void testMixedTypesDifferentSegments() throws Exception { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", 0)); w.addDocument(doc); w.commit(); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); try { w.addDocument(doc); } catch (IllegalArgumentException iae) { // expected } w.close(); dir.close(); }
public void testMixedTypesAfterReopenCreate() throws Exception { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", 0)); w.addDocument(doc); w.close(); IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE); w = new IndexWriter(dir, iwc); doc = new Document(); doc.add(new SortedDocValuesField("foo", new BytesRef("hello"))); w.addDocument(doc); w.close(); dir.close(); }
public void testIllegalTypeChange() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); IndexWriter writer = new IndexWriter(dir, conf); Document doc = new Document(); doc.add(new NumericDocValuesField("dv", 0L)); writer.addDocument(doc); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir.close(); }
public void testIllegalTypeChangeAcrossSegments() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); IndexWriter writer = new IndexWriter(dir, conf); Document doc = new Document(); doc.add(new NumericDocValuesField("dv", 0L)); writer.addDocument(doc); writer.close(); writer = new IndexWriter(dir, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir.close(); }
public void testTypeChangeAfterCloseAndDeleteAll() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); IndexWriter writer = new IndexWriter(dir, conf); Document doc = new Document(); doc.add(new NumericDocValuesField("dv", 0L)); writer.addDocument(doc); writer.close(); writer = new IndexWriter(dir, conf); writer.deleteAll(); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); writer.close(); dir.close(); }
public void testTypeChangeAfterOpenCreate() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, 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.setOpenMode(IndexWriterConfig.OpenMode.CREATE); writer = new IndexWriter(dir, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); writer.close(); dir.close(); }
public void testTypeChangeViaAddIndexes() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, 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(); writer = new IndexWriter(dir2, conf); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); writer.addDocument(doc); try { writer.addIndexes(dir); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir.close(); dir2.close(); }
public void testTypeChangeViaAddIndexes2() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, 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(); writer = new IndexWriter(dir2, conf); writer.addIndexes(dir); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir2.close(); dir.close(); }
public void testTypeChangeViaAddIndexesIR2() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, 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(); writer = new IndexWriter(dir2, conf); IndexReader[] readers = new IndexReader[] {DirectoryReader.open(dir)}; writer.addIndexes(readers); readers[0].close(); doc = new Document(); doc.add(new SortedDocValuesField("dv", new BytesRef("foo"))); try { writer.addDocument(doc); fail("did not hit exception"); } catch (IllegalArgumentException iae) { // expected } writer.close(); dir2.close(); dir.close(); }
@Override public List<IndexableField> createFields(SchemaField field, Object value, float boost) { if (field.hasDocValues()) { List<IndexableField> fields = new ArrayList<IndexableField>(); fields.add(createField(field, value, boost)); final BytesRef bytes = new BytesRef(value.toString()); if (field.multiValued()) { fields.add(new SortedSetDocValuesField(field.getName(), bytes)); } else { fields.add(new SortedDocValuesField(field.getName(), bytes)); } return fields; } else { return Collections.singletonList(createField(field, value, boost)); } }
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { final String value; if (context.externalValueSet()) { value = context.externalValue().toString(); } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { value = fieldType().nullValueAsString(); } else { value = parser.textOrNull(); } } if (value == null) { return; } RawCollationKey key = collator.getRawCollationKey(value, null); final BytesRef binaryValue = new BytesRef(key.bytes, 0, key.size); if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { Field field = new Field(fieldType().name(), binaryValue, fieldType()); fields.add(field); } if (fieldType().hasDocValues()) { fields.add(new SortedDocValuesField(fieldType().name(), binaryValue)); } }
@Test public void testDocValuesFormat() throws IOException { RAMDirectory directory = new RAMDirectory(); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new WhitespaceAnalyzer(Version.LUCENE_43)); conf.setCodec(new Blur024Codec()); IndexWriter writer = new IndexWriter(directory, conf); Document doc = new Document(); doc.add(new StringField("f", "v", Store.YES)); doc.add(new SortedDocValuesField("f", new BytesRef("v"))); writer.addDocument(doc); writer.close(); DirectoryReader reader = DirectoryReader.open(directory); AtomicReaderContext context = reader.leaves().get(0); AtomicReader atomicReader = context.reader(); SortedDocValues sortedDocValues = atomicReader.getSortedDocValues("f"); assertTrue(sortedDocValues.getClass().getName().startsWith(DiskDocValuesProducer.class.getName())); reader.close(); }