Skip to content

Commit 8f8a86d

Browse files
authored
Merge pull request #882 from lonvia/fix-language-dependet-context
This fixes a minor but long-standing bug where all extra context names ended up in the "default" language. Factors out the effected code into its own function to save a bit of code duplication. There was also a small issue with names that are removed from the address list not ending up in the context because only "name" tags are taken into account.
2 parents 21c0433 + 014ca23 commit 8f8a86d

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

app/es_embedded/src/main/java/de/komoot/photon/elasticsearch/PhotonDocConverter.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static XContentBuilder convert(PhotonDoc doc, String[] languages, String[
7676
String countryCode = doc.getCountryCode();
7777
if (countryCode != null)
7878
builder.field(Constants.COUNTRYCODE, countryCode);
79-
writeContext(builder, doc.getContext(), languages);
79+
writeContext(builder, doc.getContextByLanguage(languages));
8080
writeExtraTags(builder, doc.getExtratags(), extraTags);
8181
writeExtent(builder, doc.getBbox());
8282

@@ -151,24 +151,10 @@ private static void write(XContentBuilder builder, Map<String, String> fNames, S
151151
builder.endObject();
152152
}
153153

154-
protected static void writeContext(XContentBuilder builder, Set<Map<String, String>> contexts, String[] languages) throws IOException {
155-
final Map<String, Set<String>> multimap = new HashMap<>();
156-
157-
for (Map<String, String> context : contexts) {
158-
if (context.get("name") != null) {
159-
multimap.computeIfAbsent("default", k -> new HashSet<>()).add(context.get("name"));
160-
}
161-
162-
for (String language : languages) {
163-
if (context.get("name:" + language) != null) {
164-
multimap.computeIfAbsent("default", k -> new HashSet<>()).add(context.get("name:" + language));
165-
}
166-
}
167-
}
168-
169-
if (!multimap.isEmpty()) {
154+
protected static void writeContext(XContentBuilder builder, Map<String, Set<String>> contexts) throws IOException {
155+
if (!contexts.isEmpty()) {
170156
builder.startObject("context");
171-
for (Map.Entry<String, Set<String>> entry : multimap.entrySet()) {
157+
for (Map.Entry<String, Set<String>> entry : contexts.entrySet()) {
172158
builder.field(entry.getKey(), String.join(", ", entry.getValue()));
173159
}
174160
builder.endObject();

app/opensearch/src/main/java/de/komoot/photon/opensearch/PhotonDocSerializer.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void serialize(PhotonDoc value, JsonGenerator gen, SerializerProvider pro
8989
gen.writeStringField(Constants.COUNTRYCODE, countryCode);
9090
}
9191

92-
writeContext(gen, value.getContext());
92+
writeContext(gen, value.getContextByLanguage(languages));
9393
writeExtraTags(gen, value.getExtratags());
9494
writeExtent(gen, value.getBbox());
9595

@@ -115,24 +115,10 @@ private void writeName(JsonGenerator gen, PhotonDoc doc, String[] languages) thr
115115
gen.writeObjectField("name", fNames);
116116
}
117117

118-
private void writeContext(JsonGenerator gen, Set<Map<String, String>> contexts) throws IOException {
119-
final Map<String, Set<String>> multimap = new HashMap<>();
120-
121-
for (Map<String, String> context : contexts) {
122-
if (context.get("name") != null) {
123-
multimap.computeIfAbsent("default", k -> new HashSet<>()).add(context.get("name"));
124-
}
125-
126-
for (String language : languages) {
127-
if (context.get("name:" + language) != null) {
128-
multimap.computeIfAbsent("default", k -> new HashSet<>()).add(context.get("name:" + language));
129-
}
130-
}
131-
}
132-
133-
if (!multimap.isEmpty()) {
118+
private void writeContext(JsonGenerator gen, Map<String, Set<String>> contexts) throws IOException {
119+
if (!contexts.isEmpty()) {
134120
gen.writeObjectFieldStart("context");
135-
for (Map.Entry<String, Set<String>> entry : multimap.entrySet()) {
121+
for (Map.Entry<String, Set<String>> entry : contexts.entrySet()) {
136122
gen.writeStringField(entry.getKey(), String.join(", ", entry.getValue()));
137123
}
138124
gen.writeEndObject();

src/main/java/de/komoot/photon/PhotonDoc.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private void extractAddress(Map<String, String> address, AddressType addressType
235235
LOGGER.debug("Replacing {} name '{}' with '{}' for osmId #{}", addressFieldName, existingName, field, osmId);
236236
// we keep the former name in the context as it might be helpful when looking up typos
237237
if (!Objects.isNull(existingName)) {
238-
context.add(Collections.singletonMap("formerName", existingName));
238+
context.add(Collections.singletonMap("name", existingName));
239239
}
240240
map.put("name", field);
241241
addressParts.put(addressType, map);
@@ -273,6 +273,26 @@ public void completePlace(List<AddressRow> addresses) {
273273
}
274274
}
275275

276+
public Map<String, Set<String>> getContextByLanguage(String[] languages) {
277+
final Map<String, Set<String>> multimap = new HashMap<>();
278+
279+
for (Map<String, String> cmap : context) {
280+
String locName = cmap.get("name");
281+
if (locName != null) {
282+
multimap.computeIfAbsent("default", k -> new HashSet<>()).add(locName);
283+
}
284+
285+
for (String language : languages) {
286+
locName = cmap.get("name:" + language);
287+
if (locName != null) {
288+
multimap.computeIfAbsent(language, k -> new HashSet<>()).add(locName);
289+
}
290+
}
291+
}
292+
293+
return multimap;
294+
}
295+
276296
public void setCountry(Map<String, String> names) {
277297
addressParts.put(AddressType.COUNTRY, names);
278298
}

0 commit comments

Comments
 (0)