Skip to content

Commit

Permalink
Merge pull request #105 from midas-isg/search_by_name
Browse files Browse the repository at this point in the history
Fix bug in find-by-term end-point
  • Loading branch information
espinoj authored Sep 20, 2017
2 parents 5d2563d + 40ed312 commit 9d7f366
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/controllers/LocationServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ public Result filterByTerm(
+ "<li><i>#unique terms</i> in location name/code in ascending order</li>"
+ "<li>location <em>area</em> in descending order</li>"
+ "<li><i>alphabetical order</i> of location name/code ascendingly."
+ "</ul>"
+ "<b>FuzzyMatch feature:</b>:"
+ "<ul>"
+ "<li>Uses Postgresql pg_trgm extension to measure the similarity of two strings by counting the number of trigrams they share."
+ "<li>The threshold must be between 0 and 1 (default is 0.3)"
+ "<li>For details of 'fuzzyMatch' feature refer to: https://www.postgresql.org/docs/9.3/static/pgtrgm.html"
+ "</ul>",
response = FeatureCollection.class)
@ApiResponses(value = {
Expand Down
8 changes: 4 additions & 4 deletions app/dao/SearchSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ private String toNameSearchSql(Request req, String queryTerm) {
q += " SELECT gid, name, " + rankingStatement + " AS rank, "
+ headlineStatement + " AS headline "
+ " FROM {h-schema}location "
//+ joinLocGeomStatement
+ " WHERE " + comparisonStatement;
if (containsFilters(req))
q += " AND " + toQueryConditionSQL(req);
Expand Down Expand Up @@ -352,9 +351,10 @@ private boolean containsFilters(Request req) {
List<Long> typeIds = req.getLocationTypeIds();
Date start = req.getStartDate();
Date end = req.getEndDate();
if (typeIds != null && !typeIds.isEmpty())
return true;
if (start != null || end != null)
Long rootALC = req.getRootALC();
List<Long> codeTypeIds = req.getCodeTypeIds();
if ((typeIds != null && !typeIds.isEmpty()) || (codeTypeIds != null && !codeTypeIds.isEmpty())
|| start != null || end != null || rootALC != null)
return true;
return false;
}
Expand Down
100 changes: 96 additions & 4 deletions test/integrations/server/TestFindLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TestFindLocation {
private String basePath = "api/locations";
private String findBulkPath = basePath + "/find-bulk";
private String findByTermPath = basePath + "/find-by-term";
private String findByFilterPath = basePath + "/find-by-filter";
private long gidTest1;
private long gidTest2;
private String jsonContentType = "application/json; charset=utf-8";
Expand All @@ -60,8 +61,8 @@ public void testFindLocation() {
gidTest1 = createLocationFromFile("test/testLocation1.geojson");

try {
findByQueryTermTest2();
findByQueryTermTest();
findByFilterTest();
findByTermTest();
findByIdTest();
findByNameTest();
findBulkTest();
Expand All @@ -78,7 +79,21 @@ public void testFindLocation() {

}

private void findByQueryTermTest2() {
private void findByTermTest() {

findByQueryTermOldVersion();
findByQueryTermTestCurrentVersion();
findByTermAndCountryTest();
findByTermAndAllFiltersTest();
}

private void findByFilterTest() {

findByLocationTypeTest();
findByCodeTypeIdTest();
}

private void findByQueryTermTestCurrentVersion() {

String body = "{\"queryTerm\":\"testUnit 123\","
+ "\"limit\":10,"
Expand Down Expand Up @@ -155,8 +170,85 @@ private void findByQueryTermTest2() {
assertThat(name).isEqualTo("testUnit abc def ghi 123");
assertThat(rank).isEqualTo(1.0);
}

private void findByTermAndCountryTest() {

String body = "{\"queryTerm\":\"US\","
+ "\"limit\":0,"
+ "\"logic\":\"OR\","
+ "\"rooALC\":1216,"
+ "\"onlyFeatureFields\":"
+ "[\"properties.name\", "
+ "\"properties.matchedTerm\", "
+ "\"properties.rank\", "
+ "\"properties.headline\"]"
+ "}";
String url = Server.makeTestUrl(findByTermPath + "?_v=" + CURRENT_VERSION);
WSResponse response = post(url, body, jsonContentType);
assertStatus(response, OK);
JsonNode jsonResp = response.asJson();
assertAreEqual(jsonResp.size(), 3);
assertAreEqual(jsonResp.get("features").size(), 3);
}

private void findByTermAndAllFiltersTest() {

String body = "{\"queryTerm\":\"Pennsylvania Texas Allegheny America\","
+ "\"limit\":10,"
+ "\"logic\":\"OR\","
+ "\"locationTypeIds\":[16, 19],"
+ "\"codeTypeIds\":[1,7],"
+ "\"startDate\":\"1700\","
+ "\"endDate\":\"1800\","
+ "\"onlyFeatureFields\":"
+ "[\"properties.name\", "
+ "\"properties.matchedTerm\", "
+ "\"properties.rank\", "
+ "\"properties.headline\"]"
+ "}";
String url = Server.makeTestUrl(findByTermPath + "?_v=" + CURRENT_VERSION);
WSResponse response = post(url, body, jsonContentType);
assertStatus(response, OK);
JsonNode jsonResp = response.asJson();
assertAreEqual(jsonResp.size(), 3);
assertAreEqual(jsonResp.get("features").size(), 1);
}

private void findByLocationTypeTest() {
String body = "{\"locationTypeIds\":[4, 19],"
+ "\"limit\":10,"
+ "\"onlyFeatureFields\":"
+ "[\"properties.name\", "
+ "\"properties.matchedTerm\", "
+ "\"properties.rank\", "
+ "\"properties.headline\"]"
+ "}";
String url = Server.makeTestUrl(findByFilterPath);
WSResponse response = post(url, body, jsonContentType);
assertStatus(response, OK);
JsonNode jsonResp = response.asJson();
assertAreEqual(jsonResp.size(), 3);
assertAreEqual(jsonResp.get("features").size(), 4);
}

private void findByCodeTypeIdTest() {
String body = "{\"codeTypeIds\":[7],"
+ "\"limit\":10,"
+ "\"onlyFeatureFields\":"
+ "[\"properties.name\", "
+ "\"properties.matchedTerm\", "
+ "\"properties.rank\", "
+ "\"properties.headline\"]"
+ "}";
String url = Server.makeTestUrl(findByFilterPath);
WSResponse response = post(url, body, jsonContentType);
assertStatus(response, OK);
JsonNode jsonResp = response.asJson();
assertAreEqual(jsonResp.size(), 3);
assertAreEqual(jsonResp.get("features").size(), 9);
}

private void findByQueryTermTest() {
private void findByQueryTermOldVersion() {
String body = KmlRule.getStringFromFile(findByTermRequestFile1);
String url = Server.makeTestUrl(findByTermPath);
WSResponse response = post(url, body, jsonContentType);
Expand Down

0 comments on commit 9d7f366

Please sign in to comment.