-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Query refactoring: MatchAllQueryBuilder #10668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
cbuescher
wants to merge
11
commits into
elastic:feature/query-refactoring
from
cbuescher:feature/query-refactoring-matchall
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6071c77
Query refactoring: MatchAllQueryBuilder
ff6743d
Add getters, extract BaseQueryTest class
7438190
Change equals check for float primitive, extracting common test setup…
c1b369a
Revert test setup in @BeforeClass
e587336
Change equals() to prevent possibility of npe
99276c1
More generalization of base test case
a2e665b
Pulled out all of serialization test to base class, added javadoc
0b5ac31
Renamed methods, directly testing toQuery() on the initial testQuery
2cfe30c
Renamed test query creation method
84e3d23
Change abstract method visibility and removing common setup
6d41e1f
Deleted superflous brackets
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
166 changes: 166 additions & 0 deletions
166
src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java
This file contains hidden or 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,166 @@ | ||
| /* | ||
| * 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.elasticsearch.Version; | ||
| import org.elasticsearch.cluster.ClusterService; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.common.inject.AbstractModule; | ||
| import org.elasticsearch.common.inject.Injector; | ||
| import org.elasticsearch.common.inject.ModulesBuilder; | ||
| import org.elasticsearch.common.inject.util.Providers; | ||
| import org.elasticsearch.common.io.stream.BytesStreamInput; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.io.stream.Streamable; | ||
| import org.elasticsearch.common.settings.ImmutableSettings; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.settings.SettingsModule; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.env.EnvironmentModule; | ||
| import org.elasticsearch.index.Index; | ||
| import org.elasticsearch.index.IndexNameModule; | ||
| import org.elasticsearch.index.analysis.AnalysisModule; | ||
| import org.elasticsearch.index.cache.IndexCacheModule; | ||
| import org.elasticsearch.index.query.functionscore.FunctionScoreModule; | ||
| import org.elasticsearch.index.settings.IndexSettingsModule; | ||
| import org.elasticsearch.index.similarity.SimilarityModule; | ||
| import org.elasticsearch.indices.breaker.CircuitBreakerService; | ||
| import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
| import org.elasticsearch.indices.query.IndicesQueriesModule; | ||
| import org.elasticsearch.script.ScriptModule; | ||
| import org.elasticsearch.test.ElasticsearchTestCase; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.threadpool.ThreadPoolModule; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Ignore; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @Ignore | ||
| public abstract class BaseQueryTestCase<QB extends BaseQueryBuilder & Streamable> extends ElasticsearchTestCase { | ||
|
|
||
| private static Injector injector; | ||
| private static IndexQueryParserService queryParserService; | ||
| private static Index index; | ||
|
|
||
| protected QB testQuery = createTestQueryBuilder(); | ||
|
|
||
| /** | ||
| * Setup for the whole base test class. | ||
| * @throws IOException | ||
| */ | ||
| @BeforeClass | ||
| public static void init() throws IOException { | ||
| Settings settings = ImmutableSettings.settingsBuilder() | ||
| .put("name", BaseQueryTestCase.class.toString()) | ||
| .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) | ||
| .build(); | ||
|
|
||
| index = new Index("test"); | ||
| injector = new ModulesBuilder().add( | ||
| new EnvironmentModule(new Environment(settings)), | ||
| new SettingsModule(settings), | ||
| new ThreadPoolModule(settings), | ||
| new IndicesQueriesModule(), | ||
| new ScriptModule(settings), | ||
| new IndexSettingsModule(index, settings), | ||
| new IndexCacheModule(settings), | ||
| new AnalysisModule(settings), | ||
| new SimilarityModule(settings), | ||
| new IndexNameModule(index), | ||
| new IndexQueryParserModule(settings), | ||
| new FunctionScoreModule(), | ||
| new AbstractModule() { | ||
| @Override | ||
| protected void configure() { | ||
| bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); | ||
| bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); | ||
| } | ||
| } | ||
| ).createInjector(); | ||
| queryParserService = injector.getInstance(IndexQueryParserService.class); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void after() throws Exception { | ||
| terminate(injector.getInstance(ThreadPool.class)); | ||
| } | ||
|
|
||
| /** | ||
| * Create the query that is being tested | ||
| */ | ||
| protected abstract QB createTestQueryBuilder(); | ||
|
|
||
| /** | ||
| * Subclass should handle assertions on the lucene query produced by the query builder under test here | ||
| */ | ||
| protected abstract void assertLuceneQuery(QB queryBuilder, Query query) throws IOException; | ||
|
|
||
| /** | ||
| * Creates an empty builder of the type of query under test | ||
| */ | ||
| protected abstract QB createEmptyQueryBuilder(); | ||
|
|
||
| /** | ||
| * Generic test that creates new query from the test query and checks both for equality | ||
| * and asserts equality on the two queries. | ||
| */ | ||
| @Test | ||
| public void testFromXContent() throws IOException { | ||
| QueryParseContext context = new QueryParseContext(index, queryParserService); | ||
| String contentString = testQuery.toString(); | ||
| context.reset(XContentFactory.xContent(contentString).createParser(contentString)); | ||
|
|
||
| QueryBuilder newQuery = queryParserService.queryParser(testQuery.parserName()).fromXContent(context); | ||
| assertNotSame(newQuery, testQuery); | ||
| assertEquals(newQuery, testQuery); | ||
| } | ||
|
|
||
| /** | ||
| * Test creates the {@link Query} from the {@link QueryBuilder} under test and delegates the | ||
| * assertions being made on the result to the implementing subclass. | ||
| */ | ||
| @Test | ||
| public void testToQuery() throws IOException { | ||
| QueryParseContext context = new QueryParseContext(index, queryParserService); | ||
| assertLuceneQuery(this.testQuery, this.testQuery.toQuery(context)); | ||
| } | ||
|
|
||
| /** | ||
| * Test serialization and deserialization of the test query. | ||
| * @throws IOException | ||
| */ | ||
| @Test | ||
| public void testSerialization() throws IOException { | ||
| BytesStreamOutput output = new BytesStreamOutput(); | ||
| testQuery.writeTo(output); | ||
|
|
||
| BytesStreamInput in = new BytesStreamInput(output.bytes()); | ||
| QB deserializedQuery = createEmptyQueryBuilder(); | ||
| deserializedQuery.readFrom(in); | ||
|
|
||
| assertEquals(deserializedQuery, testQuery); | ||
| assertNotSame(deserializedQuery, testQuery); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/test/java/org/elasticsearch/index/query/MatchAllQueryBuilderTest.java
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * 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.MatchAllDocsQuery; | ||
| import org.apache.lucene.search.Query; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.hamcrest.Matchers.*; | ||
|
|
||
| public class MatchAllQueryBuilderTest extends BaseQueryTestCase<MatchAllQueryBuilder> { | ||
|
|
||
| @Override | ||
| protected void assertLuceneQuery(MatchAllQueryBuilder queryBuilder, Query query) throws IOException { | ||
| if (queryBuilder.boost() != 1.0f) { | ||
| assertThat(query, instanceOf(MatchAllDocsQuery.class)); | ||
| } else { | ||
| assertThat(query, instanceOf(ConstantScoreQuery.class)); | ||
| } | ||
| assertThat(query.getBoost(), is(queryBuilder.boost())); | ||
| } | ||
|
|
||
| @Override | ||
| protected MatchAllQueryBuilder createEmptyQueryBuilder() { | ||
| return new MatchAllQueryBuilder(); | ||
| } | ||
|
|
||
| /** | ||
| * @return a MatchAllQuery with random boost between 0.1f and 2.0f | ||
| */ | ||
| @Override | ||
| protected MatchAllQueryBuilder createTestQueryBuilder() { | ||
| MatchAllQueryBuilder query = new MatchAllQueryBuilder(); | ||
| if (randomBoolean()) { | ||
| query.boost(2.0f / randomIntBetween(1, 20)); | ||
| } | ||
| return query; | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding
equalsandhashCode, in this specific case with a single float field, I tend to like better what my IntelliJ generates. It usesFloat.compareinequals, and avoids NPEs if the argument is null:Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fancy, fine if I can add some more curly brackets to the one line ifs since you urged me to do so in another PR the other day ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea right.... curly brackets... I know :)