Example usage for org.apache.lucene.document StoredField StoredField

List of usage examples for org.apache.lucene.document StoredField StoredField

Introduction

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

Prototype

public StoredField(String name, double value) 

Source Link

Document

Create a stored-only field with the given double value.

Usage

From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.FieldSubsetReaderTests.java

License:Open Source License

/**
 * test filtering two stored fields (float)
 */// ww  w  .  j a v  a2 s  .  c  om
public void testStoredFieldsFloat() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    IndexWriter iw = new IndexWriter(dir, iwc);

    // add document with 2 fields
    Document doc = new Document();
    doc.add(new StoredField("fieldA", 1F));
    doc.add(new StoredField("fieldB", 2F));
    iw.addDocument(doc);

    // open reader
    DirectoryReader ir = FieldSubsetReader.wrap(DirectoryReader.open(iw),
            new CharacterRunAutomaton(Automata.makeString("fieldA")));

    // see only one field
    Document d2 = ir.document(0);
    assertEquals(1, d2.getFields().size());
    assertEquals(1F, d2.getField("fieldA").numericValue());

    TestUtil.checkReader(ir);
    IOUtils.close(ir, iw, dir);
}

From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.FieldSubsetReaderTests.java

License:Open Source License

/**
 * test filtering two stored fields (double)
 *///from   w w w. j a  v a 2  s  . c o  m
public void testStoredFieldsDouble() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    IndexWriter iw = new IndexWriter(dir, iwc);

    // add document with 2 fields
    Document doc = new Document();
    doc.add(new StoredField("fieldA", 1D));
    doc.add(new StoredField("fieldB", 2D));
    iw.addDocument(doc);

    // open reader
    DirectoryReader ir = FieldSubsetReader.wrap(DirectoryReader.open(iw),
            new CharacterRunAutomaton(Automata.makeString("fieldA")));

    // see only one field
    Document d2 = ir.document(0);
    assertEquals(1, d2.getFields().size());
    assertEquals(1D, d2.getField("fieldA").numericValue());

    TestUtil.checkReader(ir);
    IOUtils.close(ir, iw, dir);
}

From source file:org.exist.xquery.modules.mpeg7.net.semanticmetadata.lire.impl.ChainedDocumentBuilder.java

License:Open Source License

public Document createDocument(BufferedImage image, String identifier) throws FileNotFoundException {
    docsCreated = true;// ww w. j  a  v  a2 s .c  o  m
    Document doc = new Document();
    if (identifier != null)
        doc.add(new StoredField(DocumentBuilder.FIELD_NAME_IDENTIFIER, identifier));
    if (builders.size() >= 1) {
        for (DocumentBuilder builder : builders) {
            Field[] fields = builder.createDescriptorFields(image);
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                doc.add(field);
            }
        }
    }
    return doc;
}

From source file:org.exist.xquery.modules.mpeg7.net.semanticmetadata.lire.impl.MSERDocumentBuilder.java

License:Open Source License

