WIP city search
LocationBasedServices/mowesta/pipeline/head This commit looks good

This commit is contained in:
2021-02-23 00:43:40 +01:00
parent 1ee872e668
commit 252ee01ca0
10 changed files with 75 additions and 23 deletions
@@ -59,6 +59,27 @@ public interface ILocationService {
@ApiParam(value="The search term.", required=true) @QueryParam("term") @NotNull @Valid String term);
/**
* Returns a page of cities that match the query.
*
* @param pageConfig The page configuration.
* @param context The search context.
* @param term The search term.
* @return A page of hierarchically organized locations that match
* the query.
*/
@GET
@Path("locations/search")
@ApiOperation(value = "Returns a page of cities whose name matches the term.", tags = { Tags.DEFAULT, Tags.LOCATION })
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_OK, message="Location hierarchy page retrieved successfully.", response=LocationHierarchyPage.class),
@ApiResponse(code=HttpServletResponse.SC_BAD_REQUEST, message="Invalid request parameter.")
})
@PermitAll
Response searchCities(@BeanParam @Valid PageConfig pageConfig, @BeanParam @Valid SearchContext context,
@ApiParam(value="The search term.", required=true) @QueryParam("term") @NotNull @Valid String term);
/**
* Returns a page of cities that are inside a particular area defined by two coordinates.
@@ -79,7 +79,7 @@ public class ClothingRecommendationIntentHandler implements IntentRequestHandler
if (city == null) {
location = alexaIntentService.getGeoLocation(input);
} else {
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city);
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city, false);
location = alexaIntentService.getLocation(intentRequest, locationHierarchyPage);
}
@@ -65,7 +65,7 @@ public class CurrentWeatherIntentHandler implements IntentRequestHandler {
if (city == null) {
location = alexaIntentService.getGeoLocation(input);
} else {
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city);
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city, false);
location = alexaIntentService.getLocation(intentRequest, locationHierarchyPage);
}
@@ -80,7 +80,7 @@ public class WeatherForecastIntentHandler implements IntentRequestHandler {
if (city == null) {
location = alexaIntentService.getGeoLocation(input);
} else {
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city);
LocationHierarchyPage locationHierarchyPage = locationSessionBean.findLocations(new PageConfig(0, 60), new SearchContext(language), city, false);
location = alexaIntentService.getLocation(intentRequest, locationHierarchyPage);
}
@@ -257,10 +257,11 @@ public abstract class AbstractLocationEntity extends AbstractSpatialEntity {
* @param em The entity manager.
* @param language The user interface language.
* @param term The search term.
* @param onlyCities True if only cities should be searched
* @return The count.
*/
public static int countAllByTerm(EntityManager em, Language language, String term) {
FullTextQuery query = createQueryByTerm(em, language, term);
public static int countAllByTerm(EntityManager em, Language language, String term, boolean onlyCities) {
FullTextQuery query = createQueryByTerm(em, language, term, onlyCities);
return query.getResultSize();
}
@@ -272,11 +273,14 @@ public abstract class AbstractLocationEntity extends AbstractSpatialEntity {
* @param config The page config to limit the result.
* @param language The user interface language.
* @param term The search term.
* @param onlyCities True if only cities should be searched.
* @return The result list.
*/
@SuppressWarnings("unchecked")
public static List<AbstractLocationEntity> findAllByTerm(EntityManager em, PageConfig config, Language language, String term) {
FullTextQuery query = createQueryByTerm(em, language, term);
public static List<AbstractLocationEntity> findAllByTerm(EntityManager em, PageConfig config,
Language language, String term,
boolean onlyCities) {
FullTextQuery query = createQueryByTerm(em, language, term, onlyCities);
query.setSort(new Sort(SortField.FIELD_SCORE, new SortField("importance", Type.DOUBLE, true), new SortField("searchRank", Type.INT)));
query.setFirstResult(config.getOffset());
query.setMaxResults(config.getSize());
@@ -293,15 +297,24 @@ public abstract class AbstractLocationEntity extends AbstractSpatialEntity {
* @param em The entity manager.
* @param language The ui language.
* @param term The term to search.
* @param onlyCities True if only cities should be considered
* @return The query.
*/
private static FullTextQuery createQueryByTerm(EntityManager em, Language language, String term) {
private static FullTextQuery createQueryByTerm(EntityManager em, Language language, String term,
boolean onlyCities) {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder baseBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(AbstractLocationEntity.class).get();
Query q1 = createSubQuery(fullTextEntityManager, CityEntity.class, new String[] { "names", "state.names", "country.names" }, language, term);
Query q2 = createSubQuery(fullTextEntityManager, StateEntity.class, new String[] { "names", "country.names" }, language, term);
Query q3 = createSubQuery(fullTextEntityManager, CountryEntity.class, new String[] { "names" }, language, term);
Query query = baseBuilder.bool().should(q1).should(q2).should(q3).createQuery();
Query query;
if (onlyCities) {
Query q1 = createSubQuery(fullTextEntityManager, CityEntity.class, new String[]{"names"}, language, term);
query = baseBuilder.bool().should(q1).createQuery();
} else {
Query q1 = createSubQuery(fullTextEntityManager, CityEntity.class, new String[]{"names", "state.names", "country.names"}, language, term);
Query q2 = createSubQuery(fullTextEntityManager, StateEntity.class, new String[]{"names", "country.names"}, language, term);
Query q3 = createSubQuery(fullTextEntityManager, CountryEntity.class, new String[]{"names"}, language, term);
query = baseBuilder.bool().should(q1).should(q2).should(q3).createQuery();
}
return fullTextEntityManager.createFullTextQuery(query, AbstractLocationEntity.class);
}
@@ -54,15 +54,16 @@ public class LocationSessionBean extends AbstractSessionBean {
* @return The page with the results.
*/
@PermitAll
public LocationHierarchyPage findLocations(PageConfig config, SearchContext context, String term) {
public LocationHierarchyPage findLocations(PageConfig config, SearchContext context, String term,
boolean onlyCities) {
LocationHierarchyPage page = new LocationHierarchyPage();
page.setOffset(config.getOffset());
page.setSize(config.getSize());
if (term != null && term.length() > 1) {
log.log(Level.INFO, "Counting locations for search term: " + term);
page.setTotal(AbstractLocationEntity.countAllByTerm(em, context.getLanguage(), term));
page.setTotal(AbstractLocationEntity.countAllByTerm(em, context.getLanguage(), term, onlyCities));
log.log(Level.INFO, "Computing result set for search term: " + term);
List<AbstractLocationEntity> locations = AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), term);
List<AbstractLocationEntity> locations = AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), term, onlyCities);
log.log(Level.INFO, "Converting result set for search term: " + term);
page.setElements(Convert.with(LocationHierarchyConverter.class)
.apply(locations, false));
@@ -416,11 +416,11 @@ public class LocationSyncSessionBean extends AbstractSyncSessionBean {
protected boolean isIndexHealthy() {
PageConfig config = new PageConfig(0, 1);
SearchContext context = new SearchContext(Language.GERMAN);
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Deutschland").size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Essen").size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Deutschland", false).size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Essen", false).size() == 0) return false;
context = new SearchContext(Language.ENGLISH);
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Germany").size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Essen").size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Germany", false).size() == 0) return false;
if (AbstractLocationEntity.findAllByTerm(em, config, context.getLanguage(), "Essen", false).size() == 0) return false;
return true;
}
@@ -160,6 +160,12 @@ public class LocationSessionTest extends AbstractSessionTest {
germanyPage.getElements().get(0).getNames().get(Language.LOCAL).equals("Berlin");
}
@Test
public void testtest() {
LocationHierarchyPage result = locationSession.findLocations(new PageConfig(0, 20), new SearchContext(Language.GERMAN), "Nordrhein-Westfalen", true);
Assert.assertNotNull(result);
}
/**
* Checks that the search for the term has the specified number of
* results from the specified type. The other types are ignored.
@@ -171,7 +177,7 @@ public class LocationSessionTest extends AbstractSessionTest {
* @return The result page.
*/
private LocationHierarchyPage assertSearchResultCount(LocationType type, int results, Language language, String term) {
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 20), new SearchContext(language), term);
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 20), new SearchContext(language), term, false);
int actual = 0;
for (LocationHierarchy location: page.getElements()) {
if (location.getType() == type) actual += 1;
@@ -53,7 +53,7 @@ public class SettingsSessionTest extends AbstractSessionTest {
public void run() {
try {
Settings settings = settingsSession.getCurrentUserSettings();
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 1), new SearchContext(Language.GERMAN), "Bonn");
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 1), new SearchContext(Language.GERMAN), "Bonn", false);
LocationHierarchy bonn = page.getElements().get(0);
Assert.assertEquals("Search result should have one entry.", 1, page.getSize());
Assert.assertEquals("Search result should be type city.", LocationType.CITY, bonn.getType());
@@ -96,7 +96,7 @@ public class SettingsSessionTest extends AbstractSessionTest {
public void run() {
try {
Settings settings = settingsSession.getCurrentUserSettings();
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 1), new SearchContext(Language.GERMAN), "Bonn");
LocationHierarchyPage page = locationSession.findLocations(new PageConfig(0, 1), new SearchContext(Language.GERMAN), "Bonn", false);
LocationHierarchy bonn = page.getElements().get(0);
LocationHierarchy nrw = bonn.getParent();
LocationHierarchy germany = nrw.getParent();
@@ -7,6 +7,7 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Request;
@@ -60,7 +61,17 @@ public class LocationResource implements ILocationService {
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
ResponseBuilder b = request.evaluatePreconditions(tag);
if (b == null) {
b = Response.ok(locationSession.findLocations(pageConfig, context, term));
b = Response.ok(locationSession.findLocations(pageConfig, context, term, false));
}
return b.tag(tag).build();
}
@Override
public Response searchCities(@Valid PageConfig pageConfig, @Valid SearchContext context, @NotNull @Valid String term) {
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
ResponseBuilder b = request.evaluatePreconditions(tag);
if (b == null) {
b = Response.ok(locationSession.findLocations(pageConfig, context, term, true));
}
return b.tag(tag).build();
}