Search in sources :

Example 1 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class LuceneOGCWeight method scorer.

@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
    // final SortedDocValues values = context.reader().getSortedDocValues(IDENTIFIER_FIELD_NAME);
    final LeafReader reader = context.reader();
    boolean treeSearch = false;
    boolean reverse = false;
    boolean distanceFilter = false;
    final Set<String> treeMatching = new HashSet<>();
    if (tree != null) {
        /*
             * For distance buffer filter no envelope only mode
             */
        List<Expression<Object, ?>> expressions = filter.getExpressions();
        Expression e2 = (expressions.size() >= 2) ? expressions.get(1) : null;
        if (filter instanceof DistanceOperator) {
            distanceFilter = true;
            reverse = filter.getOperatorType() == DistanceOperatorName.BEYOND;
            final DistanceOperator sp = (DistanceOperator) filter;
            if (e2 instanceof Literal) {
                try {
                    final Literal lit = (Literal) e2;
                    Quantity distance = sp.getDistance();
                    final GeneralEnvelope bound = getExtendedReprojectedEnvelope(lit.getValue(), tree.getCrs(), distance.getUnit().toString(), distance.getValue().doubleValue());
                    final int[] resultID = tree.searchID(bound);
                    Arrays.sort(resultID);
                    treeMatching.clear();
                    TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                    for (int id : resultID) {
                        final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                        if (env != null) {
                            treeMatching.add(env.getId());
                        }
                    }
                    treeSearch = true;
                } catch (FactoryException ex) {
                    throw new IOException(ex);
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", e2);
            }
        } else if (filter instanceof BinarySpatialOperator) {
            final BinarySpatialOperator sp = (BinarySpatialOperator) filter;
            if (e2 instanceof Literal) {
                final Literal lit = (Literal) e2;
                final Envelope boundFilter = getReprojectedEnvelope(lit.getValue(), tree.getCrs());
                try {
                    if (filterType == SpatialFilterType.CROSSES || !envelopeOnly) {
                        if (filterType == SpatialFilterType.DISJOINT) {
                            reverse = true;
                        }
                        final int[] resultID = tree.searchID(boundFilter);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                        envelopeOnly = false;
                    } else {
                        final int[] resultID = TreeX.search(tree, boundFilter, filterType);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                    }
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", e2);
            }
        } else {
            LOGGER.log(Level.WARNING, "not a spatial operator:{0}", filter.getClass().getName());
        }
    } else {
        LOGGER.finer("Null R-tree in spatial search");
    }
    final BitSet set = new FixedBitSet(reader.maxDoc());
    Bits b = reader.getLiveDocs();
    if (b == null) {
        b = new Bits.MatchAllBits(reader.maxDoc());
    }
    for (int i = 0; i < b.length(); i++) {
        if (b.get(i)) {
            final int docId = i;
            final Document doc = reader.document(docId, ID_FIELDS);
            final String id = doc.get(IDENTIFIER_FIELD_NAME);
            final boolean match = treeMatching.contains(id);
            if (treeSearch && reverse && !match) {
                set.set(docId);
            } else if (!treeSearch || match) {
                if (envelopeOnly && !distanceFilter) {
                    set.set(docId);
                } else {
                    final Document geoDoc = reader.document(docId, GEOMETRY_FIELDS);
                    if (filter.test(geoDoc)) {
                        set.set(docId);
                    }
                }
            }
        }
    }
    return new ConstantScoreScorer(this, boost, scoreMode, new BitSetIterator(set, 5));
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) FactoryException(org.opengis.util.FactoryException) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) LuceneUtils.getReprojectedEnvelope(org.geotoolkit.lucene.LuceneUtils.getReprojectedEnvelope) LuceneUtils.getExtendedReprojectedEnvelope(org.geotoolkit.lucene.LuceneUtils.getExtendedReprojectedEnvelope) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) Document(org.apache.lucene.document.Document) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) FixedBitSet(org.apache.lucene.util.FixedBitSet) Literal(org.opengis.filter.Literal) HashSet(java.util.HashSet) BinarySpatialOperator(org.opengis.filter.BinarySpatialOperator) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) BitSetIterator(org.apache.lucene.util.BitSetIterator) LeafReader(org.apache.lucene.index.LeafReader) DistanceOperator(org.opengis.filter.DistanceOperator) FixedBitSet(org.apache.lucene.util.FixedBitSet) BitSet(org.apache.lucene.util.BitSet) Quantity(javax.measure.Quantity) IOException(java.io.IOException) Expression(org.opengis.filter.Expression) TreeElementMapper(org.geotoolkit.index.tree.TreeElementMapper) Bits(org.apache.lucene.util.Bits) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 2 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class AbstractIndexer method addGeometry.

/**
 * 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
 */
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(LuceneOGCSpatialQuery.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(geom)));
    return namedBound;
}
Also used : MismatchedReferenceSystemException(org.opengis.geometry.MismatchedReferenceSystemException) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) StoredField(org.apache.lucene.document.StoredField) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) FactoryException(org.opengis.util.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException)

Example 3 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class AbstractIndexer method createIndex.

/**
 * Create a new Index with the specified list of object.
 *
 * @param toIndex objects to index.
 * @throws IndexingException
 */
