use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class NestedHelperTests method testNested.
public void testNested() throws IOException {
QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// this time we do not add a filter since the inner query only matches inner docs
expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_parent
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_root
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class NestedHelperTests method testTermQuery.
public void testTermQuery() {
Query termQuery = mapperService.fullName("foo").termQuery("bar", null);
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
termQuery = mapperService.fullName("nested1.foo").termQuery("bar", null);
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
assertFalse(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
termQuery = mapperService.fullName("nested2.foo").termQuery("bar", null);
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
termQuery = mapperService.fullName("nested3.foo").termQuery("bar", null);
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class SpanMultiTermQueryBuilderTests method testUnsupportedInnerQueryType.
/**
* test checks that we throw an {@link UnsupportedOperationException} if the query wrapped
* by {@link SpanMultiTermQueryBuilder} does not generate a lucene {@link MultiTermQuery}.
* This is currently the case for {@link RangeQueryBuilder} when the target field is mapped
* to a date.
*/
public void testUnsupportedInnerQueryType() throws IOException {
MultiTermQueryBuilder query = new MultiTermQueryBuilder() {
@Override
public Query toQuery(QueryShardContext context) throws IOException {
return new TermQuery(new Term("foo", "bar"));
}
@Override
public Query toFilter(QueryShardContext context) throws IOException {
return toQuery(context);
}
@Override
public QueryBuilder queryName(String queryName) {
return this;
}
@Override
public String queryName() {
return "foo";
}
@Override
public float boost() {
return 1f;
}
@Override
public QueryBuilder boost(float boost) {
return this;
}
@Override
public String getName() {
return "foo";
}
@Override
public String getWriteableName() {
return "foo";
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
}
};
SpanMultiTermQueryBuilder spamMultiTermQuery = new SpanMultiTermQueryBuilder(query);
UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> spamMultiTermQuery.toQuery(createShardContext()));
assertThat(e.getMessage(), containsString("unsupported inner query, should be " + MultiTermQuery.class.getName()));
}
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\""));
}
Aggregations