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

Remove request handler factories #511

Merged
merged 2 commits into from
Nov 13, 2020
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
12 changes: 5 additions & 7 deletions src/main/java/de/komoot/photon/ReverseSearchRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.komoot.photon.query.ReverseRequestFactory;
import de.komoot.photon.searcher.ReverseElasticsearchSearcher;
import de.komoot.photon.searcher.ReverseRequestHandler;
import de.komoot.photon.searcher.ReverseRequestHandlerFactory;
import de.komoot.photon.utils.ConvertToGeoJson;
import org.elasticsearch.client.Client;
import org.json.JSONObject;
Expand All @@ -21,31 +20,30 @@
/**
* @author svantulden
*/
public class ReverseSearchRequestHandler<R extends ReverseRequest> extends RouteImpl {
public class ReverseSearchRequestHandler extends RouteImpl {
private final ReverseRequestFactory reverseRequestFactory;
private final ReverseRequestHandlerFactory requestHandlerFactory;
private final ReverseRequestHandler requestHandler;
private final ConvertToGeoJson geoJsonConverter;

ReverseSearchRequestHandler(String path, Client esNodeClient, String languages, String defaultLanguage) {
super(path);
List<String> supportedLanguages = Arrays.asList(languages.split(","));
this.reverseRequestFactory = new ReverseRequestFactory(supportedLanguages, defaultLanguage);
this.geoJsonConverter = new ConvertToGeoJson();
this.requestHandlerFactory = new ReverseRequestHandlerFactory(new ReverseElasticsearchSearcher(esNodeClient));
this.requestHandler = new ReverseRequestHandler(new ReverseElasticsearchSearcher(esNodeClient));
}

@Override
public String handle(Request request, Response response) {
R photonRequest = null;
ReverseRequest photonRequest = null;
try {
photonRequest = reverseRequestFactory.create(request);
} catch (BadRequestException e) {
JSONObject json = new JSONObject();
json.put("message", e.getMessage());
halt(e.getHttpStatus(), json.toString());
}
ReverseRequestHandler<R> handler = requestHandlerFactory.createHandler(photonRequest);
List<JSONObject> results = handler.handle(photonRequest);
List<JSONObject> results = requestHandler.handle(photonRequest);
JSONObject geoJsonResults = geoJsonConverter.convert(results);
if (request.queryParams("debug") != null)
return geoJsonResults.toString(4);
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/de/komoot/photon/SearchRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.komoot.photon.query.PhotonRequestFactory;
import de.komoot.photon.searcher.BaseElasticsearchSearcher;
import de.komoot.photon.searcher.PhotonRequestHandler;
import de.komoot.photon.searcher.PhotonRequestHandlerFactory;
import de.komoot.photon.utils.ConvertToGeoJson;
import org.elasticsearch.client.Client;
import org.json.JSONObject;
Expand All @@ -21,37 +20,36 @@
/**
* Created by Sachin Dole on 2/12/2015.
*/
public class SearchRequestHandler<R extends PhotonRequest> extends RouteImpl {
public class SearchRequestHandler extends RouteImpl {
private static final String DEBUG_PARAMETER = "debug";

private final PhotonRequestFactory photonRequestFactory;
private final PhotonRequestHandlerFactory requestHandlerFactory;
private final PhotonRequestHandler requestHandler;
private final ConvertToGeoJson geoJsonConverter;

SearchRequestHandler(String path, Client esNodeClient, String languages, String defaultLanguage) {
super(path);
List<String> supportedLanguages = Arrays.asList(languages.split(","));
this.photonRequestFactory = new PhotonRequestFactory(supportedLanguages, defaultLanguage);
this.geoJsonConverter = new ConvertToGeoJson();
this.requestHandlerFactory = new PhotonRequestHandlerFactory(new BaseElasticsearchSearcher(esNodeClient));
this.requestHandler = new PhotonRequestHandler(new BaseElasticsearchSearcher(esNodeClient));
}

@Override
public String handle(Request request, Response response) {
R photonRequest = null;
PhotonRequest photonRequest = null;
try {
photonRequest = photonRequestFactory.create(request);
} catch (BadRequestException e) {
JSONObject json = new JSONObject();
json.put("message", e.getMessage());
halt(e.getHttpStatus(), json.toString());
}
PhotonRequestHandler<R> handler = requestHandlerFactory.createHandler(photonRequest);
List<JSONObject> results = handler.handle(photonRequest);
List<JSONObject> results = requestHandler.handle(photonRequest);
JSONObject geoJsonResults = geoJsonConverter.convert(results);
if (request.queryParams(DEBUG_PARAMETER) != null) {
JSONObject debug = new JSONObject();
debug.put("query", new JSONObject(handler.dumpQuery(photonRequest)));
debug.put("query", new JSONObject(requestHandler.dumpQuery(photonRequest)));
geoJsonResults.put(DEBUG_PARAMETER, debug);
return geoJsonResults.toString(4);
}
Expand Down
84 changes: 0 additions & 84 deletions src/main/java/de/komoot/photon/query/FilteredPhotonRequest.java

This file was deleted.

148 changes: 147 additions & 1 deletion src/main/java/de/komoot/photon/query/PhotonRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.vividsolutions.jts.geom.Point;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Created by Sachin Dole on 2/12/2015.
* Collection of query parameters for a search request.
*/
public class PhotonRequest implements Serializable {
private String query;
Expand All @@ -16,6 +20,15 @@ public class PhotonRequest implements Serializable {
private final double scale;
private Envelope bbox;

private Set<String> excludeKeys;
private Set<String> includeKeys;
private Set<String> excludeValues;
private Set<String> includeValues;
private Map<String, Set<String>> includeTags;
private Map<String, Set<String>> excludeTags;
private Map<String, Set<String>> excludeTagValues;


public PhotonRequest(String query, int limit, Envelope bbox, Point locationForBias, double scale, String language) {
this.query = query;
this.limit = limit;
Expand Down Expand Up @@ -48,4 +61,137 @@ public double getScaleForBias() {
public String getLanguage() {
return language;
}

public Set<String> keys() {
return includeKeys;
}

public Set<String> values() {
return includeValues;
}

public Map<String, Set<String>> tags() {
return includeTags;
}

public Set<String> notValues() {
return excludeValues;
}

public Map<String, Set<String>> notTags() {
return excludeTags;
}

public Set<String> notKeys() {
return excludeKeys;
}

public Map<String, Set<String>> tagNotValues() {
return excludeTagValues;

}

private void addExcludeValues(String value) {
if (excludeValues == null) {
excludeValues = new HashSet<>(3);
}
excludeValues.add(value);
}

private void addIncludeValues(String value) {
if (includeValues == null) {
includeValues = new HashSet<>(3);
}
includeValues.add(value);
}

private void addExcludeKeys(String value) {
if (excludeKeys == null) {
excludeKeys = new HashSet<>(3);
}
excludeKeys.add(value);
}

private void addIncludeKeys(String value) {
if (includeKeys == null) {
includeKeys = new HashSet<>(3);
}
includeKeys.add(value);
}

private void addExcludeTags(String key, String value) {
if (excludeTags == null) {
excludeTags = new HashMap<>(3);
}

excludeTags.computeIfAbsent(key, k -> new HashSet<>()).add(value);
}

private void addExcludeTagValues(String key, String value) {
if (excludeTagValues == null) {
excludeTagValues = new HashMap<>(3);
}

excludeTagValues.computeIfAbsent(key, k -> new HashSet<>()).add(value);
}

private void addIncludeTags(String key, String value) {
if (includeTags == null) {
includeTags = new HashMap<>(3);
}

includeTags.computeIfAbsent(key, k -> new HashSet<>()).add(value);
}

public void setUpTagFilters(String[] tagFilters) {
for (String tagFilter : tagFilters) {
if (tagFilter.contains(":")) {
//might be tag and value OR just value.
if (tagFilter.startsWith("!")) {
//exclude
String keyValueCandidate = tagFilter.substring(1);
if (keyValueCandidate.startsWith(":")) {
//just value
addExcludeValues(keyValueCandidate.substring(1));
} else {
//key and value
String[] keyAndValue = keyValueCandidate.split(":");
String excludeKey = keyAndValue[0];
String value = keyAndValue[1].startsWith("!") ? keyAndValue[1].substring(1) : keyAndValue[1];
addExcludeTags(excludeKey, value);
}
} else {
//include key, not sure about value
if (tagFilter.startsWith(":")) {
//just value

String valueCandidate = tagFilter.substring(1);
if (valueCandidate.startsWith("!")) {
addExcludeValues(valueCandidate.substring(1));
} else {
addIncludeValues(valueCandidate);
}
} else {
//key and value
String[] keyAndValue = tagFilter.split(":");

String key = keyAndValue[0];
String value = keyAndValue[1];
if (value.startsWith("!")) {
addExcludeTagValues(key, value.substring(1));
} else {
addIncludeTags(key, value);
}
}
}
} else {
//only tag
if (tagFilter.startsWith("!")) {
addExcludeKeys(tagFilter.substring(1));
} else {
addIncludeKeys(tagFilter);
}
}
}
}
}
Loading