org.apache.lucene.document.SortedNumericDocValuesField Java Examples

The following examples show how to use org.apache.lucene.document.SortedNumericDocValuesField. 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: NumberFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new DoublePoint(name, value.doubleValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name,
            NumericUtils.doubleToSortableLong(value.doubleValue())));
    }
    if (stored) {
        fields.add(new StoredField(name, value.doubleValue()));
    }
    return fields;
}
 
Example #2
Source File: TestDocValuesQueries.java    From lucene-solr with 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 #3
Source File: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testToString() {
  Query q1 = SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5);
  assertEquals("foo:[3 TO 5]", q1.toString());
  assertEquals("[3 TO 5]", q1.toString("foo"));
  assertEquals("foo:[3 TO 5]", q1.toString("bar"));

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  assertEquals("foo:[[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, true);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, false);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), null, true, true);
  assertEquals("foo:[[62 61 72] TO *}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", null, new BytesRef("baz"), true, true);
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString());
  assertEquals("{* TO [62 61 7a]]", q2.toString("foo"));
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString("bar"));
}
 
Example #4
Source File: EnumFieldType.java    From lucene-solr with 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 #5
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new FloatPoint(name, value.floatValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name,
            NumericUtils.floatToSortableInt(value.floatValue())));
    }
    if (stored) {
        fields.add(new StoredField(name, value.floatValue()));
    }
    return fields;
}
 
Example #6
Source File: BaseDocValuesFormatTestCase.java    From lucene-solr with 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 #7
Source File: TestIndexSortSortedNumericDocValuesRangeQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the index sort optimization is not activated when some documents
 * have multiple values.
 */
public void testMultiDocValues() throws Exception {
  Directory dir = newDirectory();

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

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

  testIndexSortOptimizationDeactivated(writer);

  writer.close();
  dir.close();
}
 
Example #8
Source File: BooleanFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
        return;
    }

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

    if (value == null) {
        return;
    }
    fields.add(new Field(fieldType().names().indexName(), value ? "T" : "F", fieldType()));
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().names().indexName(), value ? 1 : 0));
    }
}
 
Example #9
Source File: DocValuesAdapterTest.java    From lucene-solr with 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 #10
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    float l = Float.NEGATIVE_INFINITY;
    float u = Float.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = FloatPoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = FloatPoint.nextDown(u);
        }
    }
    Query query = FloatPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.floatToSortableInt(l),
                NumericUtils.floatToSortableInt(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #11
Source File: NumberFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected final void addDocValue(ParseContext context, List<Field> fields, long value) {
    if (useSortedNumericDocValues) {
        fields.add(new SortedNumericDocValuesField(fieldType().names().indexName(), value));
    } else {
        CustomLongNumericDocValuesField field = (CustomLongNumericDocValuesField) context.doc().getByKey(fieldType().names().indexName());
        if (field != null) {
            field.add(value);
        } else {
            field = new CustomLongNumericDocValuesField(fieldType().names().indexName(), value);
            context.doc().addWithKey(fieldType().names().indexName(), field);
        }
    }
}
 
Example #12
Source File: TestDocValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * field with sortednumeric docvalues
 */
public void testSortedNumericField() throws Exception {
  Directory dir = newDirectory();
  IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(null));
  Document doc = new Document();
  doc.add(new SortedNumericDocValuesField("foo", 3));
  iw.addDocument(doc);
  DirectoryReader dr = DirectoryReader.open(iw);
  LeafReader r = getOnlyLeafReader(dr);
  
  // ok
  assertNotNull(DocValues.getSortedNumeric(r, "foo"));
  
  // errors
  expectThrows(IllegalStateException.class, () -> {
      DocValues.getBinary(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getNumeric(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getSorted(r, "foo");
  });
  expectThrows(IllegalStateException.class, () -> {
    DocValues.getSortedSet(r, "foo");
  });
  
  dr.close();
  iw.close();
  dir.close();
}
 
Example #13
Source File: TestExitableDirectoryReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addDVs(Document d1, int i) {
  d1.add(new NumericDocValuesField("numeric", i));
  d1.add(new BinaryDocValuesField("binary", new BytesRef(""+i)));
  d1.add(new SortedDocValuesField("sorted", new BytesRef(""+i)));
  d1.add(new SortedNumericDocValuesField("sortednumeric", i));
  d1.add(new SortedSetDocValuesField("sortedset", new BytesRef(""+i)));
}
 
Example #14
Source File: TestIndexSorting.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWrongSortFieldType() throws Exception {
  Directory dir = newDirectory();
  List<Field> dvs = new ArrayList<>();
  dvs.add(new SortedDocValuesField("field", new BytesRef("")));
  dvs.add(new SortedSetDocValuesField("field", new BytesRef("")));
  dvs.add(new NumericDocValuesField("field", 42));
  dvs.add(new SortedNumericDocValuesField("field", 42));

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

  for (int i = 0; i < sortFields.size(); i++) {
    for (int j = 0; j < dvs.size(); j++) {
      if (i == j) {
        continue;
      }
      Sort indexSort = new Sort(sortFields.get(i));
      IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
      iwc.setIndexSort(indexSort);
      IndexWriter w = new IndexWriter(dir, iwc);
      Document doc = new Document();
      doc.add(dvs.get(j));
      IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("expected field [field] to be "));
      doc.clear();
      doc.add(dvs.get(i));
      w.addDocument(doc);
      doc.add(dvs.get(j));
      exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("cannot change DocValues type"));
      w.rollback();
      IOUtils.close(w);
    }
  }
  IOUtils.close(dir);
}
 
