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

Make ES query timeout configurable #769

Merged
merged 1 commit into from
Jan 31, 2024
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
13 changes: 9 additions & 4 deletions src/main/java/de/komoot/photon/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import de.komoot.photon.elasticsearch.Server;
import de.komoot.photon.nominatim.NominatimConnector;
import de.komoot.photon.nominatim.NominatimUpdater;
import de.komoot.photon.searcher.ReverseHandler;
import de.komoot.photon.searcher.SearchHandler;
import de.komoot.photon.utils.CorsFilter;
import lombok.extern.slf4j.Slf4j;
import spark.Request;
Expand Down Expand Up @@ -162,10 +164,13 @@ private static void startApi(CommandLineArgs args, Server server) {

// setup search API
String[] langs = dbProperties.getLanguages();
get("api", new SearchRequestHandler("api", server.createSearchHandler(langs), langs, args.getDefaultLanguage()));
get("api/", new SearchRequestHandler("api/", server.createSearchHandler(langs), langs, args.getDefaultLanguage()));
get("reverse", new ReverseSearchRequestHandler("reverse", server.createReverseHandler(), dbProperties.getLanguages(), args.getDefaultLanguage()));
get("reverse/", new ReverseSearchRequestHandler("reverse/", server.createReverseHandler(), dbProperties.getLanguages(), args.getDefaultLanguage()));
SearchHandler searchHandler = server.createSearchHandler(langs, args.getQueryTimeout());
get("api", new SearchRequestHandler("api", searchHandler, langs, args.getDefaultLanguage()));
get("api/", new SearchRequestHandler("api/", searchHandler, langs, args.getDefaultLanguage()));

ReverseHandler reverseHandler = server.createReverseHandler(args.getQueryTimeout());
get("reverse", new ReverseSearchRequestHandler("reverse", reverseHandler, dbProperties.getLanguages(), args.getDefaultLanguage()));
get("reverse/", new ReverseSearchRequestHandler("reverse/", reverseHandler, dbProperties.getLanguages(), args.getDefaultLanguage()));

if (args.isEnableUpdateApi()) {
// setup update API
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 @@ -41,6 +41,9 @@ public class CommandLineArgs {
@Parameter(names = "-synonym-file", description = "file with synonym and classification terms")
private String synonymFile = null;

@Parameter(names = "-query-timeout", description = "Time after which to cancel queries to the ES database (in seconds).")
private int queryTimeout = 7;

@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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/
public class ElasticsearchReverseHandler implements ReverseHandler {
private Client client;
private TimeValue queryTimeout;

public ElasticsearchReverseHandler(Client client) {
public ElasticsearchReverseHandler(Client client, int queryTimeoutSec) {
this.client = client;
queryTimeout = TimeValue.timeValueSeconds(queryTimeoutSec);
}

@Override
Expand All @@ -49,10 +51,8 @@ public String dumpQuery(ReverseRequest photonRequest) {

private SearchResponse search(QueryBuilder queryBuilder, Integer limit, Point location,
Boolean locationDistanceSort) {
TimeValue timeout = TimeValue.timeValueSeconds(7);

SearchRequestBuilder builder = client.prepareSearch(PhotonIndex.NAME).setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(queryBuilder).setSize(limit).setTimeout(timeout);
.setQuery(queryBuilder).setSize(limit).setTimeout(queryTimeout);

if (locationDistanceSort)
builder.addSort(SortBuilders.geoDistanceSort("coordinate", new GeoPoint(location.getY(), location.getX()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public class ElasticsearchSearchHandler implements SearchHandler {
private final Client client;
private final String[] supportedLanguages;
private boolean lastLenient = false;
private TimeValue queryTimeout;

public ElasticsearchSearchHandler(Client client, String[] languages) {
public ElasticsearchSearchHandler(Client client, String[] languages, int queryTimeoutSec) {
this.client = client;
this.supportedLanguages = languages;
queryTimeout = TimeValue.timeValueSeconds(queryTimeoutSec);
}

@Override
Expand Down Expand Up @@ -63,12 +65,11 @@ public PhotonQueryBuilder buildQuery(PhotonRequest photonRequest, boolean lenien
}

private SearchResponse sendQuery(QueryBuilder queryBuilder, Integer limit) {
TimeValue timeout = TimeValue.timeValueSeconds(7);
return client.prepareSearch(PhotonIndex.NAME).
setSearchType(SearchType.QUERY_THEN_FETCH).
setQuery(queryBuilder).
setSize(limit).
setTimeout(timeout).
setTimeout(queryTimeout).
execute().
actionGet();

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/komoot/photon/elasticsearch/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ public Updater createUpdater(String[] languages, String[] extraTags) {
return new de.komoot.photon.elasticsearch.Updater(esClient, languages, extraTags);
}

public SearchHandler createSearchHandler(String[] languages) {
return new ElasticsearchSearchHandler(esClient, languages);
public SearchHandler createSearchHandler(String[] languages, int queryTimeoutSec) {
return new ElasticsearchSearchHandler(esClient, languages, queryTimeoutSec);
}

public ReverseHandler createReverseHandler() {
return new ElasticsearchReverseHandler(esClient);
public ReverseHandler createReverseHandler(int queryTimeoutSec) {
return new ElasticsearchReverseHandler(esClient, queryTimeoutSec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void tearDown() {
}

private PhotonResult search(String query) {
SearchHandler handler = getServer().createSearchHandler(new String[]{"en", "de", "it"});
SearchHandler handler = getServer().createSearchHandler(new String[]{"en", "de", "it"}, 1);

return handler.search(new PhotonRequest(query, "default")).get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private PhotonDoc createDoc(String... names) {
}

private List<PhotonResult> search(String query) {
return getServer().createSearchHandler(new String[]{"en"}).search(new PhotonRequest(query, "en"));
return getServer().createSearchHandler(new String[]{"en"}, 1).search(new PhotonRequest(query, "en"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private PhotonDoc createDoc(String key, String value, String name) {
}

private List<PhotonResult> search(String query) {
return getServer().createSearchHandler(new String[]{"en"}).search(new PhotonRequest(query, "en"));
return getServer().createSearchHandler(new String[]{"en"}, 1).search(new PhotonRequest(query, "en"));
}

private void updateClassification(String key, String value, String... terms) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private PhotonDoc createDoc(String... names) {
}

private List<PhotonResult> search(String query, String lang) {
return getServer().createSearchHandler(languageList).search(new PhotonRequest(query, lang));
return getServer().createSearchHandler(languageList, 1).search(new PhotonRequest(query, lang));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private List<PhotonResult> searchWithLayers(String... layers) {
PhotonRequest request = new PhotonRequest("berlin", "en").setLimit(50);
request.setLayerFilter(Arrays.stream(layers).collect(Collectors.toSet()));

return getServer().createSearchHandler(new String[]{"en"}).search(request);
return getServer().createSearchHandler(new String[]{"en"}, 1).search(request);
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private List<PhotonResult> searchWithTags(String[] params) {
request.addOsmTagFilter(TagFilter.buildOsmTagFilter(param));
}

return getServer().createSearchHandler(new String[]{"en"}).search(request);
return getServer().createSearchHandler(new String[]{"en"}, 1).search(request);
}


Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/komoot/photon/query/QueryRelevanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ private PhotonDoc createDoc(String key, String value, long id, String... names)
}

private List<PhotonResult> search(String query) {
return getServer().createSearchHandler(new String[]{"en"}).search(new PhotonRequest(query, "en"));
return getServer().createSearchHandler(new String[]{"en"}, 1).search(new PhotonRequest(query, "en"));
}

private List<PhotonResult> search(PhotonRequest request) {
return getServer().createSearchHandler(new String[]{"en"}).search(request);
return getServer().createSearchHandler(new String[]{"en"}, 1).search(request);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private List<PhotonResult> reverse(String... layers) {
Set<String> layerSet = Arrays.stream(layers).collect(Collectors.toSet());
ReverseRequest request = new ReverseRequest(pt, "en", 1.0, "", 10, true, layerSet, false);

return getServer().createReverseHandler().reverse(request);
return getServer().createReverseHandler(1).reverse(request);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private List<PhotonResult> reverseWithTags(String[] params) {
for (String param : params) {
request.addOsmTagFilter(TagFilter.buildOsmTagFilter(param));
}
return getServer().createReverseHandler().reverse(request);
return getServer().createReverseHandler(1).reverse(request);
}

@ParameterizedTest
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/komoot/photon/query/QueryReverseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void tearDown() {
private List<PhotonResult> reverse(double lon, double lat, double radius, int limit) {
Point pt = FACTORY.createPoint(new Coordinate(lon, lat));

return getServer().createReverseHandler().reverse(
return getServer().createReverseHandler(1).reverse(
new ReverseRequest(pt, "en", radius, "", limit, true, new HashSet<>(), false)
);
}
Expand Down
Loading