Search in sources :

Example 1 with DocumentEnvelope

use of org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope in project geotoolkit by Geomatys.

the class LuceneTest method fillTestData.

private static List<DocumentEnvelope> fillTestData() throws Exception {
    final List<DocumentEnvelope> docs = new ArrayList<>();
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document doc = new Document();
    doc.add(new StringField("id", "point 1", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addPoint(doc, -10, 10, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "point 1 projected", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    addPoint(doc, -1111475.102852225, 1113194.9079327357, CRS3395);
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "point 2", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addPoint(doc, -10, 0, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "point 3", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addPoint(doc, 0, 0, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "point 4", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addPoint(doc, 40, 20, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "point 5", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addPoint(doc, -40, 30, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 1", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, -40, -25, -50, -40, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 2", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 5, 10, 10, 15, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 2 projected", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    addBoundingBox(doc, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 3", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 30, 50, 0, 15, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 4", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, -30, -15, 0, 10, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 5", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 44.792, 51.126, -6.171, -2.28, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "line 1", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addLine(doc, 0, 0, 25, 0, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "line 1 projected", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    addLine(doc, 0, 0, 2857692.6111605316, 0, CRS3395);
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "line 2", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addLine(doc, 0, 0, 0, -15, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    return docs;
}
Also used : StringField(org.apache.lucene.document.StringField) ArrayList(java.util.ArrayList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Document(org.apache.lucene.document.Document) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Example 2 with DocumentEnvelope

use of org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope in project geotoolkit by Geomatys.

the class LuceneSearcherEnvelopeOnlyTest method QueryAndSpatialFilterAfterRemoveTest.

/**
 * Test the combination of a String query and/or spatial filter.
 * @throws java.lang.Exception
 */
@Test
public void QueryAndSpatialFilterAfterRemoveTest() throws Exception {
    org.opengis.filter.Filter bboxFilter = FF.bbox(GEOMETRY_PROPERTY, -20, -20, 20, 20, "CRS:84");
    SpatialQuery bboxQuery = new SpatialQuery(wrap(bboxFilter));
    Set<String> results = searcher.doSearch(bboxQuery);
    int nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    /*
         * we remove a document
         */
    final Analyzer analyzer = new StandardAnalyzer();
    DocumentIndexer indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.removeDocument("box 2 projected");
    indexer.destroy();
    searcher.destroy();
    searcher = new LuceneIndexSearcher(directory, null, new ClassicAnalyzer(), true);
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 2);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    // re-add the document
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document doc = new Document();
    doc.add(new StringField("id", "box 2 projected", Field.Store.YES));
    doc.add(new StringField("docid", 66 + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    NamedEnvelope env = addBoundingBox(doc, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    // attention !! reprojeté
    envelopes.put("box 2 projected", env);
    indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.indexDocument(new DocumentEnvelope(doc, env));
    indexer.destroy();
    searcher.destroy();
    searcher = new LuceneIndexSearcher(directory, null, new StandardAnalyzer(), true);
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LuceneIndexSearcher(org.geotoolkit.lucene.index.LuceneIndexSearcher) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) ClassicAnalyzer(org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer) Document(org.apache.lucene.document.Document) ClassicAnalyzer(org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StringField(org.apache.lucene.document.StringField) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Example 3 with DocumentEnvelope

use of org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope in project geotoolkit by Geomatys.

the class LuceneEnvelopeOnlyTest method fillTestData.

private static List<DocumentEnvelope> fillTestData() throws Exception {
    final List<DocumentEnvelope> docs = new ArrayList<>();
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document doc = new Document();
    doc.add(new StringField("id", "box 1", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, -40, -25, -50, -40, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 2", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 5, 10, 10, 15, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 2 projected", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    addBoundingBox(doc, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 3", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 30, 50, 0, 15, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 4", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, -30, -15, 0, 10, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    doc = new Document();
    doc.add(new StringField("id", "box 5", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    addBoundingBox(doc, 44.792, 51.126, -6.171, -2.28, CommonCRS.defaultGeographic());
    docs.add(new DocumentEnvelope(doc, null));
    return docs;
}
Also used : StringField(org.apache.lucene.document.StringField) ArrayList(java.util.ArrayList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Document(org.apache.lucene.document.Document) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Example 4 with DocumentEnvelope

use of org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope in project geotoolkit by Geomatys.

the class LuceneEnvelopeOnlyTest method QueryAndSpatialFilterAfterRemoveTest.

/**
 * Test the combination of a String query and/or spatial filter.
 */
@Test
public void QueryAndSpatialFilterAfterRemoveTest() throws Exception {
    // we remove a document
    final Analyzer analyzer = new StandardAnalyzer();
    DocumentIndexer indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.removeDocument("box 2 projected");
    indexer.destroy();
    DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory);
    Path firstChild = directoryStream.iterator().next();
    directoryStream.close();
    IndexReader reader = DirectoryReader.open(LuceneUtils.getAppropriateDirectory(firstChild));
    searcher = new IndexSearcher(reader);
    /*
         * case 1: a normal spatial request BBOX
         */
    double[] min1 = { -20, -20 };
    double[] max1 = { 20, 20 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    org.opengis.filter.Filter bboxFilter = FF.bbox(GEOMETRY_PROPERTY, -20, -20, 20, 20, "CRS:84");
    SpatialQuery bboxQuery = new SpatialQuery(wrap(bboxFilter));
    BooleanQuery serialQuery = new BooleanQuery.Builder().add(bboxQuery.getQuery(), BooleanClause.Occur.MUST).add(simpleQuery, BooleanClause.Occur.MUST).build();
    // we perform a lucene query
    TopDocs docs = searcher.search(serialQuery, 15);
    TotalHits nbResults = docs.totalHits;
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    List<String> results = new ArrayList<>();
    for (int i = 0; i < nbResults.value; i++) {
        Document doc = searcher.doc(docs.scoreDocs[i].doc);
        String name = doc.get("id");
        results.add(name);
        LOGGER.log(Level.FINER, "\tid: {0}", name);
    }
    // we verify that we obtain the correct results
    assertEquals(2, nbResults.value);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    // re-add the document
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document docu = new Document();
    docu.add(new StringField("id", "box 2 projected", Field.Store.YES));
    docu.add(new StringField("docid", 66 + "", Field.Store.YES));
    docu.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    addBoundingBox(docu, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.indexDocument(new DocumentEnvelope(docu, null));
    indexer.destroy();
    directoryStream = Files.newDirectoryStream(directory);
    firstChild = directoryStream.iterator().next();
    directoryStream.close();
    reader = DirectoryReader.open(LuceneUtils.getAppropriateDirectory(firstChild));
    searcher = new IndexSearcher(reader);
    // we perform a lucene query
    docs = searcher.search(serialQuery, 15);
    nbResults = docs.totalHits;
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    results = new ArrayList<>();
    for (int i = 0; i < nbResults.value; i++) {
        Document doc = searcher.doc(docs.scoreDocs[i].doc);
        String name = doc.get("id");
        results.add(name);
        LOGGER.log(Level.FINER, "\tid: {0}", name);
    }
    // we verify that we obtain the correct results
    assertEquals(3, nbResults.value);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TotalHits(org.apache.lucene.search.TotalHits) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) ArrayList(java.util.ArrayList) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) ClassicAnalyzer(org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope) Path(java.nio.file.Path) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 5 with DocumentEnvelope

use of org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope in project geotoolkit by Geomatys.

the class LuceneSearcherEnvelopeOnlyTest method fillTestData.

private static List<DocumentEnvelope> fillTestData() throws Exception {
    final List<DocumentEnvelope> docs = new ArrayList<>();
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document doc = new Document();
    doc.add(new StringField("id", "box 1", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    NamedEnvelope env = addBoundingBox(doc, -40, -25, -50, -40, CommonCRS.defaultGeographic());
    envelopes.put("box 1", env);
    docs.add(new DocumentEnvelope(doc, env));
    doc = new Document();
    doc.add(new StringField("id", "box 2", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    env = addBoundingBox(doc, 5, 10, 10, 15, CommonCRS.defaultGeographic());
    envelopes.put("box 2", env);
    docs.add(new DocumentEnvelope(doc, env));
    doc = new Document();
    doc.add(new StringField("id", "box 2 projected", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    env = addBoundingBox(doc, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    // attention !! reprojeté
    envelopes.put("box 2 projected", env);
    docs.add(new DocumentEnvelope(doc, env));
    doc = new Document();
    doc.add(new StringField("id", "box 3", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    env = addBoundingBox(doc, 30, 50, 0, 15, CommonCRS.defaultGeographic());
    envelopes.put("box 3", env);
    docs.add(new DocumentEnvelope(doc, env));
    doc = new Document();
    doc.add(new StringField("id", "box 4", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    env = addBoundingBox(doc, -30, -15, 0, 10, CommonCRS.defaultGeographic());
    envelopes.put("box 4", env);
    docs.add(new DocumentEnvelope(doc, env));
    doc = new Document();
    doc.add(new StringField("id", "box 5", Field.Store.YES));
    doc.add(new StringField("docid", docs.size() + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    env = addBoundingBox(doc, 44.792, 51.126, -6.171, -2.28, CommonCRS.defaultGeographic());
    envelopes.put("box 5", env);
    docs.add(new DocumentEnvelope(doc, env));
    return docs;
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) StringField(org.apache.lucene.document.StringField) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Document(org.apache.lucene.document.Document) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Aggregations

Document (org.apache.lucene.document.Document)8 DocumentEnvelope (org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)8 StringField (org.apache.lucene.document.StringField)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 ArrayList (java.util.ArrayList)5 NamedEnvelope (org.geotoolkit.index.tree.manager.NamedEnvelope)4 Analyzer (org.apache.lucene.analysis.Analyzer)3 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)3 ClassicAnalyzer (org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer)3 LuceneOGCSpatialQuery (org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery)3 SpatialQuery (org.geotoolkit.lucene.filter.SpatialQuery)3 Path (java.nio.file.Path)2 IndexReader (org.apache.lucene.index.IndexReader)2 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)2 Field (org.apache.lucene.document.Field)1 StoredField (org.apache.lucene.document.StoredField)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 SortField (org.apache.lucene.search.SortField)1 TopDocs (org.apache.lucene.search.TopDocs)1