Examples of NumericField


Examples of org.apache.lucene.document.NumericField

    for (int l = 0; l < num; l++) {
      Document doc = new Document();
      for (int m=0, c=random.nextInt(10); m<=c; m++) {
        int value = random.nextInt(Integer.MAX_VALUE);
        doc.add(newField("asc", format.format(value), Field.Store.NO, Field.Index.NOT_ANALYZED));
        doc.add(new NumericField("trie", Field.Store.NO, true).setIntValue(value));
      }
      writer.addDocument(doc);
    }
    IndexReader reader = writer.getReader();
    writer.close();
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    RandomIndexWriter writer = new RandomIndexWriter(random, directory,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
        .setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000))
        .setMergePolicy(newLogMergePolicy()));
   
    NumericField
      field8 = new NumericField("field8", 8, Field.Store.YES, true),
      field4 = new NumericField("field4", 4, Field.Store.YES, true),
      field2 = new NumericField("field2", 2, Field.Store.YES, true),
      fieldNoTrie = new NumericField("field"+Integer.MAX_VALUE, Integer.MAX_VALUE, Field.Store.YES, true),
      ascfield8 = new NumericField("ascfield8", 8, Field.Store.NO, true),
      ascfield4 = new NumericField("ascfield4", 4, Field.Store.NO, true),
      ascfield2 = new NumericField("ascfield2", 2, Field.Store.NO, true);
   
    Document doc = new Document();
    // add fields, that have a distance to test general functionality
    doc.add(field8); doc.add(field4); doc.add(field2); doc.add(fieldNoTrie);
    // add ascending fields with a distance of 1, beginning at -noDocs/2 to test the correct splitting of range and inclusive/exclusive
    doc.add(ascfield8); doc.add(ascfield4); doc.add(ascfield2);
   
    // Add a series of noDocs docs with increasing int values
    for (int l=0; l<noDocs; l++) {
      int val=distance*l+startOffset;
      field8.setIntValue(val);
      field4.setIntValue(val);
      field2.setIntValue(val);
      fieldNoTrie.setIntValue(val);

      val=l-(noDocs/2);
      ascfield8.setIntValue(val);
      ascfield4.setIntValue(val);
      ascfield2.setIntValue(val);
      writer.addDocument(doc);
    }
 
    reader = writer.getReader();
    searcher=newSearcher(reader);
View Full Code Here

