-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Refactors WildcardQueryBuilder and Parser #12110
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
Merged
alexksikes
merged 1 commit into
elastic:feature/query-refactoring
from
alexksikes:feature/query-refactoring-wildcard
Jul 24, 2015
Merged
Changes from all commits
Commits
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
101 changes: 101 additions & 0 deletions
101
core/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTest.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,101 @@ | ||
/* | ||
* 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.index.Term; | ||
import org.apache.lucene.search.MultiTermQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.WildcardQuery; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.ParseFieldMatcher; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.query.support.QueryParsers; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class WildcardQueryBuilderTest extends BaseQueryTestCase<WildcardQueryBuilder> { | ||
|
||
@Override | ||
protected WildcardQueryBuilder doCreateTestQueryBuilder() { | ||
WildcardQueryBuilder query; | ||
|
||
// mapped or unmapped field | ||
String text = randomAsciiOfLengthBetween(1, 10); | ||
if (randomBoolean()) { | ||
query = new WildcardQueryBuilder(STRING_FIELD_NAME, text); | ||
} else { | ||
query = new WildcardQueryBuilder(randomAsciiOfLengthBetween(1, 10), text); | ||
} | ||
if (randomBoolean()) { | ||
query.rewrite(randomFrom(getRandomRewriteMethod())); | ||
} | ||
return query; | ||
} | ||
|
||
@Override | ||
protected Query doCreateExpectedQuery(WildcardQueryBuilder queryBuilder, QueryParseContext context) throws IOException { | ||
String indexFieldName; | ||
BytesRef valueBytes; | ||
|
||
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName()); | ||
if (fieldType != null) { | ||
indexFieldName = fieldType.names().indexName(); | ||
valueBytes = fieldType.indexedValueForSearch(queryBuilder.value()); | ||
} else { | ||
indexFieldName = queryBuilder.fieldName(); | ||
valueBytes = new BytesRef(queryBuilder.value()); | ||
} | ||
|
||
WildcardQuery expectedQuery = new WildcardQuery(new Term(indexFieldName, valueBytes)); | ||
|
||
//norelease fix to be removed to avoid NPE on unmapped fields | ||
context.parseFieldMatcher(randomBoolean() ? ParseFieldMatcher.EMPTY : ParseFieldMatcher.STRICT); | ||
MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(context.parseFieldMatcher(), queryBuilder.rewrite(), null); | ||
QueryParsers.setRewriteMethod(expectedQuery, rewriteMethod); | ||
return expectedQuery; | ||
} | ||
|
||
@Test | ||
public void testValidate() { | ||
WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("", "text"); | ||
assertThat(wildcardQueryBuilder.validate().validationErrors().size(), is(1)); | ||
|
||
wildcardQueryBuilder = new WildcardQueryBuilder("field", null); | ||
assertThat(wildcardQueryBuilder.validate().validationErrors().size(), is(1)); | ||
|
||
wildcardQueryBuilder = new WildcardQueryBuilder(null, null); | ||
assertThat(wildcardQueryBuilder.validate().validationErrors().size(), is(2)); | ||
|
||
wildcardQueryBuilder = new WildcardQueryBuilder("field", "text"); | ||
assertNull(wildcardQueryBuilder.validate()); | ||
} | ||
|
||
@Test | ||
public void testEmptyValue() throws IOException { | ||
QueryParseContext context = createContext(); | ||
context.setAllowUnmappedFields(true); | ||
|
||
WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(getRandomType(), ""); | ||
assertEquals(wildcardQueryBuilder.toQuery(context).getClass(), WildcardQuery.class); | ||
} | ||
} |
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.
Would it make sense to check for empty string here as well?
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.
It is not something we systematically do. The parser did not do this check, hence the reason why it is not in validate.
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.
I think it would be interesting then to test what happens when you provide an empty string? is it something that we should accept or should we add validation for it?
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.
did we test this? what happens with empty string? is it something that we should allow or maybe change and disallow?
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.
The parser did allow for it. It does return a valid query.
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.
the question is if that query makes sense, does it return documents? which ones?
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.
it returns an empty result set.
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.
ok lets leave it, it doesn't seem like a problem for now