Skip to content

Commit 493d01a

Browse files
author
Леонид Лапкин
committed
Adds support of search_languages so that we can limit the list of languages for ES search via request param
1 parent 60a9754 commit 493d01a

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/main/java/de/komoot/photon/query/PhotonRequest.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class PhotonRequest implements Serializable {
1717
private Integer limit;
1818
private Point locationForBias;
1919
private String language;
20+
private String[] searchLanguages;
21+
2022
private final double scale;
2123
private final int zoom;
2224
private Envelope bbox;
@@ -30,14 +32,14 @@ public class PhotonRequest implements Serializable {
3032
private Map<String, Set<String>> excludeTags;
3133
private Map<String, Set<String>> excludeTagValues;
3234

33-
34-
public PhotonRequest(String query, int limit, Envelope bbox, Point locationForBias, double scale, int zoom, String language, boolean debug) {
35+
public PhotonRequest(String query, int limit, Envelope bbox, Point locationForBias, double scale, int zoom, String language, String[] searchLanguages, boolean debug) {
3536
this.query = query;
3637
this.limit = limit;
3738
this.locationForBias = locationForBias;
3839
this.scale = scale;
3940
this.zoom = zoom;
4041
this.language = language;
42+
this.searchLanguages = searchLanguages;
4143
this.bbox = bbox;
4244
this.debug = debug;
4345
}
@@ -70,6 +72,10 @@ public String getLanguage() {
7072
return language;
7173
}
7274

75+
public String[] getSearchLanguages() {
76+
return searchLanguages;
77+
}
78+
7379
public boolean getDebug() { return debug; }
7480

7581
public Set<String> keys() {

src/main/java/de/komoot/photon/query/PhotonRequestFactory.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PhotonRequestFactory {
1919
private final BoundingBoxParamConverter bboxParamConverter;
2020

2121
private static final HashSet<String> REQUEST_QUERY_PARAMS = new HashSet<>(Arrays.asList("lang", "q", "lon", "lat",
22-
"limit", "osm_tag", "location_bias_scale", "bbox", "debug", "zoom"));
22+
"limit", "osm_tag", "location_bias_scale", "bbox", "debug", "zoom", "search_languages"));
2323

2424
public PhotonRequestFactory(List<String> supportedLanguages, String defaultLanguage) {
2525
this.languageResolver = new RequestLanguageResolver(supportedLanguages, defaultLanguage);
@@ -43,6 +43,9 @@ public PhotonRequest create(Request webRequest) throws BadRequestException {
4343
} catch (NumberFormatException e) {
4444
limit = 15;
4545
}
46+
47+
String searchLanguagesStr = webRequest.queryParams("search_languages");
48+
String[] searchLanguages = (searchLanguagesStr != null && !searchLanguagesStr.isEmpty()) ? searchLanguagesStr.split(",") : null;
4649

4750
Point locationForBias = optionalLocationParamConverter.apply(webRequest);
4851
Envelope bbox = bboxParamConverter.apply(webRequest);
@@ -68,7 +71,7 @@ public PhotonRequest create(Request webRequest) throws BadRequestException {
6871

6972
boolean debug = webRequest.queryParams("debug") != null;
7073

71-
PhotonRequest request = new PhotonRequest(query, limit, bbox, locationForBias, scale, zoom, language, debug);
74+
PhotonRequest request = new PhotonRequest(query, limit, bbox, locationForBias, scale, zoom, language, searchLanguages, debug);
7275

7376
QueryParamsMap tagFiltersQueryMap = webRequest.queryMap("osm_tag");
7477
if (new CheckIfFilteredRequest().execute(tagFiltersQueryMap)) {

src/main/java/de/komoot/photon/searcher/PhotonRequestHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.json.JSONObject;
88

99
import java.util.List;
10+
import java.util.Arrays;
1011

1112
/**
1213
* Given a {@link PhotonRequest photon request}, execute the search, process it (for example, de-duplicate) and respond with results formatted in a list of {@link JSONObject json
@@ -52,6 +53,7 @@ public String dumpQuery(PhotonRequest photonRequest) {
5253
public PhotonQueryBuilder buildQuery(PhotonRequest photonRequest, boolean lenient) {
5354
return PhotonQueryBuilder.
5455
builder(photonRequest.getQuery(), photonRequest.getLanguage(), supportedLanguages, lenient).
56+
builder(photonRequest.getQuery(), photonRequest.getLanguage(), photonRequest.getSearchLanguages() != null ? Arrays.asList(photonRequest.getSearchLanguages()) : supportedLanguages, lenient).
5557
withTags(photonRequest.tags()).
5658
withKeys(photonRequest.keys()).
5759
withValues(photonRequest.values()).

src/test/java/de/komoot/photon/query/PhotonRequestTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class PhotonRequestTest {
1212

1313
private PhotonRequest simpleRequest() {
14-
return new PhotonRequest("foo", 1, null, null, 1.6, 16, "de", false);
14+
return new PhotonRequest("foo", 1, null, null, 1.6, 16, "de", null, false);
1515
}
1616

1717
@Test

0 commit comments

Comments
 (0)