Example usage for org.apache.lucene.document Field setBytesValue

List of usage examples for org.apache.lucene.document Field setBytesValue

Introduction

In this page you can find the example usage for org.apache.lucene.document Field setBytesValue.

Prototype

public void setBytesValue(BytesRef value) 

Source Link

Document

Expert: change the value of this field.

Usage

From source file:com.vmware.xenon.services.common.LuceneIndexDocumentHelper.java

License:Open Source License

private Field getAndSetSortedStoredField(String name, String value) {
    Field f = this.sortedStringFields.computeIfAbsent(name, (k) -> {
        return new SortedDocValuesField(name, new BytesRef(value));
    });//w  w  w  . j  av  a  2 s .co m
    f.setBytesValue(new BytesRef(value));
    return f;
}

From source file:org.apache.solr.legacy.TestLegacyField.java

License:Apache License

private void trySetBytesValue(Field f) {
    expectThrows(IllegalArgumentException.class, () -> {
        f.setBytesValue(new byte[] { 5, 5 });
    });// w  w  w. j  a va 2s  . c  o  m
}

From source file:org.apache.solr.legacy.TestLegacyField.java

License:Apache License

private void trySetBytesRefValue(Field f) {
    expectThrows(IllegalArgumentException.class, () -> {
        f.setBytesValue(new BytesRef("bogus"));
    });/*from   w  w  w . ja v a  2s. c om*/
}

From source file:org.apache.solr.uninverting.TestFieldCacheVsDocValues.java

License:Apache License

private void doTestSortedVsFieldCache(int minLength, int maxLength) throws Exception {
    Directory dir = newDirectory();/*from   w  w  w . ja va 2  s.c om*/
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, conf);
    Document doc = new Document();
    Field idField = new StringField("id", "", Field.Store.NO);
    Field indexedField = new StringField("indexed", "", Field.Store.NO);
    Field dvField = new SortedDocValuesField("dv", new BytesRef());
    doc.add(idField);
    doc.add(indexedField);
    doc.add(dvField);

    // index some docs
    int numDocs = atLeast(300);
    for (int i = 0; i < numDocs; i++) {
        idField.setStringValue(Integer.toString(i));
        final int length;
        if (minLength == maxLength) {
            length = minLength; // fixed length
        } else {
            length = TestUtil.nextInt(random(), minLength, maxLength);
        }
        String value = TestUtil.randomSimpleString(random(), length);
        indexedField.setStringValue(value);
        dvField.setBytesValue(new BytesRef(value));
        writer.addDocument(doc);
        if (random().nextInt(31) == 0) {
            writer.commit();
        }
    }

    // delete some docs
    int numDeletions = random().nextInt(numDocs / 10);
    for (int i = 0; i < numDeletions; i++) {
        int id = random().nextInt(numDocs);
        writer.deleteDocuments(new Term("id", Integer.toString(id)));
    }
    writer.close();

    // compare
    DirectoryReader ir = DirectoryReader.open(dir);
    for (LeafReaderContext context : ir.leaves()) {
        LeafReader r = context.reader();
        SortedDocValues expected = FieldCache.DEFAULT.getTermsIndex(r, "indexed");
        SortedDocValues actual = r.getSortedDocValues("dv");
        assertEquals(r.maxDoc(), expected, actual);
    }
    ir.close();
    dir.close();
}

From source file:org.lukhnos.lucenestudy.Indexer.java

License:MIT License

