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

The following examples show how to use org.apache.lucene.document.SortedDocValuesField. 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: TestIndexReaderFunctions.java    License: Apache License 2.0 7 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 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));
    iw.addDocument(document);
  }

  reader = iw.getReader();
  searcher = newSearcher(reader);
  iw.close();
}
 
Example 2
Source Project: dremio-oss   Source File: SimpleDocumentWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void addToDoc(IndexKey key, String... values){
  Preconditions.checkArgument(key.getValueType() == String.class);
  final boolean sorted = key.isSorted();
  if (sorted) {
    checkIfSorted(key, (Object[]) values);
  }

  checkIfMultiValueField(key, (Object[]) values);

  final String indexFieldName = key.getIndexFieldName();
  final Store stored = key.isStored() ? Store.YES : Store.NO;
  for (final String value : values) {
    if (value == null) {
      continue;
    }
    final String truncatedValue = StringUtils.abbreviate(value, MAX_STRING_LENGTH);
    doc.add(new StringField(indexFieldName, truncatedValue, stored));
  }

  if (sorted && values.length == 1 && values[0] != null) {
    Preconditions.checkArgument(key.getSortedValueType() == SearchFieldSorting.FieldType.STRING);
    doc.add(new SortedDocValuesField(indexFieldName, new BytesRef(values[0])));
  }
}
 
Example 3
Source Project: dremio-oss   Source File: SimpleDocumentWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void addToDoc(IndexKey key, byte[]... values){
  Preconditions.checkArgument(key.getValueType() == String.class);
  final boolean sorted = key.isSorted();
  if (sorted) {
    checkIfSorted(key, (Object[]) values);
  }

  checkIfMultiValueField(key, (Object[]) values);

  final String indexFieldName = key.getIndexFieldName();
  final Store stored = key.isStored() ? Store.YES : Store.NO;
  for (final byte[] value : values) {
    if (value == null) {
      continue;
    }
    final BytesRef truncatedValue = new BytesRef(value,0, Math.min(value.length, MAX_STRING_LENGTH));
    doc.add(new StringField(indexFieldName, truncatedValue, stored));
  }

  if (sorted && values.length == 1 && values[0] != null) {
    Preconditions.checkArgument(key.getSortedValueType() == SearchFieldSorting.FieldType.STRING);
    doc.add(new SortedDocValuesField(indexFieldName, new BytesRef(values[0])));
  }
}
 
Example 4
Source Project: dremio-oss   Source File: TestLuceneIndexer.java    License: Apache License 2.0 6 votes vote down vote up
@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) {
  }
}
 
Example 5
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 6
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 7
Source Project: lucene-solr   Source File: GroupingSearchTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testSetAllGroups() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(
      random(),
      dir,
      newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
  Document doc = new Document();
  doc.add(newField("group", "foo", StringField.TYPE_NOT_STORED));
  doc.add(new SortedDocValuesField("group", new BytesRef("foo")));
  w.addDocument(doc);

  IndexSearcher indexSearcher = newSearcher(w.getReader());
  w.close();

  GroupingSearch gs = new GroupingSearch("group");
  gs.setAllGroups(true);
  TopGroups<?> groups = gs.search(indexSearcher, new TermQuery(new Term("group", "foo")), 0, 10);
  assertEquals(1, groups.totalHitCount);
  //assertEquals(1, groups.totalGroupCount.intValue());
  assertEquals(1, groups.totalGroupedHitCount);
  assertEquals(1, gs.getAllMatchingGroups().size());
  indexSearcher.getIndexReader().close();
  dir.close();
}
 
Example 8
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 9
Source Project: lucene-solr   Source File: QueryIndex.java    License: Apache License 2.0 6 votes vote down vote up
private List<Indexable> buildIndexables(List<MonitorQuery> updates) {
  List<Indexable> indexables = new ArrayList<>();
  for (MonitorQuery mq : updates) {
    if (serializer != null && mq.getQueryString() == null) {
      throw new IllegalArgumentException("Cannot add a MonitorQuery with a null string representation to a non-ephemeral Monitor");
    }
    BytesRef serialized = serializer == null ? EMPTY : serializer.serialize(mq);
    for (QueryCacheEntry qce : QueryCacheEntry.decompose(mq, decomposer)) {
      Document doc = presearcher.indexQuery(qce.matchQuery, mq.getMetadata());
      doc.add(new StringField(FIELDS.query_id, qce.queryId, Field.Store.NO));
      doc.add(new SortedDocValuesField(FIELDS.cache_id, new BytesRef(qce.cacheId)));
      doc.add(new SortedDocValuesField(FIELDS.query_id, new BytesRef(qce.queryId)));
      doc.add(new BinaryDocValuesField(FIELDS.mq, serialized));
      indexables.add(new Indexable(qce, doc));
    }
  }
  return indexables;
}
 
Example 10
Source Project: incubator-retired-blur   Source File: Blur024CodecTest.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 11
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 12
Source Project: lucene-solr   Source File: TestIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
public void testHasUncommittedChangesAfterException() throws IOException {
  Analyzer analyzer = new MockAnalyzer(random());

  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!")));
  expectThrows(IllegalArgumentException.class, () -> {
    iwriter.addDocument(doc);
  });

  iwriter.commit();
  assertFalse(iwriter.hasUncommittedChanges());
  iwriter.close();
  directory.close();
}
 
Example 13
Source Project: tutorials   Source File: InMemoryLuceneIndex.java    License: MIT License 6 votes vote down vote up
/**
 * 
 * @param title
 * @param body
 */
