88
99package org .elasticsearch .search .query ;
1010
11+ import org .apache .lucene .search .IndexSearcher ;
1112import org .elasticsearch .ExceptionsHelper ;
1213import org .elasticsearch .action .index .IndexRequestBuilder ;
1314import org .elasticsearch .action .search .SearchResponse ;
1718import org .elasticsearch .index .query .QueryStringQueryBuilder ;
1819import org .elasticsearch .search .SearchHit ;
1920import org .elasticsearch .search .SearchHits ;
20- import org .elasticsearch .search .SearchModule ;
2121import org .elasticsearch .test .ESIntegTestCase ;
2222import org .elasticsearch .xcontent .XContentBuilder ;
2323import org .elasticsearch .xcontent .XContentType ;
2424import org .junit .Before ;
25- import org .junit .BeforeClass ;
2625
2726import java .io .IOException ;
2827import java .util .ArrayList ;
4241
4342public class QueryStringIT extends ESIntegTestCase {
4443
45- private static int CLUSTER_MAX_CLAUSE_COUNT ;
46-
47- @ BeforeClass
48- public static void createRandomClusterSetting () {
49- CLUSTER_MAX_CLAUSE_COUNT = randomIntBetween (50 , 100 );
50- }
51-
5244 @ Before
5345 public void setup () throws Exception {
5446 String indexBody = copyToStringFromClasspath ("/org/elasticsearch/search/query/all-query-index.json" );
5547 prepareCreate ("test" ).setSource (indexBody , XContentType .JSON ).get ();
5648 ensureGreen ("test" );
5749 }
5850
59- @ Override
60- protected Settings nodeSettings (int nodeOrdinal , Settings otherSettings ) {
61- return Settings .builder ()
62- .put (super .nodeSettings (nodeOrdinal , otherSettings ))
63- .put (SearchModule .INDICES_MAX_CLAUSE_COUNT_SETTING .getKey (), CLUSTER_MAX_CLAUSE_COUNT )
64- .build ();
65- }
66-
6751 public void testBasicAllQuery () throws Exception {
6852 List <IndexRequestBuilder > reqs = new ArrayList <>();
6953 reqs .add (client ().prepareIndex ("test" ).setId ("1" ).setSource ("f1" , "foo bar baz" ));
@@ -250,40 +234,18 @@ public void testAllFieldsWithSpecifiedLeniency() throws IOException {
250234 assertThat (e .getCause ().getMessage (), containsString ("unit [D] not supported for date math [-2D]" ));
251235 }
252236
253- // The only expectation for this test is to not throw exception
254- public void testLimitOnExpandedFieldsButIgnoreUnmappedFields () throws Exception {
255- XContentBuilder builder = jsonBuilder ();
256- builder .startObject ();
257- builder .startObject ("_doc" );
258- builder .startObject ("properties" );
259- for (int i = 0 ; i < CLUSTER_MAX_CLAUSE_COUNT ; i ++) {
260- builder .startObject ("field" + i ).field ("type" , "text" ).endObject ();
261- }
262- builder .endObject (); // properties
263- builder .endObject (); // type1
264- builder .endObject ();
265-
266- assertAcked (prepareCreate ("ignoreunmappedfields" ).setMapping (builder ));
267-
268- client ().prepareIndex ("ignoreunmappedfields" ).setId ("1" ).setSource ("field1" , "foo bar baz" ).get ();
269- refresh ();
237+ public void testLimitOnExpandedFields () throws Exception {
270238
271- QueryStringQueryBuilder qb = queryStringQuery ("bar" );
272- if (randomBoolean ()) {
273- qb .field ("*" ).field ("unmappedField1" ).field ("unmappedField2" ).field ("unmappedField3" ).field ("unmappedField4" );
274- }
275- client ().prepareSearch ("ignoreunmappedfields" ).setQuery (qb ).get ();
276- }
239+ final int maxClauseCount = randomIntBetween (50 , 100 );
277240
278- public void testLimitOnExpandedFields () throws Exception {
279241 XContentBuilder builder = jsonBuilder ();
280242 builder .startObject ();
281243 {
282244 builder .startObject ("_doc" );
283245 {
284246 builder .startObject ("properties" );
285247 {
286- for (int i = 0 ; i < CLUSTER_MAX_CLAUSE_COUNT ; i ++) {
248+ for (int i = 0 ; i < maxClauseCount ; i ++) {
287249 builder .startObject ("field_A" + i ).field ("type" , "text" ).endObject ();
288250 builder .startObject ("field_B" + i ).field ("type" , "text" ).endObject ();
289251 }
@@ -296,25 +258,34 @@ public void testLimitOnExpandedFields() throws Exception {
296258
297259 assertAcked (
298260 prepareCreate ("testindex" ).setSettings (
299- Settings .builder ().put (MapperService .INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING .getKey (), CLUSTER_MAX_CLAUSE_COUNT + 100 )
261+ Settings .builder ().put (MapperService .INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING .getKey (), maxClauseCount + 100 )
300262 ).setMapping (builder )
301263 );
302264
303265 client ().prepareIndex ("testindex" ).setId ("1" ).setSource ("field_A0" , "foo bar baz" ).get ();
304266 refresh ();
305267
306- // single field shouldn't trigger the limit
307- doAssertOneHitForQueryString ("field_A0:foo" );
308- // expanding to the limit should work
309- doAssertOneHitForQueryString ("field_A\\ *:foo" );
268+ int originalMaxClauses = IndexSearcher .getMaxClauseCount ();
269+ try {
270+
271+ IndexSearcher .setMaxClauseCount (maxClauseCount );
272+
273+ // single field shouldn't trigger the limit
274+ doAssertOneHitForQueryString ("field_A0:foo" );
275+ // expanding to the limit should work
276+ doAssertOneHitForQueryString ("field_A\\ *:foo" );
310277
311- // adding a non-existing field on top shouldn't overshoot the limit
312- doAssertOneHitForQueryString ("field_A\\ *:foo unmapped:something" );
278+ // adding a non-existing field on top shouldn't overshoot the limit
279+ doAssertOneHitForQueryString ("field_A\\ *:foo unmapped:something" );
313280
314- // the following should exceed the limit
315- doAssertLimitExceededException ("foo" , CLUSTER_MAX_CLAUSE_COUNT * 2 , "*" );
316- doAssertLimitExceededException ("*:foo" , CLUSTER_MAX_CLAUSE_COUNT * 2 , "*" );
317- doAssertLimitExceededException ("field_\\ *:foo" , CLUSTER_MAX_CLAUSE_COUNT * 2 , "field_*" );
281+ // the following should exceed the limit
282+ doAssertLimitExceededException ("foo" , IndexSearcher .getMaxClauseCount () * 2 , "*" );
283+ doAssertLimitExceededException ("*:foo" , IndexSearcher .getMaxClauseCount () * 2 , "*" );
284+ doAssertLimitExceededException ("field_\\ *:foo" , IndexSearcher .getMaxClauseCount () * 2 , "field_*" );
285+
286+ } finally {
287+ IndexSearcher .setMaxClauseCount (originalMaxClauses );
288+ }
318289 }
319290
320291 private void doAssertOneHitForQueryString (String queryString ) {
@@ -340,7 +311,7 @@ private void doAssertLimitExceededException(String queryString, int exceedingFie
340311 "field expansion for ["
341312 + inputFieldPattern
342313 + "] matches too many fields, limit: "
343- + CLUSTER_MAX_CLAUSE_COUNT
314+ + IndexSearcher . getMaxClauseCount ()
344315 + ", got: "
345316 + exceedingFieldCount
346317 )
0 commit comments