Adding utility method to SpanTermQueryBuilderTest that outputs certain amount of queries with same fieldname

This commit is contained in:
Christoph Büscher 2015-07-22 15:34:05 +02:00
parent afe4fb4a27
commit 8e20536f0f
7 changed files with 36 additions and 45 deletions

View File

@ -37,11 +37,8 @@ public class SpanContainingQueryBuilderTest extends BaseQueryTestCase<SpanContai
@Override
protected SpanContainingQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder bigQuery = new SpanTermQueryBuilderTest().createTestQueryBuilder();
// we need same field name and value type as bigQuery for little query
String fieldName = bigQuery.fieldName();
SpanTermQueryBuilder littleQuery = new SpanTermQueryBuilder(fieldName, randomValueForField(fieldName));
return new SpanContainingQueryBuilder(bigQuery, littleQuery);
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(2);
return new SpanContainingQueryBuilder(spanTermQueries[0], spanTermQueries[1]);
}
@Test

View File

@ -36,8 +36,8 @@ public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryB
@Override
protected SpanFirstQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder innerQueryBuilder = new SpanTermQueryBuilderTest().createTestQueryBuilder();
return new SpanFirstQueryBuilder(innerQueryBuilder, randomIntBetween(0, 1000));
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(1);
return new SpanFirstQueryBuilder(spanTermQueries[0], randomIntBetween(0, 1000));
}
@Test

View File

@ -45,12 +45,9 @@ public class SpanNearQueryBuilderTest extends BaseQueryTestCase<SpanNearQueryBui
@Override
protected SpanNearQueryBuilder doCreateTestQueryBuilder() {
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(randomIntBetween(-10, 10));
int clauses = randomIntBetween(1, 6);
// we use one random SpanTermQueryBuilder to determine same field name for subsequent clauses
String fieldName = new SpanTermQueryBuilderTest().createTestQueryBuilder().fieldName();
for (int i = 0; i < clauses; i++) {
// we need same field name in all clauses, so we only randomize value
queryBuilder.clause(new SpanTermQueryBuilder(fieldName, randomValueForField(fieldName)));
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(randomIntBetween(1, 6));
for (SpanTermQueryBuilder clause : spanTermQueries) {
queryBuilder.clause(clause);
}
queryBuilder.inOrder(randomBoolean());
queryBuilder.collectPayloads(randomBoolean());

View File

@ -45,19 +45,8 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
@Override
protected SpanNotQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder includeQuery = new SpanTermQueryBuilderTest().createTestQueryBuilder();
// we need same field name and value type as bigQuery for little query
String fieldName = includeQuery.fieldName();
Object value;
switch (fieldName) {
case BOOLEAN_FIELD_NAME: value = randomBoolean(); break;
case INT_FIELD_NAME: value = randomInt(); break;
case DOUBLE_FIELD_NAME: value = randomDouble(); break;
case STRING_FIELD_NAME: value = randomAsciiOfLengthBetween(1, 10); break;
default : value = randomAsciiOfLengthBetween(1, 10);
}
SpanTermQueryBuilder excludeQuery = new SpanTermQueryBuilder(fieldName, value);
SpanNotQueryBuilder queryBuilder = new SpanNotQueryBuilder(includeQuery, excludeQuery);
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(2);
SpanNotQueryBuilder queryBuilder = new SpanNotQueryBuilder(spanTermQueries[0], spanTermQueries[1]);
if (randomBoolean()) {
// also test negative values, they should implicitly be changed to 0
queryBuilder.dist(randomIntBetween(-2, 10));

View File

@ -44,12 +44,9 @@ public class SpanOrQueryBuilderTest extends BaseQueryTestCase<SpanOrQueryBuilder
@Override
protected SpanOrQueryBuilder doCreateTestQueryBuilder() {
SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder();
int clauses = randomIntBetween(1, 6);
// we use one random SpanTermQueryBuilder to determine same field name for subsequent clauses
String fieldName = new SpanTermQueryBuilderTest().createTestQueryBuilder().fieldName();
for (int i = 0; i < clauses; i++) {
// we need same field name in all clauses, so we only randomize value
queryBuilder.clause(new SpanTermQueryBuilder(fieldName, randomValueForField(fieldName)));
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(randomIntBetween(1, 6));
for (SpanTermQueryBuilder clause : spanTermQueries) {
queryBuilder.clause(clause);
}
return queryBuilder;
}

View File

@ -34,4 +34,26 @@ public class SpanTermQueryBuilderTest extends BaseTermQueryTestCase<SpanTermQuer
protected Query createLuceneTermQuery(Term term) {
return new SpanTermQuery(term);
}
/**
* @param amount the number of clauses that will be returned
* @return an array of random {@link SpanTermQueryBuilder} with same field name
*/
public SpanTermQueryBuilder[] createSpanTermQueryBuilders(int amount) {
SpanTermQueryBuilder[] clauses = new SpanTermQueryBuilder[amount];
SpanTermQueryBuilder first = createTestQueryBuilder();
clauses[0] = first;
for (int i = 1; i < amount; i++) {
// we need same field name in all clauses, so we only randomize value
SpanTermQueryBuilder spanTermQuery = new SpanTermQueryBuilder(first.fieldName(), randomValueForField(first.fieldName()));
if (randomBoolean()) {
spanTermQuery.boost(2.0f / randomIntBetween(1, 20));
}
if (randomBoolean()) {
spanTermQuery.queryName(randomAsciiOfLengthBetween(1, 10));
}
clauses[i] = spanTermQuery;
}
return clauses;
}
}

View File

@ -37,19 +37,8 @@ public class SpanWithinQueryBuilderTest extends BaseQueryTestCase<SpanWithinQuer
@Override
protected SpanWithinQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder bigQuery = new SpanTermQueryBuilderTest().createTestQueryBuilder();
// we need same field name and value type as bigQuery for little query
String fieldName = bigQuery.fieldName();
Object littleValue;
switch (fieldName) {
case BOOLEAN_FIELD_NAME: littleValue = randomBoolean(); break;
case INT_FIELD_NAME: littleValue = randomInt(); break;
case DOUBLE_FIELD_NAME: littleValue = randomDouble(); break;
case STRING_FIELD_NAME: littleValue = randomAsciiOfLengthBetween(1, 10); break;
default : littleValue = randomAsciiOfLengthBetween(1, 10);
}
SpanTermQueryBuilder littleQuery = new SpanTermQueryBuilder(fieldName, littleValue);
return new SpanWithinQueryBuilder(bigQuery, littleQuery);
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(2);
return new SpanWithinQueryBuilder(spanTermQueries[0], spanTermQueries[1]);
}
@Test