From 58a77748f71553703a1723c95d38bca46cdfa4d9 Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Thu, 21 Dec 2023 09:24:31 -0800 Subject: [PATCH] Formatting Signed-off-by: Harsha Vamsi Kalluri --- .../index/mapper/KeywordFieldMapper.java | 5 +- .../opensearch/index/query/QueryBuilders.java | 2 - .../index/mapper/KeywordFieldTypeTests.java | 24 +++- .../query/MultiMatchQueryBuilderTests.java | 64 +++++++-- .../query/QueryStringQueryBuilderTests.java | 132 +++++++++++++++--- .../query/SimpleQueryStringBuilderTests.java | 45 +++++- .../FunctionScoreQueryBuilderTests.java | 7 +- .../index/search/MultiMatchQueryTests.java | 43 +++++- .../index/search/NestedHelperTests.java | 25 +++- 9 files changed, 284 insertions(+), 63 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java index 8c86241b17281..59390c75fdaa7 100644 --- a/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java @@ -397,10 +397,7 @@ public Query termQuery(Object value, QueryShardContext context) { query = new BoostQuery(query, boost()); } if (isSearchable() && hasDocValues()) { - return new IndexOrDocValuesQuery( - query, - SortedSetDocValuesField.newSlowExactQuery(name(), indexedValueForSearch(value)) - ); + return new IndexOrDocValuesQuery(query, SortedSetDocValuesField.newSlowExactQuery(name(), indexedValueForSearch(value))); } if (hasDocValues()) { return SortedSetDocValuesField.newSlowExactQuery(name(), indexedValueForSearch(value)); diff --git a/server/src/main/java/org/opensearch/index/query/QueryBuilders.java b/server/src/main/java/org/opensearch/index/query/QueryBuilders.java index 02c1baf3c1aeb..387d21830aa38 100644 --- a/server/src/main/java/org/opensearch/index/query/QueryBuilders.java +++ b/server/src/main/java/org/opensearch/index/query/QueryBuilders.java @@ -32,8 +32,6 @@ package org.opensearch.index.query; -import org.apache.lucene.search.IndexOrDocValuesQuery; -import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; import org.opensearch.common.Nullable; import org.opensearch.common.geo.GeoPoint; diff --git a/server/src/test/java/org/opensearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/KeywordFieldTypeTests.java index ae7f6d3589d85..604db352c4260 100644 --- a/server/src/test/java/org/opensearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/KeywordFieldTypeTests.java @@ -131,7 +131,13 @@ protected TokenStream normalize(String fieldName, TokenStream in) { } }; MappedFieldType ft = new KeywordFieldType("field", new NamedAnalyzer("my_normalizer", AnalyzerScope.INDEX, normalizer)); - assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term("field", "foo bar")), SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("foo bar"))), ft.termQuery("fOo BaR", null)); + assertEquals( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field", "foo bar")), + SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("foo bar")) + ), + ft.termQuery("fOo BaR", null) + ); } public void testTermsQuery() { @@ -345,9 +351,21 @@ public void testWildCardQuery() { public void testNormalizeQueries() { MappedFieldType ft = new KeywordFieldType("field"); - assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term("field", new BytesRef("FOO"))), SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("FOO"))), ft.termQuery("FOO", null)); + assertEquals( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field", new BytesRef("FOO"))), + SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("FOO")) + ), + ft.termQuery("FOO", null) + ); ft = new KeywordFieldType("field", Lucene.STANDARD_ANALYZER); - assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term("field", new BytesRef("foo"))), SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("foo"))), ft.termQuery("FOO", null)); + assertEquals( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field", new BytesRef("foo"))), + SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("foo")) + ), + ft.termQuery("FOO", null) + ); } public void testFetchSourceValue() throws IOException { diff --git a/server/src/test/java/org/opensearch/index/query/MultiMatchQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/MultiMatchQueryBuilderTests.java index e9a8602f764ed..187850fe1d224 100644 --- a/server/src/test/java/org/opensearch/index/query/MultiMatchQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/MultiMatchQueryBuilderTests.java @@ -244,7 +244,13 @@ public void testToQueryMultipleFieldsDisableDismax() throws Exception { .tieBreaker(1.0f) .toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 1 ); assertEquals(expected, query); @@ -253,7 +259,13 @@ public void testToQueryMultipleFieldsDisableDismax() throws Exception { public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { Query query = multiMatchQuery("test").field(TEXT_FIELD_NAME).field(KEYWORD_FIELD_NAME).toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 0 ); assertEquals(expected, query); @@ -262,7 +274,13 @@ public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { public void testToQueryFieldsWildcard() throws Exception { Query query = multiMatchQuery("test").field("mapped_str*").tieBreaker(1.0f).toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 1 ); assertEquals(expected, query); @@ -461,7 +479,13 @@ public void testDefaultField() throws Exception { DisjunctionMaxQuery expected = new DisjunctionMaxQuery( Arrays.asList( new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), - new BoostQuery(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello"))), 5.0f) + new BoostQuery( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ), + 5.0f + ) ), 0.0f ); @@ -489,7 +513,13 @@ public void testDefaultField() throws Exception { Arrays.asList( new MatchNoDocsQuery("failed [mapped_int] query, caused by number_format_exception:[For input string: \"hello\"]"), new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), - new BoostQuery(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello"))), 5.0f) + new BoostQuery( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ), + 5.0f + ) ), 0.0f ); @@ -533,8 +563,20 @@ public void testWithStopWords() throws Exception { new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "quick")), BooleanClause.Occur.SHOULD) .add(new TermQuery(new Term(TEXT_FIELD_NAME, "fox")), BooleanClause.Occur.SHOULD) .build(), - new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick"))), BooleanClause.Occur.SHOULD) - .add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox"))), BooleanClause.Occur.SHOULD) + new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick")) + ), + BooleanClause.Occur.SHOULD + ) + .add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox")) + ), + BooleanClause.Occur.SHOULD + ) .build() ), 0f @@ -590,7 +632,13 @@ private void assertQueryWithAllFieldsWildcard(Query query) { assertEquals(9, noMatchNoDocsQueries); assertThat( disjunctionMaxQuery.getDisjuncts(), - hasItems(new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")))) + hasItems( + new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ) + ) ); } diff --git a/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java index e947d3523e7e1..934f48da2a2c9 100644 --- a/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java @@ -75,7 +75,6 @@ import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.index.mapper.FieldNamesFieldMapper; -import org.opensearch.index.mapper.KeywordFieldMapper; import org.opensearch.index.mapper.MapperService; import org.opensearch.index.search.QueryStringQueryParser; import org.opensearch.test.AbstractQueryTestCase; @@ -504,7 +503,13 @@ public void testToQueryMultipleTermsBooleanQuery() throws Exception { public void testToQueryMultipleFieldsBooleanQuery() throws Exception { Query query = queryStringQuery("test").field(TEXT_FIELD_NAME).field(KEYWORD_FIELD_NAME).toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 0 ); assertEquals(expected, query); @@ -513,7 +518,13 @@ public void testToQueryMultipleFieldsBooleanQuery() throws Exception { public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { Query query = queryStringQuery("test").field(TEXT_FIELD_NAME).field(KEYWORD_FIELD_NAME).toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 0 ); assertEquals(expected, query); @@ -522,7 +533,13 @@ public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { public void testToQueryFieldsWildcard() throws Exception { Query query = queryStringQuery("test").field("mapped_str*").toQuery(createShardContext()); Query expected = new DisjunctionMaxQuery( - List.of(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")))), + List.of( + new TermQuery(new Term(TEXT_FIELD_NAME, "test")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) + ), 0 ); assertEquals(expected, query); @@ -547,7 +564,10 @@ public void testToQueryDisMaxQuery() throws Exception { Query expected = new DisjunctionMaxQuery( List.of( new BoostQuery(new TermQuery(new Term(TEXT_FIELD_NAME, "test")), 2.2f), - new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test"))) + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "test")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("test")) + ) ), 0 ); @@ -1008,7 +1028,12 @@ public void testToQueryTextParsing() throws IOException { ).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), BooleanClause.Occur.SHOULD)).build(); List disjuncts = new ArrayList<>(); disjuncts.add(bq1); - disjuncts.add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")))); + disjuncts.add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")) + ) + ); DisjunctionMaxQuery expectedQuery = new DisjunctionMaxQuery(disjuncts, 0.0f); assertThat(query, equalTo(expectedQuery)); } @@ -1022,7 +1047,12 @@ public void testToQueryTextParsing() throws IOException { List disjuncts = new ArrayList<>(); PhraseQuery pq = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "foo")).add(new Term(TEXT_FIELD_NAME, "bar")).build(); disjuncts.add(pq); - disjuncts.add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")))); + disjuncts.add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")) + ) + ); DisjunctionMaxQuery expectedQuery = new DisjunctionMaxQuery(disjuncts, 0.0f); assertThat(query, equalTo(expectedQuery)); } @@ -1036,7 +1066,12 @@ public void testToQueryTextParsing() throws IOException { ).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), BooleanClause.Occur.SHOULD)).build(); List disjuncts = new ArrayList<>(); disjuncts.add(bq1); - disjuncts.add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")))); + disjuncts.add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo bar")) + ) + ); DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(disjuncts, 0.0f); BooleanQuery expectedQuery = new BooleanQuery.Builder().add(disjunctionMaxQuery, BooleanClause.Occur.SHOULD) .add(new TermQuery(new Term(TEXT_FIELD_NAME, "other")), BooleanClause.Occur.SHOULD) @@ -1051,12 +1086,22 @@ public void testToQueryTextParsing() throws IOException { List disjuncts1 = new ArrayList<>(); disjuncts1.add(new TermQuery(new Term(TEXT_FIELD_NAME, "foo"))); - disjuncts1.add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo")))); + disjuncts1.add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo")) + ) + ); DisjunctionMaxQuery maxQuery1 = new DisjunctionMaxQuery(disjuncts1, 0.0f); List disjuncts2 = new ArrayList<>(); disjuncts2.add(new TermQuery(new Term(TEXT_FIELD_NAME, "bar"))); - disjuncts2.add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar")))); + disjuncts2.add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar")) + ) + ); DisjunctionMaxQuery maxQuery2 = new DisjunctionMaxQuery(disjuncts2, 0.0f); BooleanQuery expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(maxQuery1, BooleanClause.Occur.SHOULD)) @@ -1297,7 +1342,13 @@ public void testDefaultField() throws Exception { Query expected = new DisjunctionMaxQuery( Arrays.asList( new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), - new BoostQuery(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello"))), 5.0f) + new BoostQuery( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ), + 5.0f + ) ), 0.0f ); @@ -1353,7 +1404,10 @@ public void testQuoteFieldSuffix() throws IOException { new QueryStringQueryBuilder("bar").quoteFieldSuffix("_2").field(TEXT_FIELD_NAME).doToQuery(context) ); assertEquals( - new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar"))), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar")) + ), new QueryStringQueryBuilder("\"bar\"").quoteFieldSuffix("_2").field(TEXT_FIELD_NAME).doToQuery(context) ); @@ -1394,8 +1448,20 @@ public void testWithStopWords() throws Exception { new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "quick")), Occur.SHOULD) .add(new TermQuery(new Term(TEXT_FIELD_NAME, "fox")), Occur.SHOULD) .build(), - new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick"))), Occur.SHOULD) - .add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox"))), Occur.SHOULD) + new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick")) + ), + Occur.SHOULD + ) + .add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox")) + ), + Occur.SHOULD + ) .build() ), 0f @@ -1461,7 +1527,10 @@ public void testCrossFields() throws Exception { .toQuery(createShardContext()); List queries = new ArrayList<>(terms.length); Query textFieldExpected = BlendedTermQuery.dismaxBlendedQuery(terms, 1.0f); - Query keywordFieldExpected = new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo"))); + Query keywordFieldExpected = new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo")) + ); queries.add(textFieldExpected); queries.add(keywordFieldExpected); Query expected = new DisjunctionMaxQuery(queries, 0); @@ -1470,13 +1539,18 @@ public void testCrossFields() throws Exception { query = new QueryStringQueryBuilder("foo mapped_string:10").analyzer("whitespace") .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS) .toQuery(createShardContext()); - expected = new BooleanQuery.Builder() - .add(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo"))), Occur.SHOULD) - .add(BlendedTermQuery.dismaxBlendedQuery(terms, 1.0f), Occur.SHOULD) + expected = new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "foo")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("foo")) + ), + Occur.SHOULD + ) + .add(BlendedTermQuery.dismaxBlendedQuery(terms, 1.0f), Occur.SHOULD) .add(new TermQuery(new Term(TEXT_FIELD_NAME, "10")), Occur.SHOULD) .build(); - //TODO: figure out how to use DisMaxQueries with keyword fields and boolean queries -// assertEquals(expected, query); + // TODO: figure out how to use DisMaxQueries with keyword fields and boolean queries + // assertEquals(expected, query); } finally { // Reset the default value context.getIndexSettings() @@ -1544,7 +1618,15 @@ public void testMergeBoosts() throws IOException { .toQuery(createShardContext()); List terms = new ArrayList<>(); terms.add(new BoostQuery(new TermQuery(new Term(TEXT_FIELD_NAME, "first")), 0.075f)); - terms.add(new BoostQuery(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "first")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("first"))), 0.5f)); + terms.add( + new BoostQuery( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "first")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("first")) + ), + 0.5f + ) + ); Query expected = new DisjunctionMaxQuery(terms, 1.0f); assertEquals(expected, query); } @@ -1566,7 +1648,13 @@ private void assertQueryWithAllFieldsWildcard(Query query) { assertEquals(9, noMatchNoDocsQueries); assertThat( disjunctionMaxQuery.getDisjuncts(), - hasItems(new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")))) + hasItems( + new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ) + ) ); } diff --git a/server/src/test/java/org/opensearch/index/query/SimpleQueryStringBuilderTests.java b/server/src/test/java/org/opensearch/index/query/SimpleQueryStringBuilderTests.java index 8a0952aea7218..9bdd6beda7be3 100644 --- a/server/src/test/java/org/opensearch/index/query/SimpleQueryStringBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/SimpleQueryStringBuilderTests.java @@ -56,7 +56,6 @@ import org.apache.lucene.tests.util.TestUtil; import org.apache.lucene.util.BytesRef; import org.opensearch.cluster.metadata.IndexMetadata; -import org.opensearch.common.inject.Key; import org.opensearch.common.settings.Settings; import org.opensearch.index.search.SimpleQueryStringQueryParser; import org.opensearch.test.AbstractQueryTestCase; @@ -309,7 +308,9 @@ protected void doAssertLuceneQuery(SimpleQueryStringBuilder queryBuilder, Query for (Query disjunct : maxQuery.getDisjuncts()) { assertThat( disjunct, - either(instanceOf(TermQuery.class)).or(instanceOf(BoostQuery.class)).or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)) + either(instanceOf(TermQuery.class)).or(instanceOf(BoostQuery.class)) + .or(instanceOf(MatchNoDocsQuery.class)) + .or(instanceOf(IndexOrDocValuesQuery.class)) ); Query termQuery = disjunct; if (disjunct instanceof BoostQuery) { @@ -628,7 +629,13 @@ public void testQuoteFieldSuffix() { createShardContext() ); assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), parser.parse("bar")); - assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar"))), parser.parse("\"bar\"")); + assertEquals( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("bar")) + ), + parser.parse("\"bar\"") + ); // Now check what happens if the quote field does not exist settings.quoteFieldSuffix(".quote"); @@ -674,7 +681,13 @@ public void testDefaultField() throws Exception { Query expected = new DisjunctionMaxQuery( Arrays.asList( new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), - new BoostQuery(new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello"))), 5.0f) + new BoostQuery( + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ), + 5.0f + ) ), 1.0f ); @@ -740,14 +753,26 @@ public void testWithStopWords() throws Exception { .toQuery(createShardContext()); expected = new BooleanQuery.Builder().add( new DisjunctionMaxQuery( - Arrays.asList(new TermQuery(new Term(TEXT_FIELD_NAME, "quick")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick")))), + Arrays.asList( + new TermQuery(new Term(TEXT_FIELD_NAME, "quick")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "quick")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("quick")) + ) + ), 1.0f ), BooleanClause.Occur.SHOULD ) .add( new DisjunctionMaxQuery( - Arrays.asList(new TermQuery(new Term(TEXT_FIELD_NAME, "fox")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox")))), + Arrays.asList( + new TermQuery(new Term(TEXT_FIELD_NAME, "fox")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "fox")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("fox")) + ) + ), 1.0f ), BooleanClause.Occur.SHOULD @@ -838,7 +863,13 @@ private void assertQueryWithAllFieldsWildcard(Query query) { assertEquals(9, noMatchNoDocsQueries); assertThat( disjunctionMaxQuery.getDisjuncts(), - hasItems(new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), new IndexOrDocValuesQuery(new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")))) + hasItems( + new TermQuery(new Term(TEXT_FIELD_NAME, "hello")), + new IndexOrDocValuesQuery( + new TermQuery(new Term(KEYWORD_FIELD_NAME, "hello")), + SortedSetDocValuesField.newSlowExactQuery(KEYWORD_FIELD_NAME, new BytesRef("hello")) + ) + ) ); } diff --git a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index b61c0245bb449..fdcd007a3a21b 100644 --- a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -34,14 +34,12 @@ import com.fasterxml.jackson.core.JsonParseException; -import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.util.BytesRef; import org.opensearch.common.geo.GeoPoint; import org.opensearch.common.lucene.search.function.CombineFunction; import org.opensearch.common.lucene.search.function.FieldValueFactorFunction; @@ -624,7 +622,10 @@ public void testCustomWeightFactorQueryBuilderWithFunctionScore() throws IOExcep ).toQuery(context); assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class)); FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery; - assertThat((((IndexOrDocValuesQuery) functionScoreQuery.getSubQuery()).getIndexQuery()), equalTo(new TermQuery(new Term(KEYWORD_FIELD_NAME, "banon")))); + assertThat( + (((IndexOrDocValuesQuery) functionScoreQuery.getSubQuery()).getIndexQuery()), + equalTo(new TermQuery(new Term(KEYWORD_FIELD_NAME, "banon"))) + ); assertThat((double) (functionScoreQuery.getFunctions()[0]).getWeight(), closeTo(1.3, 0.001)); } diff --git a/server/src/test/java/org/opensearch/index/search/MultiMatchQueryTests.java b/server/src/test/java/org/opensearch/index/search/MultiMatchQueryTests.java index 415c4c0f37d08..3042c488fa80c 100644 --- a/server/src/test/java/org/opensearch/index/search/MultiMatchQueryTests.java +++ b/server/src/test/java/org/opensearch/index/search/MultiMatchQueryTests.java @@ -43,7 +43,6 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; -import org.apache.lucene.search.Sort; import org.apache.lucene.search.SynonymQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.tests.analysis.MockSynonymAnalyzer; @@ -324,13 +323,43 @@ public void testKeywordSplitQueriesOnWhitespace() throws IOException { Query query = parser.parse(MultiMatchQueryBuilder.Type.BEST_FIELDS, fieldNames, "Foo Bar", null); DisjunctionMaxQuery expected = new DisjunctionMaxQuery( Arrays.asList( - new IndexOrDocValuesQuery(new TermQuery(new Term("field_normalizer", "foo bar")), SortedSetDocValuesField.newSlowExactQuery("field_normalizer", new BytesRef("foo bar"))), - new IndexOrDocValuesQuery(new TermQuery(new Term("field", "Foo Bar")), SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("Foo Bar"))), - new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term("field_split", "Foo")), SortedSetDocValuesField.newSlowExactQuery("field_split", new BytesRef("Foo"))), BooleanClause.Occur.SHOULD) - .add(new IndexOrDocValuesQuery(new TermQuery(new Term("field_split", "Bar")), SortedSetDocValuesField.newSlowExactQuery("field_split", new BytesRef("Bar"))), BooleanClause.Occur.SHOULD) + new IndexOrDocValuesQuery( + new TermQuery(new Term("field_normalizer", "foo bar")), + SortedSetDocValuesField.newSlowExactQuery("field_normalizer", new BytesRef("foo bar")) + ), + new IndexOrDocValuesQuery( + new TermQuery(new Term("field", "Foo Bar")), + SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef("Foo Bar")) + ), + new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field_split", "Foo")), + SortedSetDocValuesField.newSlowExactQuery("field_split", new BytesRef("Foo")) + ), + BooleanClause.Occur.SHOULD + ) + .add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field_split", "Bar")), + SortedSetDocValuesField.newSlowExactQuery("field_split", new BytesRef("Bar")) + ), + BooleanClause.Occur.SHOULD + ) .build(), - new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term("field_split_normalizer", "foo")), SortedSetDocValuesField.newSlowExactQuery("fied_split_normalizer", new BytesRef("foo"))), BooleanClause.Occur.SHOULD) - .add(new IndexOrDocValuesQuery(new TermQuery(new Term("field_split_normalizer", "bar")), SortedSetDocValuesField.newSlowExactQuery("field_split_normalizer", new BytesRef("bar"))), BooleanClause.Occur.SHOULD) + new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field_split_normalizer", "foo")), + SortedSetDocValuesField.newSlowExactQuery("fied_split_normalizer", new BytesRef("foo")) + ), + BooleanClause.Occur.SHOULD + ) + .add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("field_split_normalizer", "bar")), + SortedSetDocValuesField.newSlowExactQuery("field_split_normalizer", new BytesRef("bar")) + ), + BooleanClause.Occur.SHOULD + ) .build() ), 0.0f diff --git a/server/src/test/java/org/opensearch/index/search/NestedHelperTests.java b/server/src/test/java/org/opensearch/index/search/NestedHelperTests.java index 5e340685a1611..753db2186b871 100644 --- a/server/src/test/java/org/opensearch/index/search/NestedHelperTests.java +++ b/server/src/test/java/org/opensearch/index/search/NestedHelperTests.java @@ -342,7 +342,10 @@ public void testNested() throws IOException { query = (OpenSearchToParentBlockJoinQuery) queryBuilder.toQuery(context); // this time we do not add a filter since the inner query only matches inner docs - expectedChildQuery = new IndexOrDocValuesQuery(new TermQuery(new Term("nested1.foo", "bar")), SortedSetDocValuesField.newSlowExactQuery("nested1.foo", new BytesRef("bar"))); + expectedChildQuery = new IndexOrDocValuesQuery( + new TermQuery(new Term("nested1.foo", "bar")), + SortedSetDocValuesField.newSlowExactQuery("nested1.foo", new BytesRef("bar")) + ); assertEquals(expectedChildQuery, query.getChildQuery()); assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query)); @@ -355,9 +358,13 @@ public void testNested() throws IOException { query = (OpenSearchToParentBlockJoinQuery) queryBuilder.toQuery(context); // we need to add the filter again because of include_in_parent - expectedChildQuery = new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term("nested2.foo", "bar")), SortedSetDocValuesField.newSlowExactQuery("nested2.foo", new BytesRef("bar"))), Occur.MUST) - .add(new TermQuery(new Term(NestedPathFieldMapper.NAME, "nested2")), Occur.FILTER) - .build(); + expectedChildQuery = new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("nested2.foo", "bar")), + SortedSetDocValuesField.newSlowExactQuery("nested2.foo", new BytesRef("bar")) + ), + Occur.MUST + ).add(new TermQuery(new Term(NestedPathFieldMapper.NAME, "nested2")), Occur.FILTER).build(); assertEquals(expectedChildQuery, query.getChildQuery()); assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query)); @@ -370,9 +377,13 @@ public void testNested() throws IOException { query = (OpenSearchToParentBlockJoinQuery) queryBuilder.toQuery(context); // we need to add the filter again because of include_in_root - expectedChildQuery = new BooleanQuery.Builder().add(new IndexOrDocValuesQuery(new TermQuery(new Term("nested3.foo", "bar")), SortedSetDocValuesField.newSlowExactQuery("nested3.foo", new BytesRef("bar"))), Occur.MUST) - .add(new TermQuery(new Term(NestedPathFieldMapper.NAME, "nested3")), Occur.FILTER) - .build(); + expectedChildQuery = new BooleanQuery.Builder().add( + new IndexOrDocValuesQuery( + new TermQuery(new Term("nested3.foo", "bar")), + SortedSetDocValuesField.newSlowExactQuery("nested3.foo", new BytesRef("bar")) + ), + Occur.MUST + ).add(new TermQuery(new Term(NestedPathFieldMapper.NAME, "nested3")), Occur.FILTER).build(); assertEquals(expectedChildQuery, query.getChildQuery()); assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));