forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#12156 from cbuescher/feature/query-refacto…
…ring-spannear Query refactoring: SpanNearQueryBuilder and Parser
- Loading branch information
Showing
7 changed files
with
251 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/test/java/org/elasticsearch/index/query/SpanNearQueryBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.Query; | ||
import org.apache.lucene.search.spans.SpanNearQuery; | ||
import org.apache.lucene.search.spans.SpanQuery; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class SpanNearQueryBuilderTest extends BaseQueryTestCase<SpanNearQueryBuilder> { | ||
|
||
@Override | ||
protected Query doCreateExpectedQuery(SpanNearQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException { | ||
List<SpanQueryBuilder> clauses = testQueryBuilder.clauses(); | ||
SpanQuery[] spanQueries = new SpanQuery[clauses.size()]; | ||
for (int i = 0; i < clauses.size(); i++) { | ||
Query query = clauses.get(i).toQuery(context); | ||
assert query instanceof SpanQuery; | ||
spanQueries[i] = (SpanQuery) query; | ||
} | ||
return new SpanNearQuery(spanQueries, testQueryBuilder.slop(), testQueryBuilder.inOrder(), testQueryBuilder.collectPayloads()); | ||
|
||
} | ||
|
||
@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 | ||
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); | ||
} | ||
queryBuilder.clause(new SpanTermQueryBuilder(fieldName, value)); | ||
} | ||
queryBuilder.inOrder(randomBoolean()); | ||
queryBuilder.collectPayloads(randomBoolean()); | ||
return queryBuilder; | ||
} | ||
|
||
@Test | ||
public void testValidate() { | ||
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(1); | ||
assertValidate(queryBuilder, 1); // empty clause list | ||
|
||
int totalExpectedErrors = 0; | ||
int clauses = randomIntBetween(1, 10); | ||
for (int i = 0; i < clauses; i++) { | ||
if (randomBoolean()) { | ||
queryBuilder.clause(new SpanTermQueryBuilder("", "test")); | ||
totalExpectedErrors++; | ||
} else { | ||
queryBuilder.clause(new SpanTermQueryBuilder("name", "value")); | ||
} | ||
} | ||
assertValidate(queryBuilder, totalExpectedErrors); | ||
} | ||
} |
Oops, something went wrong.