Example #15
Source File: TestDuelingCodecs.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * populates a writer with random stuff. this must be fully reproducable with the seed!
 */
public static void createRandomIndex(int numdocs, RandomIndexWriter writer, long seed) throws IOException {
  Random random = new Random(seed);
  // primary source for our data is from linefiledocs, it's realistic.
  LineFileDocs lineFileDocs = new LineFileDocs(random);

  // TODO: we should add other fields that use things like docs&freqs but omit positions,
  // because linefiledocs doesn't cover all the possibilities.
  for (int i = 0; i < numdocs; i++) {
    Document document = lineFileDocs.nextDoc();
    // grab the title and add some SortedSet instances for fun
    String title = document.get("titleTokenized");
    String split[] = title.split("\\s+");
    document.removeFields("sortedset");
    for (String trash : split) {
      document.add(new SortedSetDocValuesField("sortedset", new BytesRef(trash)));
    }
    // add a numeric dv field sometimes
    document.removeFields("sparsenumeric");
    if (random.nextInt(4) == 2) {
      document.add(new NumericDocValuesField("sparsenumeric", random.nextInt()));
    }
    // add sortednumeric sometimes
    document.removeFields("sparsesortednum");
    if (random.nextInt(5) == 1) {
      document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
      if (random.nextBoolean()) {
        document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
      }
    }
    writer.addDocument(document);
  }
  
  lineFileDocs.close();
}
 
Example #16
Source File: ShortColumnReferenceTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (short i = -10; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("_id", Short.toString(i), Field.Store.NO));
        doc.add(new SortedNumericDocValuesField(column, i));
        writer.addDocument(doc);
    }
}
 
Example #17
Source File: NumericFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Query numericDocValuesRangeQuery(
    String field,
    Number lowerValue, Number upperValue,
    boolean lowerInclusive, boolean upperInclusive,
    boolean multiValued) {

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

  long actualUpperValue = Long.MAX_VALUE;
  if (upperValue != null) {
    actualUpperValue = upperValue.longValue();
    if (upperInclusive == false) {
      if (actualUpperValue == Long.MIN_VALUE) {
        return new MatchNoDocsQuery();
      }
      --actualUpperValue;
    }
  }
  if (multiValued) {
    // In multiValued case use SortedNumericDocValuesField, this won't work for Trie*Fields wince they use BinaryDV in the multiValue case
    return SortedNumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  } else {
    return NumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  }
}
 
