Search in sources :

Example 1 with DocValuesFieldExistsQuery

use of org.apache.lucene.search.DocValuesFieldExistsQuery in project crate by crate.

the class CommonQueryBuilderTest method testIsNullOnGeoPoint.

@Test
public void testIsNullOnGeoPoint() throws Exception {
    Query query = convert("point is null");
    assertThat(query.toString(), is("+*:* -ConstantScore(DocValuesFieldExistsQuery [field=point])"));
}
Also used : CrateRegexQuery(io.crate.lucene.match.CrateRegexQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Test(org.junit.Test)

Example 2 with DocValuesFieldExistsQuery

use of org.apache.lucene.search.DocValuesFieldExistsQuery in project crate by crate.

the class CommonQueryBuilderTest method testIsNullOnObjectArray.

@Test
public void testIsNullOnObjectArray() throws Exception {
    Query isNull = convert("o_array IS NULL");
    assertThat(isNull.toString(), is("+*:* -ConstantScore(ConstantScore(DocValuesFieldExistsQuery [field=o_array.xs]))"));
    Query isNotNull = convert("o_array IS NOT NULL");
    assertThat(isNotNull.toString(), is("ConstantScore(ConstantScore(DocValuesFieldExistsQuery [field=o_array.xs]))"));
}
Also used : CrateRegexQuery(io.crate.lucene.match.CrateRegexQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Test(org.junit.Test)

Example 3 with DocValuesFieldExistsQuery

use of org.apache.lucene.search.DocValuesFieldExistsQuery in project Anserini by castorini.

the class AxiomReranker method buildInternalDocidsCache.

/**
 * If the result is deterministic we can cache all the docids. All queries can share this
 * cache.
 */
private ScoreDoc[] buildInternalDocidsCache(String indexPath, boolean searchTweets) throws IOException {
    Path index = Paths.get(indexPath);
    if (!Files.exists(index) || !Files.isDirectory(index) || !Files.isReadable(index)) {
        throw new IllegalArgumentException(indexPath + " does not exist or is not a directory.");
    }
    IndexReader reader = DirectoryReader.open(FSDirectory.open(index));
    IndexSearcher searcher = new IndexSearcher(reader);
    if (searchTweets) {
        return searcher.search(new DocValuesFieldExistsQuery(TweetGenerator.TweetField.ID_LONG.name), reader.maxDoc(), BREAK_SCORE_TIES_BY_TWEETID).scoreDocs;
    }
    return searcher.search(new DocValuesFieldExistsQuery(IndexArgs.ID), reader.maxDoc(), BREAK_SCORE_TIES_BY_DOCID).scoreDocs;
}
Also used : Path(java.nio.file.Path) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery)

Example 4 with DocValuesFieldExistsQuery

use of org.apache.lucene.search.DocValuesFieldExistsQuery 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 5 with DocValuesFieldExistsQuery

use of org.apache.lucene.search.DocValuesFieldExistsQuery in project Anserini by castorini.

the class DocumentFieldContext method getAllDocID.

public List<Integer> getAllDocID() {
    Query q = new DocValuesFieldExistsQuery(fieldName);
    List<Integer> DocIDs = new ArrayList<>();
    try {
        ScoreDoc[] scoreDocs = searcher.search(q, reader.maxDoc()).scoreDocs;
        for (int i = 0; i < scoreDocs.length; i++) {
            DocIDs.add(scoreDocs[i].doc);
        }
    } catch (IOException e) {
    // e.printStackTrace();
    }
    return DocIDs;
}
Also used : Query(org.apache.lucene.search.Query) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) ArrayList(java.util.ArrayList) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

DocValuesFieldExistsQuery (org.apache.lucene.search.DocValuesFieldExistsQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Query (org.apache.lucene.search.Query)3 RegexpQuery (org.apache.lucene.search.RegexpQuery)3 TermQuery (org.apache.lucene.search.TermQuery)3 CrateRegexQuery (io.crate.lucene.match.CrateRegexQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 PointInSetQuery (org.apache.lucene.search.PointInSetQuery)2 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)2 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2 IntersectsPrefixTreeQuery (org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1