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));
}
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;
}
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);
}
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);
}
}
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 + ".");
}
Aggregations