Example #18
Source File: TestPointFields.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * Calls {@link SchemaField#createFields} on the specified value for the specified field name, and asserts 
 * that the results match the SchemaField propeties, with an additional check that the <code>pointType</code> 
 * is included if and only if the SchemaField is "indexed" 
 */
private List<IndexableField> callAndCheckCreateFields(final String fieldName, final Class<?> pointType, final Object value) throws Exception {
  final SchemaField sf = h.getCore().getLatestSchema().getField(fieldName);
  final List<IndexableField> results = sf.createFields(value);
  final Set<IndexableField> resultSet = new LinkedHashSet<>(results);
  assertEquals("duplicates found in results? " + results.toString(),
               results.size(), resultSet.size());

  final Set<Class<?>> resultClasses = new HashSet<>();
  for (IndexableField f : results) {
    resultClasses.add(f.getClass());
    
    if (!sf.hasDocValues() ) {
      assertFalse(f.toString(),
                  (f instanceof NumericDocValuesField) ||
                  (f instanceof SortedNumericDocValuesField));
    }
  }
  assertEquals(fieldName + " stored? Result Fields: " + Arrays.toString(results.toArray()),
               sf.stored(), resultClasses.contains(StoredField.class));
  assertEquals(fieldName + " indexed? Result Fields: " + Arrays.toString(results.toArray()),
               sf.indexed(), resultClasses.contains(pointType));
  if (sf.multiValued()) {
    assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()),
                 sf.hasDocValues(), resultClasses.contains(SortedNumericDocValuesField.class));
  } else {
    assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()),
                 sf.hasDocValues(), resultClasses.contains(NumericDocValuesField.class));
  }

  return results;
}
 
Example #19
Source File: DateFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm,
                        Object upperTerm,
                        boolean includeLower,
                        boolean includeUpper,
                        ShapeRelation relation,
                        @Nullable DateTimeZone timeZone,
                        QueryShardContext context) {
    failIfNotIndexed();
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    long l, u;
    if (lowerTerm == null) {
        l = Long.MIN_VALUE;
    } else {
        l = (Long) lowerTerm;
        if (includeLower == false) {
            ++l;
        }
    }
    if (upperTerm == null) {
        u = Long.MAX_VALUE;
    } else {
        u = (Long) upperTerm;
        if (includeUpper == false) {
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(name(), l, u);
    if (hasDocValues()) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #20
Source File: DoubleColumnReferenceTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (double d = 0.5; d < 10.0d; d++) {
        Document doc = new Document();
        doc.add(new SortedNumericDocValuesField(column, NumericUtils.doubleToSortableLong(d)));
        writer.addDocument(doc);
    }
}
 
Example #21
Source File: BooleanFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
        return;
    }

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

    if (value == null) {
        return;
    }
    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        fields.add(new Field(fieldType().name(), value ? "T" : "F", fieldType()));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0));
    } else {
        createFieldNamesField(context, fields);
    }
}
 
Example #22
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    double l = Double.NEGATIVE_INFINITY;
    double u = Double.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = DoublePoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = DoublePoint.nextDown(u);
        }
    }
    Query query = DoublePoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.doubleToSortableLong(l),
                NumericUtils.doubleToSortableLong(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #23
Source File: FloatColumnReferenceTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (float f = -0.5f; f < 10.0f; f++) {
        Document doc = new Document();
        doc.add(new SortedNumericDocValuesField(column, NumericUtils.floatToSortableInt(f)));
        writer.addDocument(doc);
    }
}
 
Example #24
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    int l = Integer.MIN_VALUE;
    int u = Integer.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Integer.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Integer.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = IntPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #25
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new IntPoint(name, value.intValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name, value.intValue()));
    }
    if (stored) {
        fields.add(new StoredField(name, value.intValue()));
    }
    return fields;
}
 
Example #26
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    long l = Long.MIN_VALUE;
    long u = Long.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Long.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Long.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #27
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new LongPoint(name, value.longValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name, value.longValue()));
    }
    if (stored) {
        fields.add(new StoredField(name, value.longValue()));
    }
    return fields;
}
 
Example #28
Source File: BaseDocValuesFormatTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortedNumericMergeAwayAllValuesLargeSegment() throws IOException {
  Directory directory = newDirectory();
  Analyzer analyzer = new MockAnalyzer(random());
  IndexWriterConfig iwconfig = newIndexWriterConfig(analyzer);
  iwconfig.setMergePolicy(newLogMergePolicy());
  RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory, iwconfig);

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

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

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

  ireader.close();
  directory.close();
}
 
Example #29
Source File: TestMemoryIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDocValues_resetIterator() throws Exception {
  Document doc = new Document();

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

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

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

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

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

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

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  QueryUtils.checkEqual(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("baz"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("bar"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("quux", new BytesRef("bar"), new BytesRef("baz"), true, true));
}