Java Code Examples for org.apache.lucene.index.LeafReader#getNumericDocValues()

The following examples show how to use org.apache.lucene.index.LeafReader#getNumericDocValues() . 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 File: StatisHelper.java    From HongsCORE with MIT License 6 votes vote down vote up
@Override
public LeafCollector getLeafCollector(LeafReaderContext lrc) throws IOException {
    LeafReader reader = lrc.reader( );

    for (int i = 0; i < fields.length; i ++) {
        if (groups[i][0] >= 1) {
        if (groups[i][1] == 1) {
            values[i] = reader.getSortedNumericDocValues("%"+fields[i]);
        } else {
            values[i] = reader.      getNumericDocValues("#"+fields[i]);
        }
        } else {
        if (groups[i][1] == 1) {
            values[i] = reader.getSortedSetDocValues("%"+fields[i]);
        } else {
            values[i] = reader.   getSortedDocValues("#"+fields[i]);
        }
        }
    }

    return this;
}
 
Example 2
Source File: DocValuesFieldExistsQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link DocIdSetIterator} from the given field or null if the field doesn't exist
 * in the reader or if the reader has no doc values for the field.
 */
public static DocIdSetIterator getDocValuesDocIdSetIterator(String field, LeafReader reader) throws IOException {
  FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
  final DocIdSetIterator iterator;
  if (fieldInfo != null) {
    switch (fieldInfo.getDocValuesType()) {
      case NONE:
        iterator = null;
        break;
      case NUMERIC:
        iterator = reader.getNumericDocValues(field);
        break;
      case BINARY:
        iterator = reader.getBinaryDocValues(field);
        break;
      case SORTED:
        iterator = reader.getSortedDocValues(field);
        break;
      case SORTED_NUMERIC:
        iterator = reader.getSortedNumericDocValues(field);
        break;
      case SORTED_SET:
        iterator = reader.getSortedSetDocValues(field);
        break;
      default:
        throw new AssertionError();
    }
    return iterator;
  }
  return null;
}
 
Example 3
Source File: FieldCacheImpl.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private BitsEntry createValueDocValues(LeafReader reader, String field) throws IOException {
  FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
  
  DocValuesType dvType = fieldInfo.getDocValuesType();
  DocIdSetIterator iterator;
  switch(dvType) {
  case NUMERIC:
    iterator = reader.getNumericDocValues(field);
    break;
  case BINARY:
    iterator = reader.getBinaryDocValues(field);
    break;
  case SORTED:
    iterator = reader.getSortedDocValues(field);
    break;
  case SORTED_NUMERIC:
    iterator = reader.getSortedNumericDocValues(field);
    break;
  case SORTED_SET:
    iterator = reader.getSortedSetDocValues(field);
    break;
  default:
    throw new AssertionError();
  }

  FixedBitSet bits = new FixedBitSet(reader.maxDoc());
  while (true) {
    int docID = iterator.nextDoc();
    if (docID == DocIdSetIterator.NO_MORE_DOCS) {
      break;
    }
    bits.set(docID);
  }

  return new BitsEntry(bits);
}
 
Example 4
Source File: StatisHelper.java    From HongsCORE with MIT License 5 votes vote down vote up
@Override
public LeafCollector getLeafCollector(LeafReaderContext lrc) throws IOException {
    LeafReader reader = lrc.reader( );

    for (int i = 0; i < fields.length; i ++) {
        if (groups[i][1] == 1) {
            values[i] = reader.getSortedNumericDocValues("%"+fields[i]);
        } else {
            values[i] = reader.      getNumericDocValues("#"+fields[i]);
        }
    }

    return this;
}
 
Example 5
Source File: CombinedDocValues.java    From crate with Apache License 2.0 5 votes vote down vote up
CombinedDocValues(LeafReader leafReader) throws IOException {
    this.versionDV = Objects.requireNonNull(leafReader.getNumericDocValues(VersionFieldMapper.NAME), "VersionDV is missing");
    this.seqNoDV = Objects.requireNonNull(leafReader.getNumericDocValues(SeqNoFieldMapper.NAME), "SeqNoDV is missing");
    this.primaryTermDV = Objects.requireNonNull(
        leafReader.getNumericDocValues(SeqNoFieldMapper.PRIMARY_TERM_NAME), "PrimaryTermDV is missing");
    this.tombstoneDV = leafReader.getNumericDocValues(SeqNoFieldMapper.TOMBSTONE_NAME);
    this.recoverySource = leafReader.getNumericDocValues(SourceFieldMapper.RECOVERY_SOURCE_NAME);
}
 
