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 new import option '-extra-tags' #576

Merged
merged 3 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/de/komoot/photon/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static void startRecreatingIndex(Server esServer) {
private static void startJsonDump(CommandLineArgs args) {
try {
final String filename = args.getJsonDump();
final JsonDumper jsonDumper = new JsonDumper(filename, args.getLanguages());
final JsonDumper jsonDumper = new JsonDumper(filename, args.getLanguages(), args.getExtraTags());
NominatimConnector nominatimConnector = new NominatimConnector(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
nominatimConnector.setImporter(jsonDumper);
nominatimConnector.readEntireDatabase(args.getCountryCodes().split(","));
Expand All @@ -133,7 +133,7 @@ private static void startNominatimImport(CommandLineArgs args, Server esServer,
}

log.info("starting import from nominatim to photon with languages: " + args.getLanguages());
de.komoot.photon.elasticsearch.Importer importer = new de.komoot.photon.elasticsearch.Importer(esNodeClient, args.getLanguages());
de.komoot.photon.elasticsearch.Importer importer = new de.komoot.photon.elasticsearch.Importer(esNodeClient, args.getLanguages(), args.getExtraTags());
NominatimConnector nominatimConnector = new NominatimConnector(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
nominatimConnector.setImporter(importer);
nominatimConnector.readEntireDatabase(args.getCountryCodes().split(","));
Expand All @@ -149,7 +149,7 @@ private static void startNominatimImport(CommandLineArgs args, Server esServer,
*/
private static NominatimUpdater setupNominatimUpdater(CommandLineArgs args, Client esNodeClient) {
NominatimUpdater nominatimUpdater = new NominatimUpdater(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
Updater updater = new de.komoot.photon.elasticsearch.Updater(esNodeClient, args.getLanguages());
Updater updater = new de.komoot.photon.elasticsearch.Updater(esNodeClient, args.getLanguages(), args.getExtraTags());
nominatimUpdater.setUpdater(updater);
return nominatimUpdater;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/de/komoot/photon/CommandLineArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class CommandLineArgs {
@Parameter(names = "-country-codes", description = "country codes filter that nominatim importer should import, comma separated. If empty full planet is done")
private String countryCodes = "";

@Parameter(names = "-extra-tags", description = "additional tags to save for each place")
private String extraTags = "";

@Parameter(names = "-json", description = "import nominatim database and dump it to a json like files in (useful for developing)")
private String jsonDump = null;

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/komoot/photon/JsonDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
public class JsonDumper implements Importer {
private PrintWriter writer = null;
private final String[] languages;
private final String[] extraTags;

public JsonDumper(String filename, String languages) throws FileNotFoundException {
public JsonDumper(String filename, String languages, String extraTags) throws FileNotFoundException {
this.writer = new PrintWriter(filename);
this.languages = languages.split(",");
this.extraTags = extraTags.split(",");
}

@Override
public void add(PhotonDoc doc) {
try {
writer.println("{\"index\": {}}");
writer.println(Utils.convert(doc, this.languages).string());
writer.println(Utils.convert(doc, languages, extraTags).string());
} catch (IOException e) {
log.error("error writing json file", e);
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/de/komoot/photon/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Utils {
private static final Joiner commaJoiner = Joiner.on(", ").skipNulls();

public static XContentBuilder convert(PhotonDoc doc, String[] languages) throws IOException {
public static XContentBuilder convert(PhotonDoc doc, String[] languages, String[] extraTags) throws IOException {
final AddressType atype = doc.getAddressType();
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.field(Constants.OSM_ID, doc.getOsmId())
Expand Down Expand Up @@ -56,6 +56,7 @@ public static XContentBuilder convert(PhotonDoc doc, String[] languages) throws
if (countryCode != null)
builder.field(Constants.COUNTRYCODE, countryCode.getAlpha2());
writeContext(builder, doc.getContext(), languages);
writeExtraTags(builder, doc.getExtratags(), extraTags);
writeExtent(builder, doc.getBbox());

builder.endObject();
Expand All @@ -64,6 +65,25 @@ public static XContentBuilder convert(PhotonDoc doc, String[] languages) throws
return builder;
}

private static void writeExtraTags(XContentBuilder builder, Map<String, String> docTags, String[] extraTags) throws IOException {
boolean foundTag = false;

for (String tag: extraTags) {
String value = docTags.get(tag);
if (value != null) {
if (!foundTag) {
builder.startObject("extra");
foundTag = true;
}
builder.field(tag, value);
}
}

if (foundTag) {
builder.endObject();
}
}

private static void writeExtent(XContentBuilder builder, Envelope bbox) throws IOException {
if (bbox == null) return;

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/komoot/photon/elasticsearch/Importer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ public class Importer implements de.komoot.photon.Importer {
private final Client esClient;
private BulkRequestBuilder bulkRequest;
private final String[] languages;
private final String[] extraTags;

public Importer(Client esClient, String languages) {
public Importer(Client esClient, String languages, String extraTags) {
this.esClient = esClient;
this.bulkRequest = esClient.prepareBulk();
this.languages = languages.split(",");
this.extraTags = extraTags.split(",");
}

@Override
public void add(PhotonDoc doc) {
try {
this.bulkRequest.add(this.esClient.prepareIndex(PhotonIndex.NAME, PhotonIndex.TYPE).
setSource(Utils.convert(doc, languages)).setId(doc.getUid()));
setSource(Utils.convert(doc, languages, extraTags)).setId(doc.getUid()));
} catch (IOException e) {
log.error("could not bulk add document " + doc.getUid(), e);
return;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/komoot/photon/elasticsearch/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class Server {
private File esDirectory;

private final String[] languages;
private String[] extraTags = new String[0];

private String transportAddresses;

Expand All @@ -62,6 +63,9 @@ public MyNode(Settings preparedSettings, Collection<Class<? extends Plugin>> cla

public Server(CommandLineArgs args) {
this(args.getCluster(), args.getDataDirectory(), args.getLanguages(), args.getTransportAddresses());
if (args.getExtraTags().length() > 0) {
this.extraTags = args.getExtraTags().split(",");
}
}

public Server(String clusterName, String mainDirectory, String languages, String transportAddresses) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/komoot/photon/elasticsearch/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class Updater implements de.komoot.photon.Updater {
private final Client esClient;
private BulkRequestBuilder bulkRequest;
private final String[] languages;
private final String[] extraTags;

public Updater(Client esClient, String languages) {
public Updater(Client esClient, String languages, String extraTags) {
this.esClient = esClient;
this.bulkRequest = esClient.prepareBulk();
this.languages = languages.split(",");
this.extraTags = extraTags.split(",");
}

public void finish() {
Expand All @@ -33,7 +35,7 @@ public void finish() {
@Override
public void create(PhotonDoc doc) {
try {
this.bulkRequest.add(this.esClient.prepareIndex(PhotonIndex.NAME, PhotonIndex.TYPE).setSource(Utils.convert(doc, this.languages)).setId(String.valueOf(doc.getPlaceId())));
bulkRequest.add(esClient.prepareIndex(PhotonIndex.NAME, PhotonIndex.TYPE).setSource(Utils.convert(doc, languages, extraTags)).setId(String.valueOf(doc.getPlaceId())));
} catch (IOException e) {
log.error(String.format("creation of new doc [%s] failed", doc), e);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/komoot/photon/utils/ConvertToJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public List<JSONObject> convert(SearchResponse searchResponse) {
properties.put("extent", new JSONArray(Lists.newArrayList(nw.get(0), nw.get(1), se.get(0), se.get(1))));
}

final Map<String, String> extraTags = (Map<String, String>) source.get("extra");
if (extraTags != null) {
properties.put("extra", extraTags);
}

feature.put(Constants.PROPERTIES, properties);

list.add(feature);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/komoot/photon/ApiIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ApiIntegrationTest extends ESBaseTester {
@Before
public void setUp() throws Exception {
setUpES();
Importer instance = new Importer(getClient(), "en");
Importer instance = new Importer(getClient(), "en", "");
instance.add(createDoc(13.38886, 52.51704, 1000, 1000, "place", "city"));
instance.add(createDoc(13.39026, 52.54714, 1001, 1001, "place", "town"));
instance.finish();
Expand Down
51 changes: 48 additions & 3 deletions src/test/java/de/komoot/photon/elasticsearch/ImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

Expand All @@ -19,15 +22,57 @@ public void tearDown() {
}

@Test
public void addSimpleDoc() throws IOException {
public void testAddSimpleDoc() throws IOException {
setUpES();
Importer instance = new Importer(getClient(), "en");
instance.add(new PhotonDoc(1234, "N", 1000, "place", "city"));
Importer instance = new Importer(getClient(), "en", "");
instance.add(new PhotonDoc(1234, "N", 1000, "place", "city")
.extraTags(Collections.singletonMap("maxspeed", "100")));
instance.finish();
refresh();

GetResponse response = getById(1234);

assertTrue(response.isExists());

Map<String, Object> source = response.getSource();

assertEquals("N", source.get("osm_type"));
assertEquals(1000, source.get("osm_id"));
assertEquals("place", source.get("osm_key"));
assertEquals("city", source.get("osm_value"));

assertNull(source.get("extra"));
}

@Test
public void testSelectedExtraTagsCanBeIncluded() throws IOException {
setUpES();
Importer instance = new Importer(getClient(), "en", "maxspeed,website");

Map<String, String> extratags = new HashMap<>();
extratags.put("website", "foo");
extratags.put("maxspeed", "100 mph");
extratags.put("source", "survey");

instance.add(new PhotonDoc(1234, "N", 1000, "place", "city").extraTags(extratags));
instance.add(new PhotonDoc(1235, "N", 1001, "place", "city")
.extraTags(Collections.singletonMap("wikidata", "100")));
instance.finish();
refresh();

GetResponse response = getById(1234);
assertTrue(response.isExists());

Map<String, String> extra = (Map<String, String>) response.getSource().get("extra");
assertNotNull(extra);

assertEquals(2, extra.size());
assertEquals("100 mph", extra.get("maxspeed"));
assertEquals("foo", extra.get("website"));

response = getById(1235);
assertTrue(response.isExists());

assertNull(response.getSource().get("extra"));
}
}
41 changes: 37 additions & 4 deletions src/test/java/de/komoot/photon/elasticsearch/UpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -28,13 +29,13 @@ public void addNameToDoc() throws IOException {
PhotonDoc doc = new PhotonDoc(1234, "N", 1000, "place", "city").names(names);

setUpES();
Importer instance = new Importer(getClient(), "en");
Importer instance = new Importer(getClient(), "en", "");
instance.add(doc);
instance.finish();
refresh();

names.put("name:en", "Enfoo");
Updater updater = new Updater(getClient(), "en,de");
Updater updater = new Updater(getClient(), "en,de", "");
updater.create(doc);
updater.finish();
refresh();
Expand All @@ -55,13 +56,13 @@ public void removeNameFromDoc() throws IOException {
PhotonDoc doc = new PhotonDoc(1234, "N", 1000, "place", "city").names(names);

setUpES();
Importer instance = new Importer(getClient(), "en");
Importer instance = new Importer(getClient(), "en", "");
instance.add(doc);
instance.finish();
refresh();

names.remove("name");
Updater updater = new Updater(getClient(), "en,de");
Updater updater = new Updater(getClient(), "en,de", "");
updater.create(doc);
updater.finish();
refresh();
Expand All @@ -73,4 +74,36 @@ public void removeNameFromDoc() throws IOException {
assertFalse(out_names.containsKey("default"));
assertEquals("Enfoo", out_names.get("en"));
}

@Test
public void addExtraTagsToDoc() throws IOException {
Map<String, String> names = new HashMap<>();
names.put("name", "Foo");
PhotonDoc doc = new PhotonDoc(1234, "N", 1000, "place", "city").names(names);

setUpES();
Importer instance = new Importer(getClient(), "en", "website");
instance.add(doc);
instance.finish();
refresh();

GetResponse response = getById(1234);
assertTrue(response.isExists());

assertNull(response.getSource().get("extra"));

doc.extraTags(Collections.singletonMap("website", "http://site.foo"));
Updater updater = new Updater(getClient(), "en,de", "website");
updater.create(doc);
updater.finish();
refresh();

response = getById(1234);
assertTrue(response.isExists());

Map<String, String> extra = (Map<String, String>) response.getSource().get("extra");

assertNotNull(extra);
assertEquals(Collections.singletonMap("website", "http://site.foo"), extra);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
* Created by Sachin Dole on 2/20/2015.
Expand All @@ -34,7 +30,7 @@ public void setUp() throws Exception {
setUpES();
ImmutableList<String> tags = ImmutableList.of("tourism", "attraction", "tourism", "hotel", "tourism", "museum", "tourism", "information", "amenity",
"parking", "amenity", "restaurant", "amenity", "information", "food", "information", "railway", "station");
Importer instance = new Importer(getClient(), "en");
Importer instance = new Importer(getClient(), "en", "");
double lon = 13.38886;
double lat = 52.51704;
for (int i = 0; i < tags.size(); i++) {
Expand Down
Loading