forked from apache/usergrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This closes apache#430 and adds geo-distance to queries with a within…
… clause
- Loading branch information
Showing
9 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,49 @@ public void testMovingTarget() throws Exception { | |
em.delete(user); | ||
} | ||
|
||
|
||
/** | ||
* Validate the ability to query a moving entity | ||
* 1. Create an entity with location | ||
* 2. Query from a point near the entity's location | ||
* 3. Move the entity farther away from the center point | ||
* 4. Run the same query again to verify the entity is no longer in the area | ||
*/ | ||
@Test | ||
public void validateDistanceQueryExists() throws Exception { | ||
LOG.info("GeoIT.validateDistanceQueryExists"); | ||
//Get the EntityManager instance | ||
EntityManager em = app.getEntityManager(); | ||
assertNotNull(em); | ||
|
||
//1. Create an entity with location | ||
Map<String, Object> properties = new LinkedHashMap<String, Object>() {{ | ||
put("username", "edanuff"); | ||
put("email", "[email protected]"); | ||
put("location", new LinkedHashMap<String, Object>() {{ | ||
put("latitude", 37.776753); | ||
put("longitude", -122.407846); | ||
}}); | ||
}}; | ||
Entity user = em.create("user", properties); | ||
assertNotNull(user); | ||
app.refreshIndex(); | ||
|
||
final double lat = 37.776753; | ||
final double lon = -122.407846; | ||
|
||
//2. Query from a point near the entity's location | ||
Query query = Query.fromQL("select * where location within 100 of " | ||
+ lat + "," + lon); | ||
Results listResults = em.searchCollection(em.getApplicationRef(), "users", query); | ||
assertEquals(1, listResults.size()); | ||
Entity entity = listResults.getEntity(); | ||
assertTrue(entity.getMetadata("distance")!=null); | ||
assertTrue(Double.parseDouble( entity.getMetadata("distance").toString())>0); | ||
|
||
em.delete(user); | ||
} | ||
|
||
/** | ||
* Validate the ability to query connections within proximity of the users | ||
* 1. Create an entity with location | ||
|
@@ -461,11 +504,20 @@ public void testPointPaging() throws Exception { | |
int count = 0; | ||
Results results; | ||
|
||
double previousDistance = 0d; | ||
do { | ||
results = em.searchCollection(em.getApplicationRef(), "stores", query); | ||
|
||
for (Entity entity : results.getEntities()) { | ||
assertEquals(String.valueOf(count), entity.getName()); | ||
|
||
Object distanceObject = entity.getMetadata("distance"); | ||
assertNotNull( distanceObject ); | ||
assertTrue( distanceObject instanceof Double ); | ||
double distance = (Double)distanceObject; | ||
assertTrue( distance >= previousDistance ); | ||
previousDistance = distance; | ||
|
||
count++; | ||
} | ||
|
||
|
@@ -508,13 +560,22 @@ public void testSamePointPaging() throws Exception { | |
int count = 0; | ||
Results results; | ||
|
||
|
||
double previousDistance = 0d; | ||
do { | ||
results = em.searchCollection(em.getApplicationRef(), "stores", query); | ||
|
||
for (Entity entity : results.getEntities()) { | ||
//TODO:can we assert order | ||
final int expected = numEntities - count - 1; | ||
|
||
Object distanceObject = entity.getMetadata("distance"); | ||
assertNotNull( distanceObject ); | ||
assertTrue( distanceObject instanceof Double ); | ||
double distance = (Double)distanceObject; | ||
assertTrue( distance >= previousDistance ); | ||
previousDistance = distance; | ||
|
||
assertEquals(String.valueOf(expected), entity.getName()); | ||
count++; | ||
} | ||
|
@@ -574,8 +635,18 @@ public void testDistanceByLimit() throws Exception { | |
do { | ||
Results results = em.searchCollection(em.getApplicationRef(), "stores", query); | ||
|
||
double previousDistance = 0d; | ||
|
||
for (Entity entity : results.getEntities()) { | ||
assertEquals(String.valueOf(count), entity.getName()); | ||
|
||
Object distanceObject = entity.getMetadata("distance"); | ||
assertNotNull( distanceObject ); | ||
assertTrue( distanceObject instanceof Double ); | ||
double distance = (Double)distanceObject; | ||
assertTrue( distance >= previousDistance ); | ||
previousDistance = distance; | ||
|
||
count++; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...stence/model/src/main/java/org/apache/usergrid/persistence/model/field/DistanceField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* | ||
* * Licensed to the Apache Software Foundation (ASF) under one or more | ||
* * contributor license agreements. The ASF licenses this file to You | ||
* * under the Apache License, Version 2.0 (the "License"); you may not | ||
* * use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * http://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. For additional information regarding | ||
* * copyright in this work, please see the NOTICE file in the top level | ||
* * directory of this distribution. | ||
* | ||
*/ | ||
package org.apache.usergrid.persistence.model.field; | ||
|
||
/** | ||
* Distance field to pass distance down the parsing chain. | ||
*/ | ||
public class DistanceField extends DoubleField { | ||
public static final String NAME = "distance"; | ||
public DistanceField( Double value) { | ||
super(NAME, value); | ||
} | ||
|
||
public DistanceField( Double value, boolean unique) { | ||
super(NAME, value, unique); | ||
} | ||
|
||
public DistanceField() { | ||
super(); | ||
} | ||
@Override | ||
public FieldTypeName getTypeName() { | ||
return FieldTypeName.DISTANCE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,6 @@ public enum FieldTypeName { | |
LONG, | ||
SET, | ||
STRING, | ||
UUID | ||
UUID, | ||
DISTANCE | ||
} |
40 changes: 40 additions & 0 deletions
40
...ce/queryindex/src/main/java/org/apache/usergrid/persistence/index/GeoCandidateResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* | ||
* * Licensed to the Apache Software Foundation (ASF) under one or more | ||
* * contributor license agreements. The ASF licenses this file to You | ||
* * under the Apache License, Version 2.0 (the "License"); you may not | ||
* * use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * http://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. For additional information regarding | ||
* * copyright in this work, please see the NOTICE file in the top level | ||
* * directory of this distribution. | ||
* | ||
*/ | ||
package org.apache.usergrid.persistence.index; | ||
|
||
import org.apache.usergrid.persistence.model.entity.Id; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Result for Geo candidates | ||
*/ | ||
public class GeoCandidateResult extends CandidateResult { | ||
private final double distance; | ||
|
||
public GeoCandidateResult(Id entityId, UUID entityVersion, String docId, double distance) { | ||
super(entityId, entityVersion, docId); | ||
this.distance = distance; | ||
} | ||
|
||
public double getDistance() { | ||
return distance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters