-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add tests for the supported query types. #35319
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
Changes from all commits
cd53702
1f1272c
d5213c8
7a3c18e
4e7765a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,20 @@ | |
|
||
import org.apache.lucene.index.IndexOptions; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.MultiTermQuery; | ||
import org.apache.lucene.search.PrefixQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TermInSetQuery; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.apache.lucene.search.TermRangeQuery; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.unit.Fuzziness; | ||
import org.elasticsearch.index.mapper.JsonFieldMapper.KeyedJsonFieldType; | ||
import org.junit.Before; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class KeyedJsonFieldTypeTests extends FieldTypeTestCase { | ||
|
||
@Before | ||
|
@@ -73,6 +79,22 @@ public void testTermQuery() { | |
assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); | ||
} | ||
|
||
public void testTermsQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
||
Query expected = new TermInSetQuery("field", | ||
new BytesRef("key\0value1"), | ||
new BytesRef("key\0value2")); | ||
|
||
List<String> terms = new ArrayList<>(); | ||
terms.add("value1"); | ||
terms.add("value2"); | ||
Query actual = ft.termsQuery(terms, null); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
public void testExistsQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
@@ -81,6 +103,14 @@ public void testExistsQuery() { | |
assertEquals(expected, ft.existsQuery(null)); | ||
} | ||
|
||
public void testPrefixQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
||
Query expected = new PrefixQuery(new Term("field", "key\0val")); | ||
assertEquals(expected, ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, null)); | ||
} | ||
|
||
public void testFuzzyQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
@@ -90,6 +120,31 @@ public void testFuzzyQuery() { | |
assertEquals("[fuzzy] queries are not currently supported on [json] fields.", e.getMessage()); | ||
} | ||
|
||
public void testRangeQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
||
TermRangeQuery expected = new TermRangeQuery("field", | ||
new BytesRef("key\0lower"), | ||
new BytesRef("key\0upper"), false, false); | ||
assertEquals(expected, ft.rangeQuery("lower", "upper", false, false, null)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also check that we do the right thing if the bounds are inclusive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
expected = new TermRangeQuery("field", | ||
new BytesRef("key\0lower"), | ||
new BytesRef("key\0upper"), true, true); | ||
assertEquals(expected, ft.rangeQuery("lower", "upper", true, true, null)); | ||
|
||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> | ||
ft.rangeQuery("lower", null, false, false, null)); | ||
assertEquals("[range] queries on keyed [json] fields must include both an upper and a lower bound.", | ||
e.getMessage()); | ||
|
||
e = expectThrows(IllegalArgumentException.class, () -> | ||
ft.rangeQuery(null, "upper", false, false, null)); | ||
assertEquals("[range] queries on keyed [json] fields must include both an upper and a lower bound.", | ||
e.getMessage()); | ||
} | ||
|
||
public void testRegexpQuery() { | ||
KeyedJsonFieldType ft = createDefaultFieldType(); | ||
ft.setName("field"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
|
||
import static java.util.Collections.emptyMap; | ||
import static java.util.Collections.singletonMap; | ||
import static org.elasticsearch.index.query.QueryBuilders.existsQuery; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; | ||
|
@@ -227,4 +228,41 @@ public void testFieldAliasWithNoDocValues() throws Exception { | |
assertSearchResponse(response); | ||
assertHitCount(response, 2); | ||
} | ||
|
||
public void testJsonFields() throws Exception { | ||
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() | ||
.startObject("type") | ||
.startObject("properties") | ||
.startObject("headers") | ||
.field("type", "json") | ||
.endObject() | ||
.endObject() | ||
.endObject() | ||
.endObject(); | ||
assertAcked(prepareCreate("test").addMapping("type", mapping)); | ||
|
||
IndexRequestBuilder indexRequest = client().prepareIndex("test", "type", "1") | ||
.setSource(XContentFactory.jsonBuilder() | ||
.startObject() | ||
.startObject("headers") | ||
.field("content-type", "application/json") | ||
.endObject() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not especially related to the current PR, but how is case normalization being handled? The normal HTTP header here is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't apply any lowercasing to the object keys. This is consistent with the way normal field names are handled in the mapping, which are not lowercased (you could have two distinct fields for example, one named Separately for field values (not keys), |
||
.endObject()); | ||
indexRandom(true, false, indexRequest); | ||
|
||
SearchResponse searchResponse = client().prepareSearch() | ||
.setQuery(existsQuery("headers")) | ||
.get(); | ||
assertHitCount(searchResponse, 1L); | ||
|
||
searchResponse = client().prepareSearch() | ||
.setQuery(existsQuery("headers.content-type")) | ||
.get(); | ||
assertHitCount(searchResponse, 1L); | ||
|
||
searchResponse = client().prepareSearch() | ||
.setQuery(existsQuery("headers.nonexistent")) | ||
.get(); | ||
assertHitCount(searchResponse, 0L); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
We could fix this instead of disallowing unbounded range queries, but I wasn't sure the extra complexity was worth it. It's not clear how useful this range functionality will be right now, especially without support for numerics.
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 feel like this limitation is ok for now. We can revisit this later if necessary