use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class FunctionScoreTests method testExplanationAndScoreEqualsEvenIfNoFunctionMatches.
public void testExplanationAndScoreEqualsEvenIfNoFunctionMatches() throws IOException {
IndexSearcher localSearcher = newSearcher(reader);
ScoreMode scoreMode = randomFrom(new ScoreMode[] { ScoreMode.SUM, ScoreMode.AVG, ScoreMode.FIRST, ScoreMode.MIN, ScoreMode.MAX, ScoreMode.MULTIPLY });
CombineFunction combineFunction = randomFrom(new CombineFunction[] { CombineFunction.SUM, CombineFunction.AVG, CombineFunction.MIN, CombineFunction.MAX, CombineFunction.MULTIPLY, CombineFunction.REPLACE });
// check for document that has no macthing function
FiltersFunctionScoreQuery query = new FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "2")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
TopDocs searchResult = localSearcher.search(query, 1);
Explanation explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
// check for document that has a matching function
query = new FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "1")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
searchResult = localSearcher.search(query, 1);
explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
}
use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.
the class NestedHelperTests method testDisjunction.
public void testDisjunction() {
BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD).build();
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested1.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested1.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertFalse(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested2.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested2"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested3.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested3"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested1.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested2"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested3"));
}
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()));
}
Aggregations