public Document createDocument(BufferedImage image, String identifier) {
    Document doc = null;// w w w  .  ja v a 2s.  c om
    try {
        // convert to grey ...
        BufferedImage image1 = convertImageToGrey(image);
        // extract features from image:
        List<MSERFeature> features = extractor.computeMSERFeatures(image1);

        ImageUtils.invertImage(image1);
        // invert grey
        features.addAll(extractor.computeMSERFeatures(image1));

        // create new document:
        doc = new Document();
        if (features.size() < 1) {
            System.err.println("No MSER features found for " + identifier);
            //            } else {
            //                System.out.println("features.size() = " + features.size());
        }
        for (Iterator<MSERFeature> fit = features.iterator(); fit.hasNext();) {
            MSERFeature f = fit.next();
            boolean skip = false;
            // add each feature to the document:
            // check first if NaN!!
            for (int j = 0; j < f.descriptor.length; j++) {
                if (Double.isNaN(f.descriptor[j]))
                    skip = true;
                break;
            }

            if (!skip)
                doc.add(new StoredField(DocumentBuilder.FIELD_NAME_MSER, f.getByteArrayRepresentation()));
            else {
                //                    System.err.println("Found NaN in features in file " + identifier + ". ");
            }
        }
        if (identifier != null) {
            doc.add(new StringField(DocumentBuilder.FIELD_NAME_IDENTIFIER, identifier, Field.Store.YES));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return doc;
}

From source file:org.exist.xquery.modules.mpeg7.net.semanticmetadata.lire.indexing.tools.ProximityHashingIndexor.java

License:Open Source License

/**
 * Overwrite this method if you want to filter the input, apply hashing, etc.
 *
 * @param feature          the current feature.
 * @param document         the current document.
 * @param featureFieldName the field hashFunctionsFileName of the feature.
 *///from  www .j  a va 2 s  . c om
protected void addToDocument(LireFeature feature, Document document, String featureFieldName) {
    if (run == 0) {
    } // just count documents
    else if (run == 1) { // Select the representatives ...
        if (representativesID.contains(docCount)
                && feature.getClass().getCanonicalName().equals(featureClass.getCanonicalName())) { // it's a representative.
            // put it into a temporary data structure ...
            representatives.add(feature);
        }
    } else if (run == 2) {
        if (feature.getClass().getCanonicalName().equals(featureClass.getCanonicalName())) { // it's a feature to be hashed
            document.add(new TextField(featureFieldName + "_hash",
                    SerializationUtils.arrayToString(getHashes(feature)), Field.Store.YES));
        }
        document.add(new StoredField(featureFieldName, feature.getByteArrayRepresentation()));
    }
}

From source file:org.frontcache.cache.impl.LuceneIndexManager.java

License:Apache License

/**
 * Writes webResponse to index//  w w w .  jav a 2 s. c o  m
 * @param response
 * @throws IOException
 */
void indexDoc(WebResponse response) throws IOException {

    IndexWriter iWriter = getIndexWriter();

    Document doc = new Document();

    String url = response.getUrl();

    if (null == url) {
        logger.error("URL can't be null during index time for " + response);
        return;
    }

    doc.add(new StringField(URL_FIELD, url, Field.Store.YES));

    doc.add(new StringField(DOMAIN_FIELD, response.getDomain(), Field.Store.YES));

    if (null != response.getContent())
        doc.add(new StoredField(BIN_FIELD, response.getContent()));

    //      doc.add(new NumericDocValuesField(EXPIRE_DATE_FIELD, response.getExpireTimeMillis())); // TODO: store map ?

    doc.add(new StoredField(JSON_FIELD, gson.toJson(response), JSON_TYPE));

    for (String tag : response.getTags())
        doc.add(new StringField(TAGS_FIELD, tag, Field.Store.NO)); // tag is StringField to exact match

    try {
        iWriter.updateDocument(new Term(URL_FIELD, url), doc);
    } catch (IOException e) {
        logger.error("Error while in Lucene index operation: {}", e.getMessage(), e);

    } finally {
        try {
            iWriter.commit();
        } catch (IOException ioEx) {
            logger.error("Error while commiting changes to Lucene index: {}", ioEx.getMessage(), ioEx);
        }
    }

}

From source file:org.geotoolkit.lucene.index.AbstractIndexer.java

License:Open Source License

/**
 * Add a geometric field with a JTS geometry in the specified lucene document.
 * @param doc The lucene document currently building.
 * @param geom A JTS geometry/*from  w ww .j  a  va2  s .c om*/
 */
public NamedEnvelope addGeometry(final Document doc, final Geometry geom, final CoordinateReferenceSystem crs) {
    NamedEnvelope namedBound = null;
    try {
        final String id = doc.get("id");
        namedBound = LuceneUtils.getNamedEnvelope(id, geom, crs);
        rTree.insert(namedBound);
        rTree.getTreeElementMapper().flush();
        rTree.flush();
    } catch (TransformException | FactoryException | MismatchedReferenceSystemException | StoreIndexException
            | IOException ex) {
        LOGGER.log(Level.WARNING, "Unable to insert envelope in R-Tree.", ex);
    }
    doc.add(new StoredField(LuceneOGCFilter.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(geom)));
    return namedBound;
}

From source file:org.geotoolkit.lucene.LuceneEnvelopeOnlyTest.java

License:Open Source License

/**
 * Add a boundingBox geometry to the specified Document.
 *
 * @param doc  The document to add the geometry
 * @param minx the minimun X coordinate of the bounding box.
 * @param maxx the maximum X coordinate of the bounding box.
 * @param miny the minimun Y coordinate of the bounding box.
 * @param maxy the maximum Y coordinate of the bounding box.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 *///from   w  w  w  .jav  a 2 s. co  m
private static NamedEnvelope addBoundingBox(final Document doc, final double minx, final double maxx,
        final double miny, final double maxy, final CoordinateReferenceSystem crs)
        throws FactoryException, TransformException {

    final Geometry poly = LuceneUtils.getPolygon(minx, maxx, miny, maxy, crs);
    final String id = doc.get("id");
    final NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, poly, CommonCRS.defaultGeographic());

    doc.add(new StoredField(LuceneOGCFilter.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(poly)));
    return namedBound;
}

From source file:org.geotoolkit.lucene.LuceneSearcherEnvelopeOnlyTest.java

License:Open Source License

/**
 * Add a boundingBox geometry to the specified Document.
 *
 * @param doc  The document to add the geometry
 * @param minx the minimun X coordinate of the bounding box.
 * @param maxx the maximum X coordinate of the bounding box.
 * @param miny the minimun Y coordinate of the bounding box.
 * @param maxy the maximum Y coordinate of the bounding box.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 *//* w w w  .j a  va 2s. c om*/
private static NamedEnvelope addBoundingBox(final Document doc, final double minx, final double maxx,
        final double miny, final double maxy, final CoordinateReferenceSystem crs)
        throws FactoryException, TransformException {

    final Geometry poly = LuceneUtils.getPolygon(minx, maxx, miny, maxy, crs);
    final String id = doc.get("id");
    final NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, poly, treeCrs);

    doc.add(new StoredField(LuceneOGCFilter.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(poly)));
    return namedBound;
}

From source file:org.geotoolkit.lucene.LuceneSearcherTest.java

License:Open Source License

/**
 * Add a Line geometry to the specified Document.
 *
 * @param doc The document to add the geometry
 * @param x1  the X coordinate of the first point of the line.
 * @param y1  the Y coordinate of the first point of the line.
 * @param x2  the X coordinate of the second point of the line.
 * @param y2  the Y coordinate of the first point of the line.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 *//*from  www  .j av a2 s. c  o  m*/
private static NamedEnvelope addLine(final Document doc, final double x1, final double y1, final double x2,
        final double y2, final CoordinateReferenceSystem crs) throws Exception {

    LineString line = GF.createLineString(new Coordinate[] { new Coordinate(x1, y1), new Coordinate(x2, y2) });
    JTS.setCRS(line, crs);

    final String id = doc.get("id");
    NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, line, treeCrs);
    doc.add(new StoredField(LuceneOGCFilter.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(line)));

    return namedBound;
}