public void addDocuments(List<Document> docs) throws IOException {
    // Reuse doc and field instances. See http://wiki.apache.org/lucene-java/ImproveIndexingSpeed
    Field titleField = new TextField(TITLE_FIELD_NAME, "", Field.Store.YES);
    Field titleDocsValueField = new SortedDocValuesField(TITLE_FIELD_NAME, new BytesRef(0));
    Field yearField = new IntField(YEAR_FIELD_NAME, 0, Field.Store.YES);
    Field yearDocsValueField = new NumericDocValuesField(YEAR_FIELD_NAME, 0L);
    Field ratingField = new IntField(RATING_FIELD_NAME, 0, Field.Store.YES);
    Field ratingDocsValueField = new NumericDocValuesField(RATING_FIELD_NAME, 0L);
    Field positiveField = new IntField(POSITIVE_FIELD_NAME, 0, Field.Store.YES);
    Field reviewField = new TextField(REVIEW_FIELD_NAME, "", Field.Store.YES);
    Field sourceField = new StringField(SOURCE_FIELD_NAME, "", Field.Store.YES);

    for (Document doc : docs) {
        org.apache.lucene.document.Document luceneDoc = new org.apache.lucene.document.Document();

        if (doc.title != null && !doc.title.isEmpty()) {
            titleField.setStringValue(doc.title);
            luceneDoc.add(titleField);//from w w  w. j  ava  2 s . com

            titleDocsValueField.setBytesValue(new BytesRef(doc.title));
            luceneDoc.add(titleDocsValueField);
        }

        yearField.setIntValue(doc.year);
        luceneDoc.add(yearField);

        yearDocsValueField.setLongValue((long) doc.year);
        luceneDoc.add(yearDocsValueField);

        ratingField.setIntValue(doc.rating);
        luceneDoc.add(ratingField);

        ratingDocsValueField.setLongValue((long) doc.rating);
        luceneDoc.add(ratingDocsValueField);

        positiveField.setIntValue(doc.positive ? 1 : 0);
        luceneDoc.add(positiveField);

        if (doc.review != null && !doc.review.isEmpty()) {
            reviewField.setStringValue(doc.review);
            luceneDoc.add(reviewField);
        }

        if (doc.source != null && !doc.source.isEmpty()) {
            sourceField.setStringValue(doc.source);
            luceneDoc.add(sourceField);
        }

        indexWriter.addDocument(luceneDoc);
    }

    indexWriter.commit();
}

From source file:org.owasp.lucenedroid.Indexer.java

License:MIT License

public void addDocuments(ArrayList<Article> docs) throws IOException {
    // Reuse doc and field instances. See http://wiki.apache.org/lucene-java/ImproveIndexingSpeed
    Field idField = new TextField(ID_FIELD_NAME, "", Field.Store.YES);
    Field titleField = new TextField(TITLE_FIELD_NAME, "", Field.Store.YES);
    Field titleDocsValueField = new SortedDocValuesField(TITLE_FIELD_NAME, new BytesRef(0));
    //    Field yearField = new IntField(YEAR_FIELD_NAME, 0, Field.Store.YES);
    //    Field yearDocsValueField = new NumericDocValuesField(YEAR_FIELD_NAME, 0L);
    //    Field ratingField = new IntField(RATING_FIELD_NAME, 0, Field.Store.YES);
    //    Field ratingDocsValueField = new NumericDocValuesField(RATING_FIELD_NAME, 0L);
    //    Field positiveField = new IntField(POSITIVE_FIELD_NAME, 0, Field.Store.YES);
    //    Field reviewField = new TextField(REVIEW_FIELD_NAME, "", Field.Store.YES);
    //    Field sourceField = new StringField(SOURCE_FIELD_NAME, "", Field.Store.YES);
    Field textField = new TextField(TEXT_FIELD_NAME, "", Field.Store.YES);
    Field categoryField = new StringField(CATEGORY_FIELD_NAME, "", Field.Store.YES);
    Field tagsField = new StringField(TAGS_FIELD_NAME, "", Field.Store.YES);

    for (Article doc : docs) {
        org.apache.lucene.document.Document luceneDoc = new org.apache.lucene.document.Document();

        if (doc.title != null && !doc.title.isEmpty()) {
            titleField.setStringValue(doc.title);
            luceneDoc.add(titleField);/* w w  w .j  a v  a  2s  .  c o m*/

            titleDocsValueField.setBytesValue(new BytesRef(doc.title));
            luceneDoc.add(titleDocsValueField);
        }

        //      yearField.setIntValue(doc.year);
        //      luceneDoc.add(yearField);
        //
        //      yearDocsValueField.setLongValue((long) doc.year);
        //      luceneDoc.add(yearDocsValueField);
        //
        //      ratingField.setIntValue(doc.rating);
        //      luceneDoc.add(ratingField);
        //
        //      ratingDocsValueField.setLongValue((long) doc.rating);
        //      luceneDoc.add(ratingDocsValueField);
        //
        //      positiveField.setIntValue(doc.positive ? 1 : 0);
        //      luceneDoc.add(positiveField);

        //      if (doc.review != null && !doc.review.isEmpty()) {
        //        reviewField.setStringValue(doc.review);
        //        luceneDoc.add(reviewField);
        //      }
        //
        //      if (doc.source != null && !doc.source.isEmpty()) {
        //        sourceField.setStringValue(doc.source);
        //        luceneDoc.add(sourceField);
        //      }

        if (doc.id != null && !doc.id.isEmpty()) {
            idField.setStringValue(doc.id);
            luceneDoc.add(idField);
        }

        if (doc.text != null && !doc.text.isEmpty()) {
            textField.setStringValue(doc.text);
            luceneDoc.add(textField);
        }

        if (doc.category != null && !doc.category.isEmpty()) {
            categoryField.setStringValue(doc.category);
            luceneDoc.add(categoryField);
        }

        if (doc.tags != null && !doc.tags.isEmpty()) {
            tagsField.setStringValue(doc.tags.toString());
            luceneDoc.add(tagsField);
        }

        indexWriter.addDocument(luceneDoc);
    }

    indexWriter.commit();
}

From source file:suonos.lucene.fields.IndexedField.java

License:Apache License

public void setValue(Field field, Object value) {
    if (javaType == String.class) {
        field.setStringValue((String) value);

    } else if (javaType == Double.class) {
        field.setDoubleValue((Double) value);

    } else if (javaType == Float.class) {
        field.setFloatValue((Float) value);

    } else if (javaType == Integer.class) {
        field.setIntValue((Integer) value);

    } else if (javaType == Long.class) {
        field.setLongValue((Long) value);

    } else if (javaType == Boolean.class) {
        field.setStringValue(getBoolValue(value));

    } else if (javaType == Date.class) {
        field.setLongValue(((Date) value).getTime());

    } else if (javaType == Byte[].class) {
        field.setBytesValue((byte[]) value);
    }/*from   w w  w  .j  a  v a  2s .c o m*/

    throw Validate.notAllowed("");
}

From source file:suonos.lucene.Statement.java

License:Apache License

public void saveObject(StoreObject object) throws IOException {

    openSharedWriter();/*from  www. ja  v a 2  s . c o  m*/

    Document doc = new Document();
    Field fld;

    if (object.getId() == null) {
        object.setId(Uids.newUID());
        log.debug("New UID {}", object.getId());
    }

    // Update date.
    //
    fld = context.update$_field();
    fld.setIntValue(MTime.fromCurrentTime());
    doc.add(fld);

    // Document type.
    //
    fld = context.type$_field();
    fld.setStringValue(object.getClass().getSimpleName());
    doc.add(fld);

    // Document object serialized in json format and compressed using
    // snappy.
    //
    fld = context.obj$_field();
    fld.setBytesValue(Snappy.compress(serializeToJson(object).getBytes()));
    doc.add(fld);

    ModelType modelType = context.getModelType(object.getClass());

    modelType.saveToLuceneDoc(context, object, doc);

    Term term = new Term("id", object.getId());

    indexWriter().updateDocument(term, doc);

    // commit();
    // object = queryObject(object.getClass(), object.getUid()).getObject();
}