Example 6
Source File: LuceneChangesSnapshot.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean assertDocSoftDeleted(LeafReader leafReader, int segmentDocId) throws IOException {
    final NumericDocValues ndv = leafReader.getNumericDocValues(Lucene.SOFT_DELETES_FIELD);
    if (ndv == null || ndv.advanceExact(segmentDocId) == false) {
        throw new IllegalStateException("DocValues for field [" + Lucene.SOFT_DELETES_FIELD + "] is not found");
    }
    return ndv.longValue() == 1;
}
 
Example 7
Source File: DocValuesCache.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static synchronized NumericDocValues getNumericDocValues(String field, LeafReader reader) throws IOException
{
    WeakHashMap<Object, NumericDocValues> fieldCache = cache.get(field);

    if(fieldCache == null)
    {
        fieldCache = new WeakHashMap<Object, NumericDocValues>();
        cache.put(field, fieldCache);
    }

    Object cacheKey = reader.getCoreCacheKey();
    NumericDocValues cachedValues = fieldCache.get(cacheKey);

    if(cachedValues == null)
    {
        NumericDocValues fieldValues = reader.getNumericDocValues(field);
        if(fieldValues == null)
        {
            return null;
        }
        else
        {
            int maxDoc = reader.maxDoc();
            boolean longs = false;
            int[] intValues = new int[maxDoc]; //Always start off with an int array.
            SettableDocValues settableValues = new IntValues(intValues);

            for(int i=0; i<maxDoc; i++)
            {
                long value = fieldValues.get(i);
                if(value > Integer.MAX_VALUE && !longs)
                {
                    longs = true;
                    settableValues = new LongValues(intValues);
                }

                settableValues.set(i, value);
            }
            fieldCache.put(cacheKey, settableValues);
            return settableValues;
        }
    }
    else
    {
        return cachedValues;
    }
}
 
Example 8
Source File: TestMemoryIndex.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testDocValues() throws Exception {
  Document doc = new Document();
  doc.add(new NumericDocValuesField("numeric", 29L));
  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));
  doc.add(new BinaryDocValuesField("binary", new BytesRef("a")));
  doc.add(new SortedDocValuesField("sorted", new BytesRef("b")));
  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")));

  MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
  LeafReader leafReader = mi.createSearcher().getIndexReader().leaves().get(0).reader();
  NumericDocValues numericDocValues = leafReader.getNumericDocValues("numeric");
  assertEquals(0, numericDocValues.nextDoc());
  assertEquals(29L, numericDocValues.longValue());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, numericDocValues.nextDoc());
  SortedNumericDocValues sortedNumericDocValues = leafReader.getSortedNumericDocValues("sorted_numeric");
  assertEquals(0, sortedNumericDocValues.nextDoc());
  assertEquals(5, sortedNumericDocValues.docValueCount());
  assertEquals(30L, sortedNumericDocValues.nextValue());
  assertEquals(31L, sortedNumericDocValues.nextValue());
  assertEquals(32L, sortedNumericDocValues.nextValue());
  assertEquals(32L, sortedNumericDocValues.nextValue());
  assertEquals(33L, sortedNumericDocValues.nextValue());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, sortedNumericDocValues.nextDoc());
  BinaryDocValues binaryDocValues = leafReader.getBinaryDocValues("binary");
  assertEquals(0, binaryDocValues.nextDoc());
  assertEquals("a", binaryDocValues.binaryValue().utf8ToString());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, binaryDocValues.nextDoc());
  SortedDocValues sortedDocValues = leafReader.getSortedDocValues("sorted");
  assertEquals(0, sortedDocValues.nextDoc());
  assertEquals("b", sortedDocValues.binaryValue().utf8ToString());
  assertEquals(0, sortedDocValues.ordValue());
  assertEquals("b", sortedDocValues.lookupOrd(0).utf8ToString());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, sortedDocValues.nextDoc());
  SortedSetDocValues sortedSetDocValues = leafReader.getSortedSetDocValues("sorted_set");
  assertEquals(3, sortedSetDocValues.getValueCount());
  assertEquals(0, sortedSetDocValues.nextDoc());
  assertEquals(0L, sortedSetDocValues.nextOrd());
  assertEquals(1L, sortedSetDocValues.nextOrd());
  assertEquals(2L, sortedSetDocValues.nextOrd());
  assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSetDocValues.nextOrd());
  assertEquals("c", sortedSetDocValues.lookupOrd(0L).utf8ToString());
  assertEquals("d", sortedSetDocValues.lookupOrd(1L).utf8ToString());
  assertEquals("f", sortedSetDocValues.lookupOrd(2L).utf8ToString());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, sortedDocValues.nextDoc());
}
 
