adapt to upstream changes
This commit is contained in:
parent
56b3db6ba3
commit
2e68738429
|
@ -1,124 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class AndQueryBuilderTests extends AbstractQueryTestCase<AndQueryBuilder> {
|
||||
|
||||
/**
|
||||
* @return a AndQueryBuilder with random limit between 0 and 20
|
||||
*/
|
||||
@Override
|
||||
protected AndQueryBuilder doCreateTestQueryBuilder() {
|
||||
AndQueryBuilder query = new AndQueryBuilder();
|
||||
int subQueries = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < subQueries; i++ ) {
|
||||
query.add(RandomQueryBuilder.createQuery(random()));
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(AndQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
||||
if (queryBuilder.innerQueries().isEmpty()) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
List<Query> clauses = new ArrayList<>();
|
||||
for (QueryBuilder innerFilter : queryBuilder.innerQueries()) {
|
||||
Query clause = innerFilter.toQuery(context);
|
||||
if (clause != null) {
|
||||
clauses.add(clause);
|
||||
}
|
||||
}
|
||||
if (clauses.isEmpty()) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
assertThat(query, instanceOf(BooleanQuery.class));
|
||||
BooleanQuery booleanQuery = (BooleanQuery) query;
|
||||
assertThat(booleanQuery.clauses().size(), equalTo(clauses.size()));
|
||||
Iterator<Query> queryIterator = clauses.iterator();
|
||||
for (BooleanClause booleanClause : booleanQuery) {
|
||||
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.MUST));
|
||||
assertThat(booleanClause.getQuery(), equalTo(queryIterator.next()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test corner case where no inner queries exist
|
||||
*/
|
||||
@Test
|
||||
public void testNoInnerQueries() throws QueryShardException, IOException {
|
||||
AndQueryBuilder andQuery = new AndQueryBuilder();
|
||||
assertNull(andQuery.toQuery(createShardContext()));
|
||||
}
|
||||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testMissingFiltersSection() throws IOException {
|
||||
parseQuery("{ \"and\" : {}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate() {
|
||||
AndQueryBuilder andQuery = new AndQueryBuilder();
|
||||
int iters = randomIntBetween(0, 5);
|
||||
int totalExpectedErrors = 0;
|
||||
for (int i = 0; i < iters; i++) {
|
||||
if (randomBoolean()) {
|
||||
if (randomBoolean()) {
|
||||
andQuery.add(RandomQueryBuilder.createInvalidQuery(random()));
|
||||
} else {
|
||||
andQuery.add(null);
|
||||
}
|
||||
totalExpectedErrors++;
|
||||
} else {
|
||||
andQuery.add(RandomQueryBuilder.createQuery(random()));
|
||||
}
|
||||
}
|
||||
assertValidate(andQuery, totalExpectedErrors);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, AndQueryBuilder> getAlternateVersions() {
|
||||
Map<String, AndQueryBuilder> alternateVersions = new HashMap<>();
|
||||
QueryBuilder innerQuery = createTestQueryBuilder().innerQueries().get(0);
|
||||
AndQueryBuilder expectedQuery = new AndQueryBuilder(innerQuery);
|
||||
String contentString = "{ \"and\" : [ " + innerQuery + "] }";
|
||||
alternateVersions.put(contentString, expectedQuery);
|
||||
return alternateVersions;
|
||||
}
|
||||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testParsingExceptionNonFiltersElementArray() throws IOException {
|
||||
String queryString = "{ \"and\" : { \"whatever_filters\" : [ { \"match_all\" : {} } ] } }";
|
||||
parseQuery(queryString);
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class FQueryFilterBuilderTests extends AbstractQueryTestCase<FQueryFilterBuilder> {
|
||||
|
||||
/**
|
||||
* @return a FQueryFilterBuilder with random inner query
|
||||
*/
|
||||
@Override
|
||||
protected FQueryFilterBuilder doCreateTestQueryBuilder() {
|
||||
QueryBuilder innerQuery = RandomQueryBuilder.createQuery(random());
|
||||
return new FQueryFilterBuilder(innerQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(FQueryFilterBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
||||
Query innerQuery = queryBuilder.innerQuery().toQuery(context);
|
||||
if (innerQuery == null) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
assertThat(query, instanceOf(ConstantScoreQuery.class));
|
||||
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
|
||||
assertThat(constantScoreQuery.getQuery(), equalTo(innerQuery));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test corner case where no inner query exist
|
||||
*/
|
||||
@Test
|
||||
public void testNoInnerQuery() throws QueryParsingException, IOException {
|
||||
FQueryFilterBuilder queryFilterQuery = new FQueryFilterBuilder(EmptyQueryBuilder.PROTOTYPE);
|
||||
assertNull(queryFilterQuery.toQuery(createShardContext()));
|
||||
}
|
||||
|
||||
/**
|
||||
* test wrapping an inner filter that returns null also returns <tt>null</null> to pass on upwards
|
||||
*/
|
||||
@Test
|
||||
public void testInnerQueryReturnsNull() throws IOException {
|
||||
// create inner filter
|
||||
String queryString = "{ \"constant_score\" : { \"filter\" : {} } }";
|
||||
QueryBuilder innerQuery = parseQuery(queryString);
|
||||
// check that when wrapping this filter, toQuery() returns null
|
||||
FQueryFilterBuilder queryFilterQuery = new FQueryFilterBuilder(innerQuery);
|
||||
assertNull(queryFilterQuery.toQuery(createShardContext()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate() {
|
||||
QueryBuilder innerQuery = null;
|
||||
int totalExpectedErrors = 0;
|
||||
if (randomBoolean()) {
|
||||
if (randomBoolean()) {
|
||||
innerQuery = RandomQueryBuilder.createInvalidQuery(random());
|
||||
}
|
||||
totalExpectedErrors++;
|
||||
} else {
|
||||
innerQuery = RandomQueryBuilder.createQuery(random());
|
||||
}
|
||||
FQueryFilterBuilder fQueryFilter = new FQueryFilterBuilder(innerQuery);
|
||||
assertValidate(fQueryFilter, totalExpectedErrors);
|
||||
}
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class FilteredQueryBuilderTests extends AbstractQueryTestCase<FilteredQueryBuilder> {
|
||||
|
||||
@Override
|
||||
protected FilteredQueryBuilder doCreateTestQueryBuilder() {
|
||||
QueryBuilder queryBuilder = RandomQueryBuilder.createQuery(random());
|
||||
QueryBuilder filterBuilder = RandomQueryBuilder.createQuery(random());
|
||||
return new FilteredQueryBuilder(queryBuilder, filterBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(FilteredQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
||||
Query innerQuery = queryBuilder.innerQuery().toQuery(context);
|
||||
if (innerQuery == null) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
assertThat(query, instanceOf(BooleanQuery.class));
|
||||
BooleanQuery booleanQuery = (BooleanQuery) query;
|
||||
Query innerFilter = queryBuilder.innerFilter().toQuery(context);
|
||||
if (innerFilter == null) {
|
||||
assertThat(booleanQuery.clauses().size(), equalTo(1));
|
||||
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
|
||||
assertThat(booleanQuery.clauses().get(0).getQuery(), equalTo(innerQuery));
|
||||
} else {
|
||||
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
|
||||
assertThat(booleanQuery.clauses().get(0).getQuery(), equalTo(innerQuery));
|
||||
assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
|
||||
assertThat(booleanQuery.clauses().get(1).getQuery(), equalTo(innerFilter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidation() {
|
||||
QueryBuilder valid = RandomQueryBuilder.createQuery(random());
|
||||
QueryBuilder invalid = RandomQueryBuilder.createInvalidQuery(random());
|
||||
|
||||
// invalid cases
|
||||
FilteredQueryBuilder qb = new FilteredQueryBuilder(invalid);
|
||||
QueryValidationException result = qb.validate();
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.validationErrors().size());
|
||||
|
||||
qb = new FilteredQueryBuilder(valid, invalid);
|
||||
result = qb.validate();
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.validationErrors().size());
|
||||
|
||||
qb = new FilteredQueryBuilder(invalid, valid);
|
||||
result = qb.validate();
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.validationErrors().size());
|
||||
|
||||
qb = new FilteredQueryBuilder(invalid, invalid);
|
||||
result = qb.validate();
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.validationErrors().size());
|
||||
|
||||
// valid cases
|
||||
qb = new FilteredQueryBuilder(valid);
|
||||
assertNull(qb.validate());
|
||||
|
||||
qb = new FilteredQueryBuilder(null);
|
||||
assertNull(qb.validate());
|
||||
|
||||
qb = new FilteredQueryBuilder(null, valid);
|
||||
assertNull(qb.validate());
|
||||
|
||||
qb = new FilteredQueryBuilder(valid, null);
|
||||
assertNull(qb.validate());
|
||||
|
||||
qb = new FilteredQueryBuilder(valid, valid);
|
||||
assertNull(qb.validate());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
|
||||
public class LimitQueryBuilderTests extends AbstractQueryTestCase<LimitQueryBuilder> {
|
||||
|
||||
/**
|
||||
* @return a LimitQueryBuilder with random limit between 0 and 20
|
||||
*/
|
||||
@Override
|
||||
protected LimitQueryBuilder doCreateTestQueryBuilder() {
|
||||
return new LimitQueryBuilder(randomIntBetween(0, 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(LimitQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
||||
assertThat(query, instanceOf(MatchAllDocsQuery.class));
|
||||
}
|
||||
}
|
|
@ -1,120 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class OrQueryBuilderTests extends AbstractQueryTestCase<OrQueryBuilder> {
|
||||
|
||||
/**
|
||||
* @return an OrQueryBuilder with random limit between 0 and 20
|
||||
*/
|
||||
@Override
|
||||
protected OrQueryBuilder doCreateTestQueryBuilder() {
|
||||
OrQueryBuilder query = new OrQueryBuilder();
|
||||
int subQueries = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < subQueries; i++ ) {
|
||||
query.add(RandomQueryBuilder.createQuery(random()));
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(OrQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
||||
if (queryBuilder.innerQueries().isEmpty()) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
List<Query> innerQueries = new ArrayList<>();
|
||||
for (QueryBuilder subQuery : queryBuilder.innerQueries()) {
|
||||
Query innerQuery = subQuery.toQuery(context);
|
||||
// ignore queries that are null
|
||||
if (innerQuery != null) {
|
||||
innerQueries.add(innerQuery);
|
||||
}
|
||||
}
|
||||
if (innerQueries.isEmpty()) {
|
||||
assertThat(query, nullValue());
|
||||
} else {
|
||||
assertThat(query, instanceOf(BooleanQuery.class));
|
||||
BooleanQuery booleanQuery = (BooleanQuery) query;
|
||||
assertThat(booleanQuery.clauses().size(), equalTo(innerQueries.size()));
|
||||
Iterator<Query> queryIterator = innerQueries.iterator();
|
||||
for (BooleanClause booleanClause : booleanQuery) {
|
||||
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
|
||||
assertThat(booleanClause.getQuery(), equalTo(queryIterator.next()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test corner case where no inner queries exist
|
||||
*/
|
||||
@Test
|
||||
public void testNoInnerQueries() throws QueryShardException, IOException {
|
||||
OrQueryBuilder orQuery = new OrQueryBuilder();
|
||||
assertNull(orQuery.toQuery(createShardContext()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, OrQueryBuilder> getAlternateVersions() {
|
||||
Map<String, OrQueryBuilder> alternateVersions = new HashMap<>();
|
||||
QueryBuilder innerQuery = createTestQueryBuilder().innerQueries().get(0);
|
||||
OrQueryBuilder expectedQuery = new OrQueryBuilder(innerQuery);
|
||||
String contentString = "{ \"or\" : [ " + innerQuery + "] }";
|
||||
alternateVersions.put(contentString, expectedQuery);
|
||||
return alternateVersions;
|
||||
}
|
||||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testMissingFiltersSection() throws IOException {
|
||||
String queryString = "{ \"or\" : {}";
|
||||
parseQuery(queryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate() {
|
||||
OrQueryBuilder orQuery = new OrQueryBuilder();
|
||||
int iters = randomIntBetween(0, 5);
|
||||
int totalExpectedErrors = 0;
|
||||
for (int i = 0; i < iters; i++) {
|
||||
if (randomBoolean()) {
|
||||
if (randomBoolean()) {
|
||||
orQuery.add(RandomQueryBuilder.createInvalidQuery(random()));
|
||||
} else {
|
||||
orQuery.add(null);
|
||||
}
|
||||
totalExpectedErrors++;
|
||||
} else {
|
||||
orQuery.add(RandomQueryBuilder.createQuery(random()));
|
||||
}
|
||||
}
|
||||
assertValidate(orQuery, totalExpectedErrors);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue