Search in sources :

Example 36 with ConstantScoreQuery

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

the class LuceneOrderedDocCollectorTest method testSearchWithScores.

@Test
public void testSearchWithScores() throws Exception {
    IndexWriter w = new IndexWriter(new ByteBuffersDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    FieldType fieldType = KeywordFieldMapper.Defaults.FIELD_TYPE;
    for (int i = 0; i < 3; i++) {
        addDoc(w, "x", fieldType, "Arthur");
    }
    // not "Arthur" to lower score
    addDoc(w, "x", fieldType, "Arthur");
    w.commit();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(w, true, true));
    List<LuceneCollectorExpression<?>> columnReferences = Collections.singletonList(new ScoreCollectorExpression());
    Query query = new ConstantScoreQuery(new TermQuery(new Term("x", new BytesRef("Arthur"))));
    LuceneOrderedDocCollector collector = collector(searcher, columnReferences, query, null, true);
    KeyIterable<ShardId, Row> result = collector.collect();
    assertThat(StreamSupport.stream(result.spliterator(), false).count(), is(2L));
    Iterator<Row> values = result.iterator();
    assertThat(values.next().get(0), Matchers.is(1.0F));
    assertThat(values.next().get(0), Matchers.is(1.0F));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) FieldType(org.apache.lucene.document.FieldType) ShardId(org.elasticsearch.index.shard.ShardId) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Row(io.crate.data.Row) ScoreCollectorExpression(io.crate.expression.reference.doc.lucene.ScoreCollectorExpression) LuceneCollectorExpression(io.crate.expression.reference.doc.lucene.LuceneCollectorExpression) BytesRef(org.apache.lucene.util.BytesRef) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) Test(org.junit.Test)

Example 37 with ConstantScoreQuery

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

the class MatchPredicate method geoMatch.

private Query geoMatch(LuceneQueryBuilder.Context context, List<Symbol> arguments, Object queryTerm) {
    Map fields = (Map) ((Literal) arguments.get(0)).value();
    String fieldName = (String) fields.keySet().iterator().next();
    MappedFieldType fieldType = context.queryShardContext().getMapperService().fullName(fieldName);
    GeoShapeFieldMapper.GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    String matchType = (String) ((Input) arguments.get(2)).value();
    @SuppressWarnings("unchecked") Shape shape = GeoJSONUtils.map2Shape((Map<String, Object>) queryTerm);
    ShapeRelation relation = ShapeRelation.getRelationByName(matchType);
    assert relation != null : "invalid matchType: " + matchType;
    PrefixTreeStrategy prefixTreeStrategy = geoShapeFieldType.defaultStrategy();
    if (relation == ShapeRelation.DISJOINT) {
        /**
         * See {@link org.elasticsearch.index.query.GeoShapeQueryParser}:
         */
        // this strategy doesn't support disjoint anymore: but it did before, including creating lucene fieldcache (!)
        // in this case, execute disjoint as exists && !intersects
        BooleanQuery.Builder bool = new BooleanQuery.Builder();
        Query exists = new TermRangeQuery(fieldName, null, null, true, true);
        Query intersects = prefixTreeStrategy.makeQuery(getArgs(shape, ShapeRelation.INTERSECTS));
        bool.add(exists, BooleanClause.Occur.MUST);
        bool.add(intersects, BooleanClause.Occur.MUST_NOT);
        return new ConstantScoreQuery(bool.build());
    }
    SpatialArgs spatialArgs = getArgs(shape, relation);
    return prefixTreeStrategy.makeQuery(spatialArgs);
}
Also used : ShapeRelation(org.elasticsearch.common.geo.ShapeRelation) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) FunctionToQuery(io.crate.lucene.FunctionToQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) LuceneQueryBuilder(io.crate.lucene.LuceneQueryBuilder) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Map(java.util.Map) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy)

Example 38 with ConstantScoreQuery

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

the class CommonQueryBuilderTest method testGeoShapeMatchDisJoint.

@Test
public void testGeoShapeMatchDisJoint() throws Exception {
    Query query = convert("match(shape, 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))') using disjoint");
    assertThat(query, instanceOf(ConstantScoreQuery.class));
    Query booleanQuery = ((ConstantScoreQuery) query).getQuery();
    assertThat(booleanQuery, instanceOf(BooleanQuery.class));
    BooleanClause existsClause = ((BooleanQuery) booleanQuery).clauses().get(0);
    BooleanClause intersectsClause = ((BooleanQuery) booleanQuery).clauses().get(1);
    assertThat(existsClause.getQuery(), instanceOf(TermRangeQuery.class));
    assertThat(intersectsClause.getQuery(), instanceOf(IntersectsPrefixTreeQuery.class));
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) Test(org.junit.Test)

Example 39 with ConstantScoreQuery

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

the class CommonQueryBuilderTest method testRegexQueryFast.

/**
 * Make sure we still sport the fast Lucene regular
 * expression engine when not using PCRE features.
 */
@Test
public void testRegexQueryFast() throws Exception {
    Query query = convert("name ~ '[a-z]'");
    assertThat(query, instanceOf(ConstantScoreQuery.class));
    ConstantScoreQuery scoreQuery = (ConstantScoreQuery) query;
    assertThat(scoreQuery.getQuery(), instanceOf(RegexpQuery.class));
}
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) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) Test(org.junit.Test)

Example 40 with ConstantScoreQuery

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

the class CommonQueryBuilderTest method testWhereRefEqRef.

@Test
public void testWhereRefEqRef() throws Exception {
    // 3vl
    Query query = convert("name = name");
    assertThat(query, instanceOf(ConstantScoreQuery.class));
    ConstantScoreQuery scoreQuery = (ConstantScoreQuery) query;
    assertThat(scoreQuery.getQuery(), instanceOf(DocValuesFieldExistsQuery.class));
    // 2vl
    query = convert("ignore3vl(name = name)");
    assertThat(query, instanceOf(MatchAllDocsQuery.class));
}
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) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Test(org.junit.Test)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)68 BooleanQuery (org.apache.lucene.search.BooleanQuery)43 TermQuery (org.apache.lucene.search.TermQuery)42 Query (org.apache.lucene.search.Query)40 Term (org.apache.lucene.index.Term)30 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Document (org.apache.lucene.document.Document)10 Test (org.junit.Test)10 Sort (org.apache.lucene.search.Sort)9 TopDocs (org.apache.lucene.search.TopDocs)9 StringField (org.apache.lucene.document.StringField)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8 SortField (org.apache.lucene.search.SortField)8