Example 9
Source File: TestUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void checkReaderSanity(LeafReader reader) throws IOException {
  for (FieldInfo info : reader.getFieldInfos()) {
    
    // reader shouldn't return normValues if the field does not have them
    if (!info.hasNorms()) {
      if (reader.getNormValues(info.name) != null) {
        throw new RuntimeException("field: " + info.name + " should omit norms but has them!");
      }
    }
    
    // reader shouldn't return docValues if the field does not have them
    // reader shouldn't return multiple docvalues types for the same field.
    switch(info.getDocValuesType()) {
      case NONE:
        if (reader.getBinaryDocValues(info.name) != null ||
            reader.getNumericDocValues(info.name) != null ||
            reader.getSortedDocValues(info.name) != null || 
            reader.getSortedSetDocValues(info.name) != null) {
          throw new RuntimeException("field: " + info.name + " has docvalues but should omit them!");
        }
        break;
      case SORTED:
        if (reader.getBinaryDocValues(info.name) != null ||
            reader.getNumericDocValues(info.name) != null ||
            reader.getSortedNumericDocValues(info.name) != null ||
            reader.getSortedSetDocValues(info.name) != null) {
          throw new RuntimeException(info.name + " returns multiple docvalues types!");
        }
        break;
      case SORTED_NUMERIC:
        if (reader.getBinaryDocValues(info.name) != null ||
            reader.getNumericDocValues(info.name) != null ||
            reader.getSortedSetDocValues(info.name) != null ||
            reader.getSortedDocValues(info.name) != null) {
          throw new RuntimeException(info.name + " returns multiple docvalues types!");
        }
        break;
      case SORTED_SET:
        if (reader.getBinaryDocValues(info.name) != null ||
            reader.getNumericDocValues(info.name) != null ||
            reader.getSortedNumericDocValues(info.name) != null ||
            reader.getSortedDocValues(info.name) != null) {
          throw new RuntimeException(info.name + " returns multiple docvalues types!");
        }
        break;
      case BINARY:
        if (reader.getNumericDocValues(info.name) != null ||
            reader.getSortedDocValues(info.name) != null ||
            reader.getSortedNumericDocValues(info.name) != null ||
            reader.getSortedSetDocValues(info.name) != null) {
          throw new RuntimeException(info.name + " returns multiple docvalues types!");
        }
        break;
      case NUMERIC:
        if (reader.getBinaryDocValues(info.name) != null ||
            reader.getSortedDocValues(info.name) != null ||
            reader.getSortedNumericDocValues(info.name) != null ||
            reader.getSortedSetDocValues(info.name) != null) {
          throw new RuntimeException(info.name + " returns multiple docvalues types!");
        }
        break;
      default:
        throw new AssertionError();
    }
  }
}
 
Example 10
Source File: FieldCacheImpl.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public NumericDocValues getNumerics(LeafReader reader, String field, Parser parser) throws IOException {
  if (parser == null) {
    throw new NullPointerException();
  }
  final NumericDocValues valuesIn = reader.getNumericDocValues(field);
  if (valuesIn != null) {
    return valuesIn;
  } else {
    final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
    if (info == null) {
      return DocValues.emptyNumeric();
    } else if (info.getDocValuesType() != DocValuesType.NONE) {
      throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
    }
    
    if (parser instanceof PointParser) {
      // points case
      // no points in this segment
      if (info.getPointDimensionCount() == 0) {
        return DocValues.emptyNumeric();
      }
      if (info.getPointDimensionCount() != 1) {
        throw new IllegalStateException("Type mismatch: " + field + " was indexed with dimensions=" + info.getPointDimensionCount());
      }
      PointValues values = reader.getPointValues(field);
      // no actual points for this field (e.g. all points deleted)
      if (values == null || values.size() == 0) {
        return DocValues.emptyNumeric();
      }
      // not single-valued
      if (values.size() != values.getDocCount()) {
        throw new IllegalStateException("Type mismatch: " + field + " was indexed with multiple values, numValues=" + values.size() + ",numDocs=" + values.getDocCount());
      }
    } else {
      // postings case 
      // not indexed
      if (info.getIndexOptions() == IndexOptions.NONE) {
        return DocValues.emptyNumeric();
      }
    }

    return ((LongsFromArray) caches.get(Long.TYPE).get(reader, new CacheKey(field, parser))).iterator();
  }
}
 
