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));
}
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);
}
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));
}
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));
}
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));
}
Aggregations