QueryShardContextTests.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. package org.elasticsearch.index.query;
  20. import org.apache.lucene.search.Query;
  21. import org.elasticsearch.Version;
  22. import org.elasticsearch.cluster.metadata.IndexMetadata;
  23. import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
  24. import org.elasticsearch.common.io.stream.StreamOutput;
  25. import org.elasticsearch.common.settings.Settings;
  26. import org.elasticsearch.common.util.BigArrays;
  27. import org.elasticsearch.common.xcontent.NamedXContentRegistry;
  28. import org.elasticsearch.common.xcontent.XContentBuilder;
  29. import org.elasticsearch.index.IndexSettings;
  30. import org.elasticsearch.index.fielddata.IndexFieldData;
  31. import org.elasticsearch.index.fielddata.plain.AbstractLeafOrdinalsFieldData;
  32. import org.elasticsearch.index.mapper.IndexFieldMapper;
  33. import org.elasticsearch.index.mapper.MappedFieldType;
  34. import org.elasticsearch.index.mapper.MapperService;
  35. import org.elasticsearch.index.mapper.TextFieldMapper;
  36. import org.elasticsearch.test.ESTestCase;
  37. import java.io.IOException;
  38. import java.util.Collections;
  39. import static org.hamcrest.Matchers.equalTo;
  40. import static org.hamcrest.Matchers.instanceOf;
  41. import static org.hamcrest.Matchers.notNullValue;
  42. import static org.hamcrest.Matchers.nullValue;
  43. import static org.hamcrest.Matchers.sameInstance;
  44. import static org.mockito.Mockito.mock;
  45. import static org.mockito.Mockito.when;
  46. public class QueryShardContextTests extends ESTestCase {
  47. public void testFailIfFieldMappingNotFound() {
  48. QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
  49. context.setAllowUnmappedFields(false);
  50. MappedFieldType fieldType = new TextFieldMapper.TextFieldType("text");
  51. MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType);
  52. assertThat(result, sameInstance(fieldType));
  53. QueryShardException e = expectThrows(QueryShardException.class, () -> context.failIfFieldMappingNotFound("name", null));
  54. assertEquals("No field mapping can be found for the field with name [name]", e.getMessage());
  55. context.setAllowUnmappedFields(true);
  56. result = context.failIfFieldMappingNotFound("name", fieldType);
  57. assertThat(result, sameInstance(fieldType));
  58. result = context.failIfFieldMappingNotFound("name", null);
  59. assertThat(result, nullValue());
  60. context.setAllowUnmappedFields(false);
  61. context.setMapUnmappedFieldAsString(true);
  62. result = context.failIfFieldMappingNotFound("name", fieldType);
  63. assertThat(result, sameInstance(fieldType));
  64. result = context.failIfFieldMappingNotFound("name", null);
  65. assertThat(result, notNullValue());
  66. assertThat(result, instanceOf(TextFieldMapper.TextFieldType.class));
  67. assertThat(result.name(), equalTo("name"));
  68. }
  69. public void testToQueryFails() {
  70. QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
  71. Exception exc = expectThrows(Exception.class,
  72. () -> context.toQuery(new AbstractQueryBuilder() {
  73. @Override
  74. public String getWriteableName() {
  75. return null;
  76. }
  77. @Override
  78. protected void doWriteTo(StreamOutput out) throws IOException {
  79. }
  80. @Override
  81. protected void doXContent(XContentBuilder builder, Params params) throws IOException {
  82. }
  83. @Override
  84. protected Query doToQuery(QueryShardContext context) throws IOException {
  85. throw new RuntimeException("boom");
  86. }
  87. @Override
  88. protected boolean doEquals(AbstractQueryBuilder other) {
  89. return false;
  90. }
  91. @Override
  92. protected int doHashCode() {
  93. return 0;
  94. }
  95. }));
  96. assertThat(exc.getMessage(), equalTo("failed to create query: boom"));
  97. }
  98. public void testClusterAlias() throws IOException {
  99. final String clusterAlias = randomBoolean() ? null : "remote_cluster";
  100. QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, clusterAlias);
  101. IndexFieldMapper mapper = new IndexFieldMapper();
  102. IndexFieldData<?> forField = context.getForField(mapper.fieldType());
  103. String expected = clusterAlias == null ? context.getIndexSettings().getIndexMetadata().getIndex().getName()
  104. : clusterAlias + ":" + context.getIndexSettings().getIndex().getName();
  105. assertEquals(expected, ((AbstractLeafOrdinalsFieldData)forField.load(null)).getOrdinalsValues().lookupOrd(0).utf8ToString());
  106. }
  107. public void testGetFullyQualifiedIndex() {
  108. String clusterAlias = randomAlphaOfLengthBetween(5, 10);
  109. String indexUuid = randomAlphaOfLengthBetween(3, 10);
  110. QueryShardContext shardContext = createQueryShardContext(indexUuid, clusterAlias);
  111. assertThat(shardContext.getFullyQualifiedIndex().getName(), equalTo(clusterAlias + ":index"));
  112. assertThat(shardContext.getFullyQualifiedIndex().getUUID(), equalTo(indexUuid));
  113. }
  114. public void testIndexSortedOnField() {
  115. Settings settings = Settings.builder()
  116. .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
  117. .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
  118. .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
  119. .put("index.sort.field", "sort_field")
  120. .build();
  121. IndexMetadata indexMetadata = new IndexMetadata.Builder("index")
  122. .settings(settings)
  123. .build();
  124. IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
  125. QueryShardContext context = new QueryShardContext(
  126. 0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null,
  127. null, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()),
  128. null, null, () -> 0L, null, null, () -> true, null);
  129. assertTrue(context.indexSortedOnField("sort_field"));
  130. assertFalse(context.indexSortedOnField("second_sort_field"));
  131. assertFalse(context.indexSortedOnField("non_sort_field"));
  132. }
  133. public static QueryShardContext createQueryShardContext(String indexUuid, String clusterAlias) {
  134. IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder("index");
  135. indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT)
  136. .put("index.number_of_shards", 1)
  137. .put("index.number_of_replicas", 1)
  138. .put(IndexMetadata.SETTING_INDEX_UUID, indexUuid)
  139. );
  140. IndexMetadata indexMetadata = indexMetadataBuilder.build();
  141. IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
  142. MapperService mapperService = mock(MapperService.class);
  143. when(mapperService.getIndexSettings()).thenReturn(indexSettings);
  144. when(mapperService.index()).thenReturn(indexMetadata.getIndex());
  145. final long nowInMillis = randomNonNegativeLong();
  146. return new QueryShardContext(
  147. 0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null,
  148. (mappedFieldType, idxName) -> mappedFieldType.fielddataBuilder(idxName).build(null, null, null),
  149. mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()),
  150. null, null, () -> nowInMillis, clusterAlias, null, () -> true, null);
  151. }
  152. }