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

Adds support of search_languages so that we can limit the list of lan… #615

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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
29 changes: 29 additions & 0 deletions src/test/java/de/komoot/photon/query/PhotonRequestFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ public void testWithExcludeValueFilter() throws Exception {
Assert.assertEquals(ImmutableSet.of("aValue"), filteredPhotonRequest.notValues());
}

@Test
public void testWithSearchLanguages() throws Exception {
Request mockRequest = Mockito.mock(Request.class);
Mockito.when(mockRequest.queryParams("q")).thenReturn("berlin");
Mockito.when(mockRequest.queryParams("search_languages")).thenReturn("en,es,pt");
PhotonRequestFactory photonRequestFactory = new PhotonRequestFactory(ImmutableList.of("en"), "en");
QueryParamsMap mockQueryParamsMap = Mockito.mock(QueryParamsMap.class);
Mockito.when(mockRequest.queryMap("osm_tag")).thenReturn(mockQueryParamsMap);
photonRequest = photonRequestFactory.create(mockRequest);
Assert.assertEquals("berlin", photonRequest.getQuery());
Assert.assertArrayEquals(new String[]{"en", "es", "pt"}, photonRequest.getSearchLanguages());
Mockito.verify(mockRequest, Mockito.times(1)).queryParams("q");
Mockito.verify(mockRequest, Mockito.times(1)).queryParams("search_languages");
}

@Test
public void testWithoutSearchLanguages() throws Exception {
Request mockRequest = Mockito.mock(Request.class);
Mockito.when(mockRequest.queryParams("q")).thenReturn("berlin");
Mockito.when(mockRequest.queryParams("search_languages")).thenReturn(null);
PhotonRequestFactory photonRequestFactory = new PhotonRequestFactory(ImmutableList.of("en"), "en");
QueryParamsMap mockQueryParamsMap = Mockito.mock(QueryParamsMap.class);
Mockito.when(mockRequest.queryMap("osm_tag")).thenReturn(mockQueryParamsMap);
photonRequest = photonRequestFactory.create(mockRequest);
Assert.assertEquals("berlin", photonRequest.getQuery());
Assert.assertNull(photonRequest.getSearchLanguages());
Mockito.verify(mockRequest, Mockito.times(1)).queryParams("q");
}

@Test
public void testWithBboxFilter() throws Exception {
Request mockRequest = Mockito.mock(Request.class);
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