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

AER-2539 Attempt at improving suggestions from Bing map search #43

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -47,8 +51,22 @@ public class BingSearchService implements SearchTaskService {
private static final String REGION = "GB";
private static final String CULTURE = "en-GB";
private static final String BNG_BOUNDS = "49.79,-8.82,60.94,1.92";
private static final String PDOK_SUGGEST_ENDPOINT = "https://dev.virtualearth.net/REST/v1/Locations?query=%s"
+ "&key=%s&include=ciso2&userRegion=" + REGION + "&c=" + CULTURE + "&userMapView=" + BNG_BOUNDS;
private static final String BING_SUGGEST_ENDPOINT = "https://dev.virtualearth.net/REST/v1/Autosuggest?query=%s"
+ "&key=%s"
+ "&userRegion=" + REGION
+ "&countryFilter=" + REGION
+ "&includeEntityTypes=Address,Place"
+ "&c=" + CULTURE
+ "&userMapView=" + BNG_BOUNDS
+ "&maxResults=10";
private static final String BING_lOCATIONS_ENDPOINT = "https://dev.virtualearth.net/REST/v1/Locations?"
+ "key=%s"
+ "&userRegion=" + REGION
+ "&countryRegion=" + REGION
+ "&c=" + CULTURE
+ "&userMapView=" + BNG_BOUNDS
+ "&maxResults=1"
+ "%s";

private static final Logger LOG = LoggerFactory.getLogger(BingSearchService.class);

Expand Down Expand Up @@ -76,29 +94,58 @@ public Single<SearchTaskResult> retrieveSearchResults(final String query) {
}

private void retrieveSuggestions(final String query, final Map<String, SearchSuggestion> sugs) {
final String url = String.format(PDOK_SUGGEST_ENDPOINT, query, apiKey);
final String url = String.format(BING_SUGGEST_ENDPOINT, query, apiKey);

// First obtain suggestions, then translate them to actual locations.
final HttpResponse<JsonNode> json = Unirest.get(url).asJson();
final JSONObject body = json.getBody().getObject();

final JSONArray arr = body.getJSONArray("resourceSets").getJSONObject(0).getJSONArray("resources");
final JSONArray arr = body
.getJSONArray("resourceSets").getJSONObject(0)
.getJSONArray("resources").getJSONObject(0)
.getJSONArray("value");
// As the list of suggestions can contain duplicates when we translate them to actual locations, filter those out by using a set
// Use a LinkedHashSet to keep the order.
final Set<SuggestedLocation> suggestedLocations = new LinkedHashSet<>();
for (int i = 0; i < arr.length(); i++) {
final JSONObject jsonObject = arr.getJSONObject(i);

// Skip if the result is not in GB
if (!jsonObject.getJSONObject("address").getString("countryRegionIso2").equals(REGION)) {
continue;
BertScholten marked this conversation as resolved.
Show resolved Hide resolved
final JSONObject jsonObject = arr.getJSONObject(i).getJSONObject("address");
final SuggestedLocation suggestedLocation = createSuggestedLocation(jsonObject);
suggestedLocations.add(suggestedLocation);
}
// Now convert the suggestions by Bing to actual locations, to obtain geo information.
int i = 0;
for (final SuggestedLocation suggestedLocation : suggestedLocations) {
final JSONObject jsonObject = obtainLocation(suggestedLocation);
if (jsonObject != null) {
final SearchSuggestion sug = createSuggestion(query, i, jsonObject);
sugs.merge(sug.getDescription(), sug, (a, b) -> a.getScore() > b.getScore() ? a : b);
i++;
}

final SearchSuggestion sug = createSuggestion(query, i, jsonObject);
sugs.merge(sug.getDescription(), sug, (a, b) -> a.getScore() > b.getScore() ? a : b);
}
}

private SuggestedLocation createSuggestedLocation(final JSONObject jsonObject) {
return new SuggestedLocation(jsonObject.optString("locality"),
jsonObject.optString("adminDistrict2"),
jsonObject.optString("addressLine"),
jsonObject.optString("formattedAddress"));
}

private JSONObject obtainLocation(final SuggestedLocation location) {
final String url = String.format(BING_lOCATIONS_ENDPOINT, apiKey, location.toUrlParameters());
final HttpResponse<JsonNode> json = Unirest.get(url).asJson();
final JSONObject body = json.getBody().getObject();
final JSONArray arr = body.getJSONArray("resourceSets").getJSONObject(0).getJSONArray("resources");
return arr.length() == 0 ? null : arr.getJSONObject(0);
}

private SearchSuggestion createSuggestion(final String query, final int idx, final JSONObject jsonObject) {
final String id = "id-" + query + "-" + idx;
final String displayText = jsonObject.getString("name");
final double score = getScoreFromConfidence(jsonObject.getString("confidence"));
// While there is a confidence bit, as we use a 2nd call we can't really use it
/// (the confidence is always based on input for that second call, which isn't user input)
// Instead, assume that Bing knows what it's doing with the autosuggest and score earlier records higher
final double score = 90D - idx;
final SearchSuggestionType type = determineType(jsonObject.getString("entityType"));
final JSONArray bbox = jsonObject.getJSONArray("bbox");
final String wktBbox = "POLYGON(("
Expand All @@ -118,19 +165,6 @@ private SearchSuggestion createSuggestion(final String query, final int idx, fin
return suggestion;
}

private static double getScoreFromConfidence(final String confidence) {
switch (confidence) {
case "High":
return 90D;
case "Medium":
return 50D;
case "Low":
return 30D;
default:
return 15D;
}
}

private static SearchSuggestionType determineType(final String type) {
SearchSuggestionType suggestionType;

Expand Down Expand Up @@ -162,4 +196,58 @@ private static SearchSuggestionType determineType(final String type) {

return suggestionType;
}

private static class SuggestedLocation {

private final String locality;
private final String adminDistrict;
private final String addressLine;
private final String formattedAddress;

SuggestedLocation(final String locality, final String adminDistrict, final String addressLine, final String formattedAddress) {
this.locality = locality;
this.adminDistrict = adminDistrict;
this.addressLine = addressLine;
this.formattedAddress = formattedAddress;
}

@Override
public int hashCode() {
return Objects.hash(addressLine, adminDistrict, formattedAddress, locality);
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final SuggestedLocation other = (SuggestedLocation) obj;
return Objects.equals(addressLine, other.addressLine) && Objects.equals(adminDistrict, other.adminDistrict)
&& Objects.equals(formattedAddress, other.formattedAddress) && Objects.equals(locality, other.locality);
}

String toUrlParameters() {
final Map<String, String> parameters = new HashMap<>();
if (locality != null) {
parameters.put("locality", locality);
}
if (adminDistrict != null) {
parameters.put("adminDistrict", adminDistrict);
}
if (addressLine != null) {
parameters.put("addressLine", addressLine);
}
if (formattedAddress != null) {
parameters.put("query", formattedAddress);
}
return parameters.isEmpty()
? ""
: "&" + parameters.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining("&"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ void testWorksAtAll() {
return;
}

final Single<SearchTaskResult> result = delegator.retrieveSearchResults("utrecht");
final Single<SearchTaskResult> result = delegator.retrieveSearchResults("edin");

final SearchTaskResult suggestions = result.blockingGet();

assertEquals(10, suggestions.getSuggestions().size(), "Expected 10 results for 'utrecht'");
assertEquals(7, suggestions.getSuggestions().size(), "Expected number of results for 'edin' (should include 'edinburgh')");
}
}
Loading