问题
I'm using dynamic queries to search SOLR and I'm trying to execute a complicated geolocation query for a store locator feature. It should retrieve up to 5 locations within a distance D from a location (Point). It should also sort by distance, and return the distance in the result.
I can't figure out how to get the API to output the right query though.
Could someone please guide me in how to use Spring SOLR Data API to get this query output correctly?
I'm using SOLR 4+ geofilter feature.
The SOLR query I need is this (working directly):
http://localhost:8983/solr/aust/select?q={!func}geodist()&fl=*,score,geodist()&fq={!geofilt}&pt=-37.818635,145.0014704&sfield=location&d=15&fq=docType:LOCATION&rows=5&wt=json&indent=true&sort=score asc
And my code so far looks like this (not working):
@Override
public SimpleSearchResults searchByRegion (SimpleSearchForm searchForm, RegionDocument region)
{
logger.entry();
SimpleQuery query = new SimpleQuery();
query.addCriteria(new Criteria("text").contains(Criteria.WILDCARD));
int distance = 5;
Point point = new org.springframework.data.solr.core.geo.Point(region.getLat(), region.getLng());
GeoDistanceFunction geo = GeoDistanceFunction.distanceFrom("location").to(point);
query.addFilterQuery(new SimpleFilterQuery(Criteria.where(geo)));
SimpleFilterQuery typeQuery = new SimpleFilterQuery();
typeQuery.addCriteria(new Criteria("docType").is("LOCATION"));
Sort sort = new Sort(Sort.Direction.ASC, "score");
query.addFilterQuery(typeQuery);
// add paging to request
PageRequest page = new PageRequest(0, 5, sort);
query.setPageRequest(page);
// we build the search request without facets initially
//search.setPageRequest(page);
Page<LocationDocument> results = solrTemplate1.queryForPage(query, LocationDocument.class);
logger.exit();
SimpleSearchResults dto2 = new SimpleSearchResults();
dto2.setSearchTerm(searchForm.getSearchTerm());
dto2.setPage(results);
return dto2;
}
回答1:
For reference I managed to get this working after a variation on a solution posted here: How to return distance and score in spatial search using spring-data-solr
Because I'm using SOLR 4 type of location_rpt, the geodist() function didn't support the type. So in the documentation they suggest an alternative of returning the distance using the score parameter. (note it's returned as degrees and needs to be converted = if anyone knows how to do that conversion on SOLR then please post it here).
So I ended up using a simple String Criteria to add the score=distance phrase into the Geofilter expression.
Here's my code to get the results of a geofilter, sorted by distance using the Spring Data SOLR API and location_rpt schema type.
@Override
public SimpleSearchResults searchByRegion(SimpleSearchForm searchForm, RegionDocument region) {
logger.entry();
SimpleQuery query = new SimpleQuery();
query.addProjectionOnField("*");
query.addProjectionOnField("score");
StringBuffer buf = new StringBuffer("");
buf.append("{!geofilt pt=");
buf.append(region.getLat());
buf.append(",");
buf.append(region.getLng());
buf.append(" sfield=location d=");
buf.append(5.0);
buf.append(" score=distance}");
query.addCriteria(new SimpleStringCriteria(buf.toString()));
FilterQuery docType = new SimpleFilterQuery();
docType.addCriteria(new Criteria("docType").is("LOCATION"));
query.addFilterQuery(docType);
query.addSort(new Sort(Sort.Direction.ASC, "score"));
Page<LocationDocument> results = solrTemplate1.queryForPage(query, LocationDocument.class);
SimpleSearchResults dto2 = new SimpleSearchResults();
dto2.setSearchTerm(searchForm.getSearchTerm());
if (results.getContent().isEmpty()) {
logger.debug("results were empty.");
} else {
dto2.setPage(results);
}
logger.exit();
return dto2;
}
来源:https://stackoverflow.com/questions/25461757/solr-spring-data-geospatial-query