public void indexDocument(String title, String body) {

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    try {
        IndexWriter writter = new IndexWriter(memoryIndex, indexWriterConfig);
        Document document = new Document();

        document.add(new TextField("title", title, Field.Store.YES));
        document.add(new TextField("body", body, Field.Store.YES));
        document.add(new SortedDocValuesField("title", new BytesRef(title)));

        writter.addDocument(document);
        writter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
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")));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc);
  });

  DirectoryReader r = w.getReader();
  NumericDocValues values = DocValues.getNumeric(getOnlyLeafReader(r), "field");
  assertEquals(0, values.nextDoc());
  assertEquals(17, values.longValue());
  r.close();
  w.close();
  d.close();
}
 
Example 15
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
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")));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc);
  });

  IndexReader ir = w.getReader();
  assertEquals(1, ir.numDocs());
  ir.close();
  w.close();
  dir.close();
}
 
Example 16
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 17
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
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(analyzer);
  iwc.setMergePolicy(newLogMergePolicy());
  IndexWriter iwriter = new IndexWriter(directory, iwc);
  Document doc = new Document();
  doc.add(new SortedDocValuesField("dv", new BytesRef("foo!")));
  iwriter.addDocument(doc);
  
  doc.add(new SortedDocValuesField("dv", new BytesRef("bar!")));
  expectThrows(IllegalArgumentException.class, () -> {
    iwriter.addDocument(doc);
  });

  IndexReader ir = iwriter.getReader();
  assertEquals(1, ir.numDocs());
  ir.close();
  iwriter.close();
  directory.close();
}
 
Example 18
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 19
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 20
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 21
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 22
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 23
Source Project: lucene-solr   Source File: TestDocValuesIndexing.java    License: Apache License 2.0 6 votes vote down vote up
public void testTypeChangeViaAddIndexesIR2() 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);
  DirectoryReader reader = DirectoryReader.open(dir);
  TestUtil.addIndexesSlowly(writer2, reader);
  reader.close();
  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 24
Source Project: lucene-solr   Source File: ICUCollationField.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<IndexableField> createFields(SchemaField field, Object value) {
  if (field.hasDocValues()) {
    List<IndexableField> fields = new ArrayList<>();
    fields.add(createField(field, value));
    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));
  }
}
 
Example 25
Source Project: lucene-solr   Source File: CollationField.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<IndexableField> createFields(SchemaField field, Object value) {
  if (field.hasDocValues()) {
    List<IndexableField> fields = new ArrayList<>();
    fields.add(createField(field, value));
    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));
  }
}
 
Example 26
Source Project: lucene-solr   Source File: TestSortableTextField.java    License: Apache License 2.0 6 votes vote down vote up
public void testWhiteboxCreateFields() throws Exception {
  List<IndexableField> values = null;

  // common case...
  for (String field : Arrays.asList("keyword_stxt", "keyword_dv_stxt",
                                    "whitespace_stxt", "whitespace_f_stxt", "whitespace_l_stxt")) {
    values = createIndexableFields(field);
    assertEquals(field, 2, values.size());
    assertThat(field, values.get(0), instanceOf(Field.class));
    assertThat(field, values.get(1), instanceOf(SortedDocValuesField.class));
  }
  
  // special cases...
  values = createIndexableFields("whitespace_nois_stxt");
  assertEquals(1, values.size());
  assertThat(values.get(0), instanceOf(SortedDocValuesField.class));
  //
  values = createIndexableFields("whitespace_nodv_stxt");
  assertEquals(1, values.size());
  assertThat(values.get(0), instanceOf(Field.class));
  //
  values = createIndexableFields("whitespace_m_stxt");
  assertEquals(2, values.size());
  assertThat(values.get(0), instanceOf(Field.class));
  assertThat(values.get(1), instanceOf(SortedSetDocValuesField.class));      
}
 
Example 27
Source Project: lucene-solr   Source File: SortableBinaryField.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<IndexableField> createFields(SchemaField field, Object value) {
  if (field.hasDocValues()) {
    List<IndexableField> fields = new ArrayList<>();
    IndexableField storedField = createField(field, value);
    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));
  }
}
 
Example 28
Source Project: crate   Source File: LuceneOrderedDocCollectorTest.java    License: Apache License 2.0 5 votes vote down vote up
private void addDocToLucene(IndexWriter w, Long value) throws IOException {
    Document doc = new Document();
    if (value != null) {
        fieldType.createFields("value", value, true, true, false).forEach(doc::add);
    } else {
        // Create a placeholder field
        doc.add(new SortedDocValuesField("null_value", new BytesRef("null")));
    }
    w.addDocument(doc);
}
 
Example 29
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 30
Source Project: lucene-solr   Source File: BaseGroupSelectorTestCase.java    License: Apache License 2.0 5 votes vote down vote up
private void indexRandomDocs(RandomIndexWriter w) throws IOException {
  String[] texts = new String[]{ "foo", "bar", "bar baz", "foo foo bar" };

  int numDocs = atLeast(200);
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", i));
    doc.add(new TextField("name", Integer.toString(i), Field.Store.YES));
    doc.add(new TextField("text", texts[random().nextInt(texts.length)], Field.Store.NO));
    doc.add(new SortedDocValuesField("sort1", new BytesRef("sort" + random().nextInt(4))));
    doc.add(new NumericDocValuesField("sort2", random().nextLong()));
    addGroupField(doc, i);
    w.addDocument(doc);
  }
}