public void createIndex(final List<E> toIndex) throws IndexingException {
    LOGGER.log(logLevel, "Creating lucene index for please wait...");
    final long time = System.currentTimeMillis();
    int nbEntries = 0;
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final String serviceID = getServiceID();
    try (final IndexWriter writer = new IndexWriter(LuceneUtils.getAppropriateDirectory(getFileDirectory()), conf)) {
        resetTree();
        nbEntries = toIndex.size();
        for (E entry : toIndex) {
            if (!stopIndexing && !indexationToStop.contains(serviceID)) {
                indexDocument(writer, entry);
            } else {
                LOGGER.info("Index creation stopped after " + (System.currentTimeMillis() - time) + " ms for service:" + serviceID);
                stopIndexation(serviceID);
                return;
            }
        }
        // we store the numeric fields in a properties file int the index directory
        storeNumericFieldsFile();
    } catch (IOException | StoreIndexException | SQLException ex) {
        LOGGER.log(Level.WARNING, IO_SINGLE_MSG, ex);
    }
    LOGGER.log(logLevel, "Index creation process in " + (System.currentTimeMillis() - time) + " ms\n" + " documents indexed: " + nbEntries);
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) IndexWriter(org.apache.lucene.index.IndexWriter) SQLException(java.sql.SQLException) IOException(java.io.IOException) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 4 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class AbstractIndexer method indexDocument.

/**
 * This method add to index of lucene a new document.
 *
 * @param meta The object to index.
 */
public void indexDocument(final E meta) {
    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    try (final IndexWriter writer = new IndexWriter(LuceneUtils.getAppropriateDirectory(getFileDirectory()), config)) {
        final int docId = writer.getDocStats().maxDoc;
        // adding the document in a specific model. in this case we use a MDwebDocument.
        writer.addDocument(createDocument(meta, docId));
        LOGGER.log(Level.FINER, "Metadata: {0} indexed", getIdentifier(meta));
        if (rTree != null) {
            rTree.getTreeElementMapper().flush();
            rTree.flush();
        }
    } catch (IndexingException | StoreIndexException ex) {
        LOGGER.log(Level.WARNING, "Error while indexing single document", ex);
    } catch (IOException ex) {
        LOGGER.log(Level.WARNING, IO_SINGLE_MSG + ex.getMessage(), ex);
    }
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) IndexingException(org.geotoolkit.index.IndexingException) IndexWriter(org.apache.lucene.index.IndexWriter) IOException(java.io.IOException) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 5 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class AbstractIndexer method createIndex.

/**
 * Create a new Index.
 *
 * @throws IndexingException
 */
public void createIndex() throws IndexingException {
    LOGGER.log(logLevel, "(light memory) Creating lucene index please wait...");
    final long time = System.currentTimeMillis();
    int nbEntries = 0;
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final String serviceID = getServiceID();
    try (final IndexWriter writer = new IndexWriter(LuceneUtils.getAppropriateDirectory(getFileDirectory()), conf)) {
        resetTree();
        LOGGER.log(logLevel, "starting indexing...");
        if (useEntryIterator()) {
            final Iterator<E> entries = getEntryIterator();
            while (entries.hasNext()) {
                if (!stopIndexing && !indexationToStop.contains(serviceID)) {
                    try {
                        final E entry = entries.next();
                        indexDocument(writer, entry);
                    } catch (IndexingException ex) {
                        LOGGER.warning("Error while indexing document. moving to next.");
                    }
                    nbEntries++;
                } else {
                    LOGGER.info("Index creation stopped after " + (System.currentTimeMillis() - time) + " ms for service:" + serviceID);
                    stopIndexation(serviceID);
                    return;
                }
            }
            if (entries instanceof CloseableIterator) {
                ((CloseableIterator) entries).close();
            }
        } else {
            final Iterator<String> identifiers = getIdentifierIterator();
            while (identifiers.hasNext()) {
                final String identifier = identifiers.next();
                if (!stopIndexing && !indexationToStop.contains(serviceID)) {
                    try {
                        final E entry = getEntry(identifier);
                        indexDocument(writer, entry);
                        nbEntries++;
                    } catch (IndexingException ex) {
                        LOGGER.log(Level.WARNING, "Metadata IO exeption while indexing metadata: " + identifier + " " + ex.getMessage() + "\nmove to next metadata...", ex);
                    }
                } else {
                    LOGGER.info("Index creation stopped after " + (System.currentTimeMillis() - time) + " ms for service:" + serviceID);
                    stopIndexation(serviceID);
                    return;
                }
            }
            if (identifiers instanceof CloseableIterator) {
                ((CloseableIterator) identifiers).close();
            }
        }
        // we store the numeric fields in a properties file int the index directory
        storeNumericFieldsFile();
    } catch (IOException | StoreIndexException | SQLException ex) {
        LOGGER.log(Level.SEVERE, IO_SINGLE_MSG + "{0}", ex.getMessage());
        throw new IndexingException("IOException while indexing documents:" + ex.getMessage(), ex);
    }
    LOGGER.log(logLevel, "Index creation process in " + (System.currentTimeMillis() - time) + " ms\n documents indexed: " + nbEntries + ".");
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) CloseableIterator(org.geotoolkit.util.collection.CloseableIterator) SQLException(java.sql.SQLException) IOException(java.io.IOException) IndexingException(org.geotoolkit.index.IndexingException) IndexWriter(org.apache.lucene.index.IndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IOException (java.io.IOException)10 StoreIndexException (org.geotoolkit.index.tree.StoreIndexException)10 SQLException (java.sql.SQLException)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 NamedEnvelope (org.geotoolkit.index.tree.manager.NamedEnvelope)3 Path (java.nio.file.Path)2 IndexingException (org.geotoolkit.index.IndexingException)2 TreeElementMapper (org.geotoolkit.index.tree.TreeElementMapper)2 FactoryException (org.opengis.util.FactoryException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Quantity (javax.measure.Quantity)1 Document (org.apache.lucene.document.Document)1 StoredField (org.apache.lucene.document.StoredField)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 LeafReader (org.apache.lucene.index.LeafReader)1 Term (org.apache.lucene.index.Term)1 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)1