Search in sources :

Example 1 with BooleanFilter

use of org.apache.lucene.queries.BooleanFilter 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 2 with BooleanFilter

use of org.apache.lucene.queries.BooleanFilter in project titan by thinkaurelius.

the class LuceneIndex method numericFilter.

private static final Filter numericFilter(String key, Cmp relation, Number value) {
    switch(relation) {
        case EQUAL:
            return (value instanceof Long || value instanceof Integer) ? NumericRangeFilter.newLongRange(key, value.longValue(), value.longValue(), true, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), value.doubleValue(), true, true);
        case NOT_EQUAL:
            BooleanFilter q = new BooleanFilter();
            if (value instanceof Long || value instanceof Integer) {
                q.add(NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, false), BooleanClause.Occur.SHOULD);
                q.add(NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, false, true), BooleanClause.Occur.SHOULD);
            } else {
                q.add(NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, false), BooleanClause.Occur.SHOULD);
                q.add(NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, false, true), BooleanClause.Occur.SHOULD);
            }
            return q;
        case LESS_THAN:
            return (value instanceof Long || value instanceof Integer) ? NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, false) : NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, false);
        case LESS_THAN_EQUAL:
            return (value instanceof Long || value instanceof Integer) ? NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, true) : NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, true);
        case GREATER_THAN:
            return (value instanceof Long || value instanceof Integer) ? NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, false, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, false, true);
        case GREATER_THAN_EQUAL:
            return (value instanceof Long || value instanceof Integer) ? NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, true, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, true, true);
        default:
            throw new IllegalArgumentException("Unexpected relation: " + relation);
    }
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter)

Example 3 with BooleanFilter

use of org.apache.lucene.queries.BooleanFilter 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)

Aggregations

BooleanFilter (org.apache.lucene.queries.BooleanFilter)3 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)2 Shape (com.spatial4j.core.shape.Shape)1 Mapping (com.thinkaurelius.titan.core.Mapping)1 TitanPredicate (com.thinkaurelius.titan.graphdb.query.TitanPredicate)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 TermsFilter (org.apache.lucene.queries.TermsFilter)1 Directory (org.apache.lucene.store.Directory)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 Test (org.junit.Test)1