Example 11
Source File: IGainTermsQParserPlugin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void doSetNextReader(LeafReaderContext context) throws IOException {
  super.doSetNextReader(context);
  LeafReader reader = context.reader();
  leafOutcomeValue = reader.getNumericDocValues(outcome);
}
 
Example 12
Source File: DocValuesTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocValues() throws IOException {
  assertU(adoc("id", "1"));
  assertU(commit());
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      final LeafReader reader = searcher.getSlowAtomicReader();
      assertEquals(1, reader.numDocs());
      final FieldInfos infos = reader.getFieldInfos();
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("floatdv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("intdv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("doubledv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("longdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED, infos.fieldInfo("stringdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED, infos.fieldInfo("booldv").getDocValuesType());

      NumericDocValues dvs = reader.getNumericDocValues("floatdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals((long) Float.floatToIntBits(1), dvs.longValue());
      dvs = reader.getNumericDocValues("intdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(2L, dvs.longValue());
      dvs = reader.getNumericDocValues("doubledv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(Double.doubleToLongBits(3), dvs.longValue());
      dvs = reader.getNumericDocValues("longdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(4L, dvs.longValue());
      SortedDocValues sdv = reader.getSortedDocValues("stringdv");
      assertEquals(0, sdv.nextDoc());
      assertEquals("solr", sdv.binaryValue().utf8ToString());
      sdv = reader.getSortedDocValues("booldv");
      assertEquals(0, sdv.nextDoc());
      assertEquals("T", sdv.binaryValue().utf8ToString());

      final IndexSchema schema = core.getLatestSchema();
      final SchemaField floatDv = schema.getField("floatdv");
      final SchemaField intDv = schema.getField("intdv");
      final SchemaField doubleDv = schema.getField("doubledv");
      final SchemaField longDv = schema.getField("longdv");
      final SchemaField boolDv = schema.getField("booldv");

      FunctionValues values = floatDv.getType().getValueSource(floatDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(1f, values.floatVal(0), 0f);
      assertEquals(1f, values.objectVal(0));
      values = intDv.getType().getValueSource(intDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(2, values.intVal(0));
      assertEquals(2, values.objectVal(0));
      values = doubleDv.getType().getValueSource(doubleDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(3d, values.doubleVal(0), 0d);
      assertEquals(3d, values.objectVal(0));
      values = longDv.getType().getValueSource(longDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(4L, values.longVal(0));
      assertEquals(4L, values.objectVal(0));
      
      values = boolDv.getType().getValueSource(boolDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals("true", values.strVal(0));
      assertEquals(true, values.objectVal(0));

      // check reversibility of created fields
      tstToObj(schema.getField("floatdv"), -1.5f);
      tstToObj(schema.getField("floatdvs"), -1.5f);
      tstToObj(schema.getField("doubledv"), -1.5d);
      tstToObj(schema.getField("doubledvs"), -1.5d);
      tstToObj(schema.getField("intdv"), -7);
      tstToObj(schema.getField("intdvs"), -7);
      tstToObj(schema.getField("longdv"), -11L);
      tstToObj(schema.getField("longdvs"), -11L);
      tstToObj(schema.getField("datedv"), new Date(1000));
      tstToObj(schema.getField("datedvs"), new Date(1000));
      tstToObj(schema.getField("stringdv"), "foo");
      tstToObj(schema.getField("stringdvs"), "foo");
      tstToObj(schema.getField("booldv"), true);
      tstToObj(schema.getField("booldvs"), true);

    } finally {
      searcherRef.decref();
    }
  }
}