Search in sources :

Example 21 with SpatialArgs

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);
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(com.spatial4j.core.shape.Shape) TermsFilter(org.apache.lucene.queries.TermsFilter) Mapping(com.thinkaurelius.titan.core.Mapping) TitanPredicate(com.thinkaurelius.titan.graphdb.query.TitanPredicate)

Example 22 with SpatialArgs

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);
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) Test(org.junit.Test)

Example 23 with SpatialArgs

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;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Shape(org.locationtech.spatial4j.shape.Shape) Or(org.janusgraph.graphdb.query.condition.Or) Builder(org.apache.lucene.search.BooleanQuery.Builder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Mapping(org.janusgraph.core.schema.Mapping) NormsFieldExistsQuery(org.apache.lucene.search.NormsFieldExistsQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) RegexpQuery(org.apache.lucene.search.RegexpQuery) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) UUID(java.util.UUID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) TermQuery(org.apache.lucene.search.TermQuery) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Cmp(org.janusgraph.core.attribute.Cmp) Instant(java.time.Instant) Geoshape(org.janusgraph.core.attribute.Geoshape) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Date(java.util.Date) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) Geo(org.janusgraph.core.attribute.Geo) Not(org.janusgraph.graphdb.query.condition.Not) PrefixQuery(org.apache.lucene.search.PrefixQuery) And(org.janusgraph.graphdb.query.condition.And) FuzzyQuery(org.apache.lucene.search.FuzzyQuery)

Example 24 with SpatialArgs

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;
    }
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 25 with SpatialArgs

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();
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) Rectangle(org.locationtech.spatial4j.shape.Rectangle) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) Test(org.junit.Test)

Aggregations

SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)34 Query (org.apache.lucene.search.Query)16 Shape (org.locationtech.spatial4j.shape.Shape)14 Test (org.junit.Test)12 Point (org.locationtech.spatial4j.shape.Point)10 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)6 Document (org.apache.lucene.document.Document)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 TopDocs (org.apache.lucene.search.TopDocs)4 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)3 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Field (org.apache.lucene.document.Field)2 TextField (org.apache.lucene.document.TextField)2 IndexReader (org.apache.lucene.index.IndexReader)2 Term (org.apache.lucene.index.Term)2 BooleanFilter (org.apache.lucene.queries.BooleanFilter)2