use of org.apache.lucene.search.TermQuery 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.TermQuery in project elasticsearch by elastic.
the class SimpleQueryParserTests method testQuoteFieldSuffix.
public void testQuoteFieldSuffix() {
SimpleQueryParser.Settings sqpSettings = new SimpleQueryParser.Settings();
sqpSettings.quoteFieldSuffix(".quote");
Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_INDEX_UUID, "some_uuid").put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
IndexMetaData indexState = IndexMetaData.builder("index").settings(indexSettings).build();
IndexSettings settings = new IndexSettings(indexState, Settings.EMPTY);
QueryShardContext mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {
@Override
public MappedFieldType fieldMapper(String name) {
return new MockFieldMapper.FakeFieldType();
}
};
SimpleQueryParser parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
assertEquals(new TermQuery(new Term("foo.quote", "bar")), parser.parse("\"bar\""));
// Now check what happens if foo.quote does not exist
mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {
@Override
public MappedFieldType fieldMapper(String name) {
if (name.equals("foo.quote")) {
return null;
}
return new MockFieldMapper.FakeFieldType();
}
};
parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("\"bar\""));
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class MultiMatchQueryTests method testCrossFieldMultiMatchQuery.
public void testCrossFieldMultiMatchQuery() throws IOException {
QueryShardContext queryShardContext = indexService.newQueryShardContext(randomInt(20), null, () -> {
throw new UnsupportedOperationException();
});
queryShardContext.setAllowUnmappedFields(true);
Query parsedQuery = multiMatchQuery("banon").field("name.first", 2).field("name.last", 3).field("foobar").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
Query rewrittenQuery = searcher.searcher().rewrite(parsedQuery);
BooleanQuery.Builder expected = new BooleanQuery.Builder();
expected.add(new TermQuery(new Term("foobar", "banon")), BooleanClause.Occur.SHOULD);
Query tq1 = new BoostQuery(new TermQuery(new Term("name.first", "banon")), 2);
Query tq2 = new BoostQuery(new TermQuery(new Term("name.last", "banon")), 3);
expected.add(new DisjunctionMaxQuery(Arrays.<Query>asList(tq1, tq2), 0f), BooleanClause.Occur.SHOULD);
assertEquals(expected.build(), rewrittenQuery);
}
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class FunctionScoreEquivalenceTests method testMinScoreAllExcluded.
public void testMinScoreAllExcluded() throws Exception {
Term term = randomTerm();
Query query = new TermQuery(term);
FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, Float.POSITIVE_INFINITY, null, Float.POSITIVE_INFINITY);
assertSameScores(new MatchNoDocsQuery(), fsq);
FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, CombineFunction.MULTIPLY);
assertSameScores(new MatchNoDocsQuery(), ffsq);
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class SpanTermQueryBuilderTests method doAssertLuceneQuery.
@Override
protected void doAssertLuceneQuery(SpanTermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
assertThat(query, instanceOf(SpanTermQuery.class));
SpanTermQuery spanTermQuery = (SpanTermQuery) query;
assertThat(spanTermQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
MappedFieldType mapper = context.getQueryShardContext().fieldMapper(queryBuilder.fieldName());
if (mapper != null) {
Term term = ((TermQuery) mapper.termQuery(queryBuilder.value(), null)).getTerm();
assertThat(spanTermQuery.getTerm(), equalTo(term));
} else {
assertThat(spanTermQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
}
}
Aggregations