use of org.apache.lucene.spatial.query.SpatialArgs in project titan by thinkaurelius.
the class LuceneIndex method convertQuery.
private final Filter convertQuery(Condition<?> condition, KeyInformation.StoreRetriever informations) {
if (condition instanceof PredicateCondition) {
PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
String key = atom.getKey();
TitanPredicate titanPredicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
Preconditions.checkArgument(value instanceof Number);
return numericFilter(key, (Cmp) titanPredicate, (Number) value);
} else if (value instanceof String) {
Mapping map = Mapping.getMapping(informations.get(key));
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
if (map == Mapping.STRING && titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
if (titanPredicate == Text.CONTAINS) {
value = ((String) value).toLowerCase();
return new TermsFilter(new Term(key, (String) value));
} else if (titanPredicate == Text.CONTAINS_PREFIX) {
value = ((String) value).toLowerCase();
return new PrefixFilter(new Term(key, (String) value));
} else if (titanPredicate == Text.PREFIX) {
return new PrefixFilter(new Term(key, (String) value));
// } else if (titanPredicate == Text.CONTAINS_REGEX) {
// value = ((String) value).toLowerCase();
// return new RegexpQuery(new Term(key,(String)value));
} else if (titanPredicate == Cmp.EQUAL) {
return new TermsFilter(new Term(key, (String) value));
} else if (titanPredicate == Cmp.NOT_EQUAL) {
BooleanFilter q = new BooleanFilter();
q.add(new TermsFilter(new Term(key, (String) value)), BooleanClause.Occur.MUST_NOT);
return q;
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + titanPredicate);
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(titanPredicate == Geo.WITHIN, "Relation is not supported for geo value: " + titanPredicate);
Shape shape = ((Geoshape) value).convert2Spatial4j();
SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin, shape);
return getSpatialStrategy(key).makeFilter(args);
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
BooleanFilter q = new BooleanFilter();
q.add(convertQuery(((Not) condition).getChild(), informations), BooleanClause.Occur.MUST_NOT);
return q;
} else if (condition instanceof And) {
BooleanFilter q = new BooleanFilter();
for (Condition c : condition.getChildren()) {
q.add(convertQuery(c, informations), BooleanClause.Occur.MUST);
}
return q;
} else if (condition instanceof Or) {
BooleanFilter q = new BooleanFilter();
for (Condition c : condition.getChildren()) {
q.add(convertQuery(c, informations), BooleanClause.Occur.SHOULD);
}
return q;
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
}
use of org.apache.lucene.spatial.query.SpatialArgs in project titan by thinkaurelius.
the class LuceneExample method example1.
@Test
public void example1() throws Exception {
Directory dir = FSDirectory.open(path);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_41, analyzer);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(dir, iwc);
indexDocs(writer, "doc1", ImmutableMap.of("name", "The laborious work of John Doe as we know it", "city", "Blumenkamp", "location", Geoshape.point(51.687882, 6.612053), "time", 1000342034));
indexDocs(writer, "doc2", ImmutableMap.of("name", "Life as we know it or not", "city", "Essen", "location", Geoshape.point(51.787882, 6.712053), "time", 1000342034 - 500));
indexDocs(writer, "doc3", ImmutableMap.of("name", "Berlin - poor but sexy and a display of the extraordinary", "city", "Berlin", "location", Geoshape.circle(52.509535, 13.425293, 50), "time", 1000342034 + 2000));
writer.close();
// Search
IndexReader reader = DirectoryReader.open(FSDirectory.open(path));
IndexSearcher searcher = new IndexSearcher(reader);
analyzer = new StandardAnalyzer(Version.LUCENE_41);
// Auesee
BooleanFilter filter = new BooleanFilter();
// filter.add(new TermsFilter(new Term("name_txt","know")), BooleanClause.Occur.MUST);
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, Geoshape.circle(51.666167, 6.58905, 450).convert2Spatial4j());
// filter.add(getSpatialStrategy("location").makeFilter(args), BooleanClause.Occur.MUST);
filter.add(NumericRangeFilter.newLongRange("time", (long) 1000342034, (long) 1000342034, true, true), BooleanClause.Occur.MUST);
// filter.add(NumericRangeFilter.newLongRange("time",(long)1000342034-100,Long.MAX_VALUE,true,true), BooleanClause.Occur.MUST);
// filter.add(NumericRangeFilter.newLongRange("time",Long.MIN_VALUE,(long)1000342034+300,true,true), BooleanClause.Occur.MUST);
filter.add(new PrefixFilter(new Term("city_str", "B")), BooleanClause.Occur.MUST);
TopDocs docs = searcher.search(new MatchAllDocsQuery(), filter, MAX_RESULT);
if (docs.totalHits >= MAX_RESULT)
throw new RuntimeException("Max results exceeded: " + MAX_RESULT);
Set<String> result = getResults(searcher, docs);
System.out.println(result);
}
use of org.apache.lucene.spatial.query.SpatialArgs in project janusgraph by JanusGraph.
the class LuceneIndex method convertQuery.
private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
final SearchParams params = new SearchParams();
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
final String key = atom.getKey();
KeyInformation ki = information.get(key);
final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
if (value == null && janusgraphPredicate == Cmp.NOT_EQUAL) {
// some fields like Integer omit norms but have docValues
params.addQuery(new DocValuesFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
// some fields like Text have no docValue but have norms
params.addQuery(new NormsFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
} else if (value instanceof Number) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
} else if (value instanceof String) {
if (janusgraphPredicate == Cmp.LESS_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
} else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
} else if (janusgraphPredicate == Cmp.GREATER_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
} else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
} else {
final Mapping map = Mapping.getMapping(ki);
final String stringFieldKey;
if (Mapping.getMapping(ki) == Mapping.TEXTSTRING) {
stringFieldKey = getDualFieldName(key, ki).orElse(key);
} else {
stringFieldKey = key;
}
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
if (janusgraphPredicate == Text.CONTAINS) {
tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.PREFIX) {
params.addQuery(new PrefixQuery(new Term(stringFieldKey, (String) value)));
} else if (janusgraphPredicate == Text.REGEX) {
final RegexpQuery rq = new RegexpQuery(new Term(stringFieldKey, (String) value));
params.addQuery(rq);
} else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
// This is terrible -- there is probably a better way
// putting this to lowercase because Text search is supposed to be case insensitive
final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
params.addQuery(rq);
} else if (janusgraphPredicate == Cmp.EQUAL || janusgraphPredicate == Cmp.NOT_EQUAL) {
tokenize(params, map, delegatingAnalyzer, (String) value, stringFieldKey, janusgraphPredicate);
} else if (janusgraphPredicate == Text.FUZZY) {
params.addQuery(new FuzzyQuery(new Term(stringFieldKey, (String) value), Text.getMaxEditDistance((String) value)));
} else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
value = ((String) value).toLowerCase();
final Builder b = new BooleanQuery.Builder();
for (final String term : Text.tokenize((String) value)) {
b.add(new FuzzyQuery(new Term(key, term), Text.getMaxEditDistance(term)), BooleanClause.Occur.MUST);
}
params.addQuery(b.build());
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
}
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: %s", janusgraphPredicate);
final Shape shape = ((Geoshape) value).getShape();
final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
final SpatialArgs args = new SpatialArgs(spatialOp, shape);
params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
} else if (value instanceof Date) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
} else if (value instanceof Instant) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
} else if (value instanceof Boolean) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: %s", janusgraphPredicate);
final int intValue;
switch((Cmp) janusgraphPredicate) {
case EQUAL:
intValue = ((Boolean) value) ? 1 : 0;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
case NOT_EQUAL:
intValue = ((Boolean) value) ? 0 : 1;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: %s", janusgraphPredicate);
if (janusgraphPredicate == Cmp.EQUAL) {
params.addQuery(new TermQuery(new Term(key, value.toString())));
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else {
throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
}
} else {
throw new IllegalArgumentException("Unsupported type: " + value);
}
} else if (condition instanceof Not) {
final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
} else if (condition instanceof And) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.MUST);
}
} else if (condition instanceof Or) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.SHOULD);
}
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
return params;
}
use of org.apache.lucene.spatial.query.SpatialArgs in project elasticsearch by elastic.
the class GeoFilterIT method testRelationSupport.
protected static boolean testRelationSupport(SpatialOperation relation) {
if (relation == SpatialOperation.IsDisjointTo) {
// disjoint works in terms of intersection
relation = SpatialOperation.Intersects;
}
try {
GeohashPrefixTree tree = new GeohashPrefixTree(SpatialContext.GEO, 3);
RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(tree, "area");
Shape shape = SpatialContext.GEO.makePoint(0, 0);
SpatialArgs args = new SpatialArgs(relation, shape);
strategy.makeQuery(args);
return true;
} catch (UnsupportedSpatialOperation e) {
final SpatialOperation finalRelation = relation;
ESLoggerFactory.getLogger(GeoFilterIT.class.getName()).info((Supplier<?>) () -> new ParameterizedMessage("Unsupported spatial operation {}", finalRelation), e);
return false;
}
}
use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.
the class TestBBoxStrategy method testAlongDatelineOppositeSign.
/** See https://github.com/spatial4j/spatial4j/issues/85 */
@Test
public void testAlongDatelineOppositeSign() throws IOException {
// Due to Spatial4j bug #85, we can't simply do:
// testOperation(indexedShape,
// SpatialOperation.IsWithin,
// queryShape, true);
//both on dateline but expressed using opposite signs
setupGeo();
final Rectangle indexedShape = ctx.makeRectangle(180, 180, -10, 10);
final Rectangle queryShape = ctx.makeRectangle(-180, -180, -20, 20);
final SpatialOperation operation = SpatialOperation.IsWithin;
//yes it is within
final boolean match = true;
//the rest is super.testOperation without leading assert:
adoc("0", indexedShape);
commit();
Query query = strategy.makeQuery(new SpatialArgs(operation, queryShape));
SearchResults got = executeQuery(query, 1);
assert got.numFound <= 1 : "unclean test env";
if ((got.numFound == 1) != match)
fail(operation + " I:" + indexedShape + " Q:" + queryShape);
//clean up after ourselves
deleteAll();
}
Aggregations