Query name and location in radius (1.4.2 b)
This commit is contained in:
parent
fc6ea761d3
commit
d5df23a8c3
@ -1,11 +1,17 @@
|
||||
package dst.ass1.doc.impl;
|
||||
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.mongodb.client.model.Indexes;
|
||||
import com.mongodb.client.model.Projections;
|
||||
import com.mongodb.client.model.geojson.Point;
|
||||
import com.mongodb.client.model.geojson.Position;
|
||||
import dst.ass1.doc.IDocumentQuery;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static dst.ass1.jpa.util.Constants.*;
|
||||
@ -25,7 +31,27 @@ public class DocumentQuery implements IDocumentQuery {
|
||||
|
||||
@Override
|
||||
public List<Long> findIdsByNameAndRadius(String name, double longitude, double latitude, double radius) {
|
||||
return null;
|
||||
MongoCollection<Document> collection = db.getCollection(COLL_LOCATION_DATA);
|
||||
collection.createIndex(Indexes.geo2dsphere(M_LOCATION_GEO));
|
||||
Point refPoint = new Point(new Position(longitude, latitude));
|
||||
|
||||
List<Long> result = new ArrayList<>();
|
||||
|
||||
MongoCursor<Document> cursor = collection.find(
|
||||
Filters.and(
|
||||
Filters.regex(M_LOCATION_NAME, ".*" + name + ".*"),
|
||||
Filters.nearSphere(M_LOCATION_GEO, refPoint, radius, 0.0)
|
||||
)
|
||||
).projection(Projections.include(I_LOCATION)).iterator();
|
||||
|
||||
try {
|
||||
while (cursor.hasNext())
|
||||
result.add(cursor.next().getLong(I_LOCATION));
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user