Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for polygon/linestring in results #823

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions app/es_embedded/es/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"coordinate": {
"type": "geo_point"
},
"geometry": {
"type": "geo_shape"
},
"country": {
"properties": {
"default": {
Expand Down
18 changes: 14 additions & 4 deletions app/es_embedded/src/main/java/de/komoot/photon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class Server {
private static final String FIELD_VERSION = "database_version";
private static final String FIELD_LANGUAGES = "indexed_languages";
private static final String FIELD_IMPORT_DATE = "import_date";
private static final String FIELD_SUPPORT_POLYGONS = "support_polygons";

private Node esNode;

Expand Down Expand Up @@ -177,14 +178,18 @@ private void setupDirectories(URL directoryName) throws IOException, URISyntaxEx

}

public DatabaseProperties recreateIndex(String[] languages, Date importDate, boolean supportStructuredQueries) throws IOException {
public DatabaseProperties recreateIndex(String[] languages, Date importDate, boolean supportStructuredQueries, boolean supportPolygons) throws IOException {
deleteIndex();

loadIndexSettings().createIndex(esClient, PhotonIndex.NAME);

createAndPutIndexMapping(languages, supportStructuredQueries);

DatabaseProperties dbProperties = new DatabaseProperties(languages, importDate, false);
DatabaseProperties dbProperties = new DatabaseProperties()
.setLanguages(languages)
.setImportDate(importDate)
.setSupportPolygons(supportPolygons);

saveToDatabase(dbProperties);

return dbProperties;
Expand Down Expand Up @@ -239,6 +244,7 @@ public void saveToDatabase(DatabaseProperties dbProperties) throws IOException
.field(FIELD_VERSION, DATABASE_VERSION)
.field(FIELD_LANGUAGES, String.join(",", dbProperties.getLanguages()))
.field(FIELD_IMPORT_DATE, dbProperties.getImportDate() instanceof Date ? dbProperties.getImportDate().toInstant() : null)
.field(FIELD_SUPPORT_POLYGONS, Boolean.toString(dbProperties.getSupportPolygons()))
.endObject().endObject();

esClient.prepareIndex(PhotonIndex.NAME, PhotonIndex.TYPE).
Expand Down Expand Up @@ -276,11 +282,15 @@ public DatabaseProperties loadFromDatabase() {
}

String langString = properties.get(FIELD_LANGUAGES);

String importDateString = properties.get(FIELD_IMPORT_DATE);

String supportPolygons = properties.get(FIELD_SUPPORT_POLYGONS);

return new DatabaseProperties(langString == null ? null : langString.split(","),
importDateString == null ? null : Date.from(Instant.parse(importDateString)),
false);
importDateString == null ? null : Date.from(Instant.parse(importDateString)),
false,
supportPolygons == null ? null : Boolean.parseBoolean(supportPolygons));
}

public Importer createImporter(String[] languages, String[] extraTags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import de.komoot.photon.Constants;
import de.komoot.photon.PhotonDoc;
import de.komoot.photon.nominatim.model.AddressType;

import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.io.geojson.GeoJsonWriter;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -38,6 +44,17 @@ public static XContentBuilder convert(PhotonDoc doc, String[] languages, String[
.endObject();
}

if (doc.getGeometry() != null) {
GeoJsonWriter g = new GeoJsonWriter();

XContentParser parser = JsonXContent
.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, g.write(doc.getGeometry()));

builder.field("geometry");
builder.copyCurrentStructure(parser);
}

if (doc.getHouseNumber() != null) {
builder.field("housenumber", doc.getHouseNumber());
}
Expand Down
16 changes: 11 additions & 5 deletions app/es_embedded/src/test/java/de/komoot/photon/ESBaseTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import de.komoot.photon.searcher.PhotonResult;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.io.TempDir;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -25,9 +27,9 @@ public class ESBaseTester {

private ElasticTestServer server;

protected PhotonDoc createDoc(double lon, double lat, int id, int osmId, String key, String value) {
protected PhotonDoc createDoc(double lon, double lat, int id, int osmId, String key, String value) throws ParseException {
Point location = FACTORY.createPoint(new Coordinate(lon, lat));
return new PhotonDoc(id, "W", osmId, key, value).names(Collections.singletonMap("name", "berlin")).centroid(location);
return new PhotonDoc(id, "W", osmId, key, value).names(Collections.singletonMap("name", "berlin")).centroid(location).geometry(new WKTReader().read("POLYGON ((6.4440619 52.1969454, 6.4441094 52.1969158, 6.4441408 52.1969347, 6.4441138 52.1969516, 6.4440933 52.1969643, 6.4440619 52.1969454))"));
}

protected PhotonResult getById(int id) {
Expand All @@ -45,17 +47,21 @@ public void tearDown() throws IOException {
}

public void setUpES() throws IOException {
setUpES(dataDirectory, "en");
setUpES(dataDirectory, false,"en");
}

public void setUpESWithPolygons() throws IOException {
setUpES(dataDirectory, true,"en");
}
/**
* Setup the ES server
*
* @throws IOException
*/
public void setUpES(Path testDirectory, String... languages) throws IOException {
public void setUpES(Path testDirectory, boolean supportPolygons, String... languages) throws IOException {
server = new ElasticTestServer(testDirectory.toString());
server.start(TEST_CLUSTER_NAME, new String[]{});
server.recreateIndex(languages, new Date(), false);
server.recreateIndex(languages, new Date(), false, supportPolygons);
refresh();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected PhotonDoc createDoc(double lon, double lat, int id, int osmId, String

@BeforeAll
void setUp() throws Exception {
setUpES(instanceTestDirectory, "en", "de", "fr", "it");
setUpES(instanceTestDirectory, false, "en", "de", "fr", "it");
Importer instance = getServer().createImporter(new String[]{"en", "de", "fr", "it"},
new String[]{"population", "capital"});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void testSaveAndLoadFromDatabase() throws IOException {
setUpES();

Date now = new Date();
DatabaseProperties prop = new DatabaseProperties(new String[]{"en", "de", "fr"}, now, false);
DatabaseProperties prop = new DatabaseProperties(new String[]{"en", "de", "fr"}, now, false, false);
getServer().saveToDatabase(prop);

prop = getServer().loadFromDatabase();
Expand Down
7 changes: 4 additions & 3 deletions app/opensearch/src/main/java/de/komoot/photon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void shutdown() {
}
}

public DatabaseProperties recreateIndex(String[] languages, Date importDate, boolean supportStructuredQueries) throws IOException {
public DatabaseProperties recreateIndex(String[] languages, Date importDate, boolean supportStructuredQueries, boolean supportPolygons) throws IOException {
// delete any existing data
if (client.indices().exists(e -> e.index(PhotonIndex.NAME)).value()) {
client.indices().delete(d -> d.index(PhotonIndex.NAME));
Expand All @@ -119,7 +119,7 @@ public DatabaseProperties recreateIndex(String[] languages, Date importDate, boo

(new IndexMapping(supportStructuredQueries)).addLanguages(languages).putMapping(client, PhotonIndex.NAME);

var dbProperties = new DatabaseProperties(languages, importDate, supportStructuredQueries);
var dbProperties = new DatabaseProperties(languages, importDate, supportStructuredQueries, supportPolygons);
saveToDatabase(dbProperties);

return dbProperties;
Expand Down Expand Up @@ -163,7 +163,8 @@ public DatabaseProperties loadFromDatabase() throws IOException {

return new DatabaseProperties(dbEntry.source().languages,
dbEntry.source().importDate,
dbEntry.source().supportStructuredQueries);
dbEntry.source().supportStructuredQueries,
dbEntry.source().supportPolygons);
}

public Importer createImporter(String[] languages, String[] extraTags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DBPropertyEntry {
public Date importDate;
public String[] languages;
public boolean supportStructuredQueries;
public boolean supportPolygons;

public DBPropertyEntry() {}

Expand All @@ -17,5 +18,6 @@ public DBPropertyEntry(DatabaseProperties props, String databaseVersion) {
importDate = props.getImportDate();
languages = props.getLanguages();
supportStructuredQueries = props.getSupportStructuredQueries();
supportPolygons = props.getSupportPolygons();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void setupBaseMappings() {
}

mappings.properties("coordinate", b -> b.geoPoint(p -> p));
mappings.properties("geometry", b -> b.geoShape(p -> p));
mappings.properties("countrycode", b -> b.keyword(p -> p.index(true)));
mappings.properties("importance", b -> b.float_(p -> p.index(false)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.komoot.photon.PhotonDoc;
import de.komoot.photon.Utils;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.io.geojson.GeoJsonWriter;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -48,6 +49,11 @@ public void serialize(PhotonDoc value, JsonGenerator gen, SerializerProvider pro
gen.writeEndObject();
}

if (value.getGeometry() != null) {
GeoJsonWriter g = new GeoJsonWriter();
gen.writeObjectField("geometry", g.write(value.getGeometry()));
}

if (value.getHouseNumber() != null) {
gen.writeStringField("housenumber", value.getHouseNumber());
}
Expand Down
22 changes: 14 additions & 8 deletions app/opensearch/src/test/java/de/komoot/photon/ESBaseTester.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.komoot.photon;

import de.komoot.photon.opensearch.Importer;
import de.komoot.photon.opensearch.OpenSearchTestServer;
import de.komoot.photon.opensearch.Updater;
import de.komoot.photon.searcher.PhotonResult;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -43,34 +45,38 @@ protected PhotonResult getById(String id) {
}

public void setUpES() throws IOException {
setUpES(dataDirectory, "en");
setUpES(dataDirectory, false,"en");
}

public void setUpES(Path testDirectory, String... languages) throws IOException {
public void setUpESWithPolygons() throws IOException {
setUpES(dataDirectory, true,"en");
}

public void setUpES(Path testDirectory, boolean supportPolygons, String... languages) throws IOException {
server = new OpenSearchTestServer(testDirectory.toString());
server.startTestServer(TEST_CLUSTER_NAME);
server.recreateIndex(languages, new Date(), true);
server.recreateIndex(languages, new Date(), true, supportPolygons);
server.refreshIndexes();
}

protected Importer makeImporter() {
return server.createImporter(new String[]{"en"}, new String[]{});
return (Importer) server.createImporter(new String[]{"en"}, new String[]{});
}

protected Importer makeImporterWithExtra(String... extraTags) {
return server.createImporter(new String[]{"en"}, extraTags);
return (Importer) server.createImporter(new String[]{"en"}, extraTags);
}

protected Importer makeImporterWithLanguages(String... languages) {
return server.createImporter(languages, new String[]{});
return (Importer) server.createImporter(languages, new String[]{});
}

protected Updater makeUpdater() {
return server.createUpdater(new String[]{"en"}, new String[]{});
return (Updater) server.createUpdater(new String[]{"en"}, new String[]{});
}

protected Updater makeUpdaterWithExtra(String... extraTags) {
return server.createUpdater(new String[]{"en"}, extraTags);
return (Updater) server.createUpdater(new String[]{"en"}, extraTags);
}

protected Server getServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ void testSaveAndLoadFromDatabase() throws IOException {
Date now = new Date();
DatabaseProperties prop = new DatabaseProperties(new String[]{"en", "de", "fr"},
now,
false);
false,
false);
getServer().saveToDatabase(prop);

prop = getServer().loadFromDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static int getRank(AddressType type) {

@BeforeEach
void setUp() throws Exception {
setUpES(instanceTestDirectory, LANGUAGE, "de", "fr");
setUpES(instanceTestDirectory, false, LANGUAGE, "de", "fr");
Importer instance = makeImporter();

var country = new PhotonDoc(0, "R", 0, "place", "country")
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/shared.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ application {
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"
}

repositories {
Expand Down Expand Up @@ -52,6 +54,7 @@ dependencies {
exclude(module: 'commons-logging')
}
implementation 'org.locationtech.jts:jts-core:1.20.0'
implementation 'org.locationtech.jts.io:jts-io-common:1.20.0'
implementation 'com.sparkjava:spark-core:2.9.4'
implementation 'net.postgis:postgis-jdbc:2024.1.0'
implementation 'org.json:json:20240303'
Expand Down
Loading
Loading