Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Aug 21, 2015
1 parent 5109323 commit 8f782f6
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private OIndexInternal<?> createLuceneIndex(String name, ODatabaseDocumentIntern
new OLuceneFullTextIndexManager(), indexType), valueContainerAlgorithm, metadata);
} else if (OClass.INDEX_TYPE.SPATIAL.toString().equals(indexType)) {
return new OLuceneSpatialIndex(name, indexType, LUCENE_ALGORITHM, new OLuceneIndexEngine<Set<OIdentifiable>>(
new OLuceneSpatialIndexManager(new OShapeFactoryImpl()), indexType), valueContainerAlgorithm, metadata);
new OLuceneSpatialIndexManager(OShapeFactoryImpl.INSTANCE), indexType), valueContainerAlgorithm, metadata);
}
throw new OConfigurationException("Unsupported type : " + indexType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField;
import com.orientechnologies.orient.core.sql.operator.OIndexReuseType;
import com.orientechnologies.orient.core.sql.operator.OQueryTargetOperator;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Circle;
import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.SpatialRelation;

import java.util.Collection;
import java.util.List;
Expand All @@ -45,11 +51,32 @@ public OLuceneNearOperator() {
public Object evaluateRecord(OIdentifiable iRecord, ODocument iCurrentResult, OSQLFilterCondition iCondition, Object iLeft,
Object iRight, OCommandContext iContext) {

if (iContext.getVariable("$luceneIndex") != null) {
return true;
} else {
return false;
List<Number> left = (List<Number>) iLeft;

double lat = left.get(0).doubleValue();
double lon = left.get(1).doubleValue();

Shape shape = SpatialContext.GEO.makePoint(lon, lat);
List<Number> right = (List<Number>) iRight;

double lat1 = right.get(0).doubleValue();
double lon1 = right.get(1).doubleValue();
Shape shape1 = SpatialContext.GEO.makePoint(lon1, lat1);

Map map = (Map) right.get(2);
double distance = 0;

Number n = (Number) map.get("maxDistance");
if (n != null) {
distance = n.doubleValue();
}
Point p = (Point) shape1;
Circle circle = SpatialContext.GEO.makeCircle(p.getX(), p.getY(),
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM));
double docDistDEG = SpatialContext.GEO.getDistCalc().distance((Point) shape, p);
final double docDistInKM = DistanceUtils.degrees2Dist(docDistDEG, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM);
iContext.setVariable("distance", docDistInKM);
return shape.relate(circle) == SpatialRelation.WITHIN;
}

private Object[] parseParams(OIdentifiable iRecord, OSQLFilterCondition iCondition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.orientechnologies.lucene.operator;

import com.orientechnologies.lucene.collections.OSpatialCompositeKey;
import com.orientechnologies.lucene.shape.OShapeFactoryImpl;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
Expand All @@ -28,13 +29,18 @@
import com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition;
import com.orientechnologies.orient.core.sql.operator.OIndexReuseType;
import com.orientechnologies.orient.core.sql.operator.OQueryTargetOperator;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.SpatialRelation;
import org.apache.lucene.spatial.query.SpatialOperation;

import java.util.Collection;
import java.util.List;

public class OLuceneWithinOperator extends OQueryTargetOperator {

OShapeFactoryImpl shapeFactory = OShapeFactoryImpl.INSTANCE;

public OLuceneWithinOperator() {
super("WITHIN", 5, false);
}
Expand All @@ -48,11 +54,16 @@ public Collection<OIdentifiable> filterRecords(ODatabase<?> iRecord, List<String
@Override
public Object evaluateRecord(OIdentifiable iRecord, ODocument iCurrentResult, OSQLFilterCondition iCondition, Object iLeft,
Object iRight, OCommandContext iContext) {
if (iContext.getVariable("$luceneIndex") != null) {
return true;
} else {
return false;
}
List<Number> left = (List<Number>) iLeft;

double lat = left.get(0).doubleValue();
double lon = left.get(1).doubleValue();

Shape shape = SpatialContext.GEO.makePoint(lon, lat);

Shape shape1 = shapeFactory.makeShape(new OSpatialCompositeKey((List<?>) iRight), SpatialContext.GEO);

return shape.relate(shape1) == SpatialRelation.WITHIN;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package com.orientechnologies.lucene.shape;

import com.orientechnologies.lucene.collections.OSpatialCompositeKey;
import com.orientechnologies.orient.core.index.OCompositeKey;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Rectangle;
import com.spatial4j.core.shape.Shape;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OShapeFactoryImpl implements OShapeFactory {

private Map<Class<? extends Shape>, OShapeFactory> factories = new HashMap<Class<? extends Shape>, OShapeFactory>();

public OShapeFactoryImpl() {
public static OShapeFactoryImpl INSTANCE = new OShapeFactoryImpl();
protected OShapeFactoryImpl() {
registerFactory(Point.class, new OPointShapeFactory());
registerFactory(Rectangle.class, new ORectangleShapeFactory());
registerFactory(Shape.class, new OPolygonShapeFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@

package com.orientechnologies.lucene.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.index.OIndex;
Expand All @@ -35,7 +26,10 @@
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.tx.OTransaction;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Collection;

/**
* Created by enricorisa on 28/06/14.
Expand All @@ -46,7 +40,7 @@ public class LuceneInsertMultithreadTest {

private final static int THREADS = 10;
private final static int RTHREADS = 1;
private final static int CYCLE = 1000;
private final static int CYCLE = 100;
private ODatabaseDocument databaseDocumentTx;

private static String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LuceneInsertReadMultithreadTest extends BaseLuceneTest {

private final static int THREADS = 10;
private final static int RTHREADS = 1;
private final static int CYCLE = 1000;
private final static int CYCLE = 100;

protected String url = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void init() {
public void loadCloseDelete() {

ODocument city = new ODocument("City");
int size = 100000;
int size = 1000;
for (int i = 0; i < size; i++) {
city.field("name", "Rome " + i);
databaseDocumentTx.save(city);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.testng.annotations.Test;

@Test(groups = "remote")
@Test(enabled = false)
public class LuceneSpatialQueryRemoteTest extends LuceneSpatialQueryTest {

public LuceneSpatialQueryRemoteTest() {
Expand All @@ -31,4 +31,29 @@ public LuceneSpatialQueryRemoteTest() {
protected String getDatabaseName() {
return super.getDatabaseName() + "Remote";
}

@Test(enabled = false)
@Override
public void testNearQuery() {
super.testNearQuery();
}

@Test(enabled = false)
@Override
public void testNearQueryWithoutIndex() {
super.testNearQueryWithoutIndex();

}

@Test(enabled = false)
@Override
public void testWithinQuery() {
super.testWithinQuery();
}

@Test(enabled = false)
@Override
public void testWithinQueryWithoutIndex() {
super.testWithinQueryWithoutIndex();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,42 @@ public void deInit() {
deInitDB();
}

@Test
@Test(priority = 1)
public void testNearQuery() {

assertNear();
}

@Test(priority = 3)
public void testNearQueryWithoutIndex() {

databaseDocumentTx.command(new OCommandSQL("drop index Place.l_lon")).execute();
assertNear();
}

@Test(priority = 4)
public void testWithinQueryWithoutIndex() {
databaseDocumentTx.command(new OCommandSQL("drop index Place.l_lon")).execute();
assertWithin();
}

protected void assertNear() {
String query = "select *,$distance from Place where [latitude,longitude,$spatial] NEAR [41.893056,12.482778,{\"maxDistance\": 0.5}]";
List<ODocument> docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(query));

Assert.assertEquals(1, docs.size());

// WHY ? 0.2749329729746763
// Assert.assertEquals(0.27504313167833594, docs.get(0).field("$distance"));
Assert.assertEquals(0.2749329729746763, docs.get(0).field("$distance"));
}

@Test
@Test(priority = 2)
public void testWithinQuery() {
String query = "select * from Place where [latitude,longitude] WITHIN [[51.507222,-0.1275],[55.507222,-0.1275]]";
assertWithin();
}

protected void assertWithin() {
String query = "select * from Place where [latitude,longitude] WITHIN [[51.286839,-0.51035],[51.692322,0.33403]]";
List<ODocument> docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(238, docs.size());
Assert.assertEquals(291, docs.size());
}
}
Binary file modified src/test/resources/location.csv.zip
Binary file not shown.

0 comments on commit 8f782f6

Please sign in to comment.