use of org.apache.lucene.search.PrefixQuery in project elasticsearch by elastic.
the class QueryStringQueryBuilderTests method testToQueryWildcarQuery.
public void testToQueryWildcarQuery() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
QueryParserSettings settings = new QueryParserSettings("first foo-bar-foobar* last");
settings.defaultField(STRING_FIELD_NAME);
settings.fieldsAndWeights(Collections.emptyMap());
settings.analyzeWildcard(true);
settings.fuzziness(Fuzziness.AUTO);
settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
settings.defaultOperator(op.toQueryParserOperator());
queryParser.reset(settings);
Query query = queryParser.parse("first foo-bar-foobar* last");
Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "foo")), defaultOp)).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "bar")), defaultOp)).add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")), defaultOp)).build(), defaultOp).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "last")), defaultOp)).build();
assertThat(query, Matchers.equalTo(expectedQuery));
}
}
use of org.apache.lucene.search.PrefixQuery in project elasticsearch by elastic.
the class SimpleQueryParserTests method testAnalyzeWildcard.
public void testAnalyzeWildcard() {
SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
settings.analyzeWildcard(true);
Map<String, Float> weights = new HashMap<>();
weights.put("field1", 1.0f);
SimpleQueryParser parser = new MockSimpleQueryParser(new StandardAnalyzer(), weights, -1, settings);
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
parser.setDefaultOperator(defaultOp);
Query query = parser.parse("first foo-bar-foobar* last");
Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term("field1", "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term("field1", "foo")), defaultOp)).add(new BooleanClause(new TermQuery(new Term("field1", "bar")), defaultOp)).add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), defaultOp)).build(), defaultOp).add(new BooleanClause(new TermQuery(new Term("field1", "last")), defaultOp)).build();
assertThat(query, equalTo(expectedQuery));
}
}
use of org.apache.lucene.search.PrefixQuery in project elasticsearch by elastic.
the class SimpleQueryParserTests method testAnalyzerWildcardWithSynonyms.
public void testAnalyzerWildcardWithSynonyms() {
SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
settings.analyzeWildcard(true);
Map<String, Float> weights = new HashMap<>();
weights.put("field1", 1.0f);
SimpleQueryParser parser = new MockSimpleQueryParser(new MockRepeatAnalyzer(), weights, -1, settings);
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
parser.setDefaultOperator(defaultOp);
Query query = parser.parse("first foo-bar-foobar* last");
Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term("field1", "first"), new Term("field1", "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term("field1", "foo"), new Term("field1", "foo")), defaultOp)).add(new BooleanClause(new SynonymQuery(new Term("field1", "bar"), new Term("field1", "bar")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), BooleanClause.Occur.SHOULD)).add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), BooleanClause.Occur.SHOULD)).setDisableCoord(true).build(), defaultOp).build(), defaultOp).add(new BooleanClause(new SynonymQuery(new Term("field1", "last"), new Term("field1", "last")), defaultOp)).build();
assertThat(query, equalTo(expectedQuery));
}
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestSpanMultiTermQueryWrapper method testNoSuchMultiTermsInSpanFirst.
public void testNoSuchMultiTermsInSpanFirst() throws Exception {
//this hasn't been a problem
FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestSpanMultiTermQueryWrapper method testWrappedQueryIsNotModified.
@Test
public void testWrappedQueryIsNotModified() {
final PrefixQuery pq = new PrefixQuery(new Term("field", "test"));
int pqHash = pq.hashCode();
SpanMultiTermQueryWrapper<PrefixQuery> wrapper = new SpanMultiTermQueryWrapper<>(pq);
assertEquals(pqHash, pq.hashCode());
wrapper.setRewriteMethod(new SpanMultiTermQueryWrapper.SpanRewriteMethod() {
@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
return null;
}
});
assertEquals(pqHash, pq.hashCode());
}
Aggregations