|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.search; |
| 21 | + |
| 22 | +import org.elasticsearch.common.settings.Settings; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.index.IndexService; |
| 25 | +import org.elasticsearch.index.mapper.MapperService; |
| 26 | +import org.elasticsearch.index.query.QueryShardContext; |
| 27 | +import org.elasticsearch.test.ESSingleNodeTestCase; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | + |
| 31 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 32 | + |
| 33 | +public class QueryParserHelperTests extends ESSingleNodeTestCase { |
| 34 | + |
| 35 | + /** |
| 36 | + * Test that when {@link QueryParserHelper#resolveMappingFields(QueryShardContext, java.util.Map, String)} exceeds |
| 37 | + * the limit of 1024 fields, we emit a warning |
| 38 | + */ |
| 39 | + public void testLimitOnExpandedFields() throws Exception { |
| 40 | + |
| 41 | + XContentBuilder builder = jsonBuilder(); |
| 42 | + builder.startObject(); |
| 43 | + builder.startObject("type1"); |
| 44 | + builder.startObject("properties"); |
| 45 | + for (int i = 0; i < 1025; i++) { |
| 46 | + builder.startObject("field" + i).field("type", "text").endObject(); |
| 47 | + } |
| 48 | + builder.endObject(); // properties |
| 49 | + builder.endObject(); // type1 |
| 50 | + builder.endObject(); |
| 51 | + IndexService indexService = createIndex("toomanyfields", Settings.builder() |
| 52 | + .put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1200).build(), |
| 53 | + "type1", builder); |
| 54 | + client().prepareIndex("toomanyfields", "type1", "1").setSource("field171", "foo bar baz").get(); |
| 55 | + |
| 56 | + QueryShardContext queryShardContext = indexService.newQueryShardContext( |
| 57 | + randomInt(20), null, () -> { throw new UnsupportedOperationException(); }, null); |
| 58 | + |
| 59 | + QueryParserHelper.resolveMappingField(queryShardContext, "*", 1.0f, true, false); |
| 60 | + assertWarnings("Field expansion matches too many fields, got: 1025. A limit of 1024 will be enforced starting with " |
| 61 | + + "version 7.0 of Elasticsearch. Lowering the number of fields will be necessary before upgrading."); |
| 62 | + |
| 63 | + QueryParserHelper.resolveMappingFields(queryShardContext,Collections.singletonMap("*", 1.0f)); |
| 64 | + assertWarnings("Field expansion matches too many fields, got: 1025. A limit of 1024 will be enforced starting with " |
| 65 | + + "version 7.0 of Elasticsearch. Lowering the number of fields will be necessary before upgrading."); |
| 66 | + } |
| 67 | +} |
0 commit comments