Examples of org.apache.lucene.document.NumericField

  @Test
  public void testInfiniteValues() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
    Document doc = new Document();
    doc.add(new NumericField("float").setFloatValue(Float.NEGATIVE_INFINITY));
    doc.add(new NumericField("int").setIntValue(Integer.MIN_VALUE));
    writer.addDocument(doc);
   
    doc = new Document();
    doc.add(new NumericField("float").setFloatValue(Float.POSITIVE_INFINITY));
    doc.add(new NumericField("int").setIntValue(Integer.MAX_VALUE));
    writer.addDocument(doc);
   
    doc = new Document();
    doc.add(new NumericField("float").setFloatValue(0.0f));
    doc.add(new NumericField("int").setIntValue(0));
    writer.addDocument(doc);
    writer.close();
   
    IndexSearcher s = new IndexSearcher(dir);
   
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    final int numDocs = atLeast(500);
    final Number[] answers = new Number[numDocs];
    final NumericField.DataType[] typeAnswers = new NumericField.DataType[numDocs];
    for(int id=0;id<numDocs;id++) {
      Document doc = new Document();
      NumericField nf = new NumericField("nf", Field.Store.YES, false);
      doc.add(nf);
      final Number answer;
      final NumericField.DataType typeAnswer;
      if (random.nextBoolean()) {
        // float/double
        if (random.nextBoolean()) {
          final float f = random.nextFloat();
          nf.setFloatValue(f);
          answer = Float.valueOf(f);
          typeAnswer = NumericField.DataType.FLOAT;
        } else {
          final double d = random.nextDouble();
          nf.setDoubleValue(d);
          answer = Double.valueOf(d);
          typeAnswer = NumericField.DataType.DOUBLE;
        }
      } else {
        // int/long
        if (random.nextBoolean()) {
          final int i = random.nextInt();
          nf.setIntValue(i);
          answer = Integer.valueOf(i);
          typeAnswer = NumericField.DataType.INT;
        } else {
          final long l = random.nextLong();
          nf.setLongValue(l);
          answer = Long.valueOf(l);
          typeAnswer = NumericField.DataType.LONG;
        }
      }
      answers[id] = answer;
      typeAnswers[id] = typeAnswer;
      doc.add(new NumericField("id", Integer.MAX_VALUE, Field.Store.NO, true).setIntValue(id));
      w.addDocument(doc);
    }
    final IndexReader r = w.getReader();
    w.close();
   
    assertEquals(numDocs, r.numDocs());

    for(IndexReader sub : r.getSequentialSubReaders()) {
      final int[] ids = FieldCache.DEFAULT.getInts(sub, "id");
      for(int docID=0;docID<sub.numDocs();docID++) {
        final Document doc = sub.document(docID);
        final Fieldable f = doc.getFieldable("nf");
        assertTrue("got f=" + f, f instanceof NumericField);
        final NumericField nf = (NumericField) f;
        assertEquals(answers[ids[docID]], nf.getNumericValue());
        assertSame(typeAnswers[ids[docID]], nf.getDataType());
      }
    }
    r.close();
    dir.close();
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    getLuceneDocument().add( (Fieldable) toSerializable( instanceAsByte, loader ) );
  }

  @Override
  public void addIntNumericField(int value, String name, int precisionStep, SerializableStore store, boolean indexed, float boost, boolean omitNorms, boolean omitTermFreqAndPositions) {
    NumericField numField = buildNumericField(
        name,
        precisionStep,
        store,
        indexed,
        boost,
        omitNorms,
        omitTermFreqAndPositions
    );
    numField.setIntValue( value );
    getLuceneDocument().add( numField );
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    getLuceneDocument().add( numField );
  }

  @Override
  public void addLongNumericField(long value, String name, int precisionStep, SerializableStore store, boolean indexed, float boost, boolean omitNorms, boolean omitTermFreqAndPositions) {
    NumericField numField = buildNumericField(
        name,
        precisionStep,
        store,
        indexed,
        boost,
        omitNorms,
        omitTermFreqAndPositions
    );
    numField.setLongValue( value );
    getLuceneDocument().add( numField );
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    getLuceneDocument().add( numField );
  }

  @Override
  public void addFloatNumericField(float value, String name, int precisionStep, SerializableStore store, boolean indexed, float boost, boolean omitNorms, boolean omitTermFreqAndPositions) {
    NumericField numField = buildNumericField(
        name,
        precisionStep,
        store,
        indexed,
        boost,
        omitNorms,
        omitTermFreqAndPositions
    );
    numField.setFloatValue( value );
    getLuceneDocument().add( numField );
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    numField.setFloatValue( value );
    getLuceneDocument().add( numField );
  }

  private NumericField buildNumericField(String name, int precisionStep, SerializableStore store, boolean indexed, float boost, boolean omitNorms, boolean omitTermFreqAndPositions) {
    NumericField numField = new NumericField(
        name,
        precisionStep,
        getStore( store ),
        indexed
    );
    numField.setBoost( boost );
    numField.setOmitNorms( omitNorms );
    if ( omitTermFreqAndPositions ) {
      numField.setIndexOptions( IndexOptions.DOCS_ONLY );
    }
    else {
      numField.setIndexOptions( IndexOptions.DOCS_AND_FREQS_AND_POSITIONS );
    }
    return numField;
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

    return numField;
  }

  @Override
  public void addDoubleNumericField(double value, String name, int precisionStep, SerializableStore store, boolean indexed, float boost, boolean omitNorms, boolean omitTermFreqAndPositions) {
    NumericField numField = buildNumericField(
        name,
        precisionStep,
        store,
        indexed,
        boost,
        omitNorms,
        omitTermFreqAndPositions
    );
    numField.setDoubleValue( value );
    getLuceneDocument().add( numField );
  }
View Full Code Here

Examples of org.apache.lucene.document.NumericField

  private void buildDocument(Document document, Serializer serializer) {
    List<Fieldable> docFields = document.getFields();
    serializer.fields( docFields );
    for ( Fieldable fieldable : docFields ) {
      if ( fieldable instanceof NumericField ) {
        NumericField safeField = (NumericField) fieldable;
        LuceneNumericFieldContext context = new LuceneNumericFieldContext( (NumericField) fieldable );
        switch ( safeField.getDataType() ) {
          case INT:
            serializer.addIntNumericField(
                safeField.getNumericValue().intValue(),
                context
            );
            break;
          case LONG:
            serializer.addLongNumericField(
                safeField.getNumericValue().longValue(),
                context
            );
            break;
          case FLOAT:
            serializer.addFloatNumericField(
                safeField.getNumericValue().floatValue(),
                context
            );
            break;
          case DOUBLE:
            serializer.addDoubleNumericField(
                safeField.getNumericValue().doubleValue(),
                context
            );
            break;
          default:
            String dataType = safeField.getDataType() == null ? "null" : safeField.getDataType().toString();
            throw log.unknownNumericFieldType( dataType );
        }
      }
      else if (fieldable instanceof Field) {
        Field safeField = (Field) fieldable;
        if ( safeField.isBinary() ) {
          serializer.addFieldWithBinaryData( new LuceneFieldContext( safeField ) );
        }
        else if ( safeField.stringValue() != null ) {
          serializer.addFieldWithStringData( new LuceneFieldContext( safeField ) );
        }
        else if ( safeField.readerValue() != null && safeField.readerValue() instanceof Serializable ) {
          serializer.addFieldWithSerializableReaderData( new LuceneFieldContext( safeField ) );
        }
        else if ( safeField.readerValue() != null ) {
          throw log.conversionFromReaderToStringNotYetImplemented();
        }
        else if ( safeField.tokenStreamValue() != null ) {
          serializer.addFieldWithTokenStreamData( new LuceneFieldContext( safeField ) );
        }
        else {
          throw log.unknownFieldType( safeField.getClass() );
        }
      }
      else if (fieldable instanceof Serializable) { //Today Fieldable is Serializable but for how long?
        serializer.addFieldWithSerializableFieldable( toByteArray( fieldable ) );
      }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.