Skip to content

Commit

Permalink
Adds support of search_languages so that we can limit the list of lan…
Browse files Browse the repository at this point in the history
…guages for ES search via request param
  • Loading branch information
Леонид Лапкин committed Nov 24, 2021
1 parent 60a9754 commit 493d01a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/main/java/de/komoot/photon/query/PhotonRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class PhotonRequest implements Serializable {
private Integer limit;
private Point locationForBias;
private String language;
private String[] searchLanguages;

private final double scale;
private final int zoom;
private Envelope bbox;
Expand All @@ -30,14 +32,14 @@ public class PhotonRequest implements Serializable {
private Map<String, Set<String>> excludeTags;
private Map<String, Set<String>> excludeTagValues;


public PhotonRequest(String query, int limit, Envelope bbox, Point locationForBias, double scale, int zoom, String language, boolean debug) {
public PhotonRequest(String query, int limit, Envelope bbox, Point locationForBias, double scale, int zoom, String language, String[] searchLanguages, boolean debug) {
this.query = query;
this.limit = limit;
this.locationForBias = locationForBias;
this.scale = scale;
this.zoom = zoom;
this.language = language;
this.searchLanguages = searchLanguages;
this.bbox = bbox;
this.debug = debug;
}
Expand Down Expand Up @@ -70,6 +72,10 @@ public String getLanguage() {
return language;
}

public String[] getSearchLanguages() {
return searchLanguages;
}

public boolean getDebug() { return debug; }

public Set<String> keys() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PhotonRequestFactory {
private final BoundingBoxParamConverter bboxParamConverter;

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

public PhotonRequestFactory(List<String> supportedLanguages, String defaultLanguage) {
this.languageResolver = new RequestLanguageResolver(supportedLanguages, defaultLanguage);
Expand All @@ -43,6 +43,9 @@ public PhotonRequest create(Request webRequest) throws BadRequestException {
} catch (NumberFormatException e) {
limit = 15;
}

String searchLanguagesStr = webRequest.queryParams("search_languages");
String[] searchLanguages = (searchLanguagesStr != null && !searchLanguagesStr.isEmpty()) ? searchLanguagesStr.split(",") : null;

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

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

PhotonRequest request = new PhotonRequest(query, limit, bbox, locationForBias, scale, zoom, language, debug);
PhotonRequest request = new PhotonRequest(query, limit, bbox, locationForBias, scale, zoom, language, searchLanguages, debug);

QueryParamsMap tagFiltersQueryMap = webRequest.queryMap("osm_tag");
if (new CheckIfFilteredRequest().execute(tagFiltersQueryMap)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.json.JSONObject;

import java.util.List;
import java.util.Arrays;

/**
* 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
Expand Down Expand Up @@ -52,6 +53,7 @@ public String dumpQuery(PhotonRequest photonRequest) {
public PhotonQueryBuilder buildQuery(PhotonRequest photonRequest, boolean lenient) {
return PhotonQueryBuilder.
builder(photonRequest.getQuery(), photonRequest.getLanguage(), supportedLanguages, lenient).
builder(photonRequest.getQuery(), photonRequest.getLanguage(), photonRequest.getSearchLanguages() != null ? Arrays.asList(photonRequest.getSearchLanguages()) : supportedLanguages, lenient).
withTags(photonRequest.tags()).
withKeys(photonRequest.keys()).
withValues(photonRequest.values()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PhotonRequestTest {

private PhotonRequest simpleRequest() {
return new PhotonRequest("foo", 1, null, null, 1.6, 16, "de", false);
return new PhotonRequest("foo", 1, null, null, 1.6, 16, "de", null, false);
}

@Test
Expand Down

0 comments on commit 493d01a

Please sign in to comment.