123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * Licensed to Elasticsearch under one or more contributor
- * license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright
- * ownership. Elasticsearch licenses this file to you under
- * the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- package org.elasticsearch.index.query;
- import org.apache.lucene.search.Query;
- import org.elasticsearch.Version;
- import org.elasticsearch.cluster.metadata.IndexMetadata;
- import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
- import org.elasticsearch.common.io.stream.StreamOutput;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.util.BigArrays;
- import org.elasticsearch.common.xcontent.NamedXContentRegistry;
- import org.elasticsearch.common.xcontent.XContentBuilder;
- import org.elasticsearch.index.IndexSettings;
- import org.elasticsearch.index.fielddata.IndexFieldData;
- import org.elasticsearch.index.fielddata.plain.AbstractLeafOrdinalsFieldData;
- import org.elasticsearch.index.mapper.IndexFieldMapper;
- import org.elasticsearch.index.mapper.MappedFieldType;
- import org.elasticsearch.index.mapper.MapperService;
- import org.elasticsearch.index.mapper.TextFieldMapper;
- import org.elasticsearch.test.ESTestCase;
- import java.io.IOException;
- import java.util.Collections;
- import static org.hamcrest.Matchers.equalTo;
- import static org.hamcrest.Matchers.instanceOf;
- import static org.hamcrest.Matchers.notNullValue;
- import static org.hamcrest.Matchers.nullValue;
- import static org.hamcrest.Matchers.sameInstance;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.when;
- public class QueryShardContextTests extends ESTestCase {
- public void testFailIfFieldMappingNotFound() {
- QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
- context.setAllowUnmappedFields(false);
- MappedFieldType fieldType = new TextFieldMapper.TextFieldType("text");
- MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType);
- assertThat(result, sameInstance(fieldType));
- QueryShardException e = expectThrows(QueryShardException.class, () -> context.failIfFieldMappingNotFound("name", null));
- assertEquals("No field mapping can be found for the field with name [name]", e.getMessage());
- context.setAllowUnmappedFields(true);
- result = context.failIfFieldMappingNotFound("name", fieldType);
- assertThat(result, sameInstance(fieldType));
- result = context.failIfFieldMappingNotFound("name", null);
- assertThat(result, nullValue());
- context.setAllowUnmappedFields(false);
- context.setMapUnmappedFieldAsString(true);
- result = context.failIfFieldMappingNotFound("name", fieldType);
- assertThat(result, sameInstance(fieldType));
- result = context.failIfFieldMappingNotFound("name", null);
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(TextFieldMapper.TextFieldType.class));
- assertThat(result.name(), equalTo("name"));
- }
- public void testToQueryFails() {
- QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
- Exception exc = expectThrows(Exception.class,
- () -> context.toQuery(new AbstractQueryBuilder() {
- @Override
- public String getWriteableName() {
- return null;
- }
- @Override
- protected void doWriteTo(StreamOutput out) throws IOException {
- }
- @Override
- protected void doXContent(XContentBuilder builder, Params params) throws IOException {
- }
- @Override
- protected Query doToQuery(QueryShardContext context) throws IOException {
- throw new RuntimeException("boom");
- }
- @Override
- protected boolean doEquals(AbstractQueryBuilder other) {
- return false;
- }
- @Override
- protected int doHashCode() {
- return 0;
- }
- }));
- assertThat(exc.getMessage(), equalTo("failed to create query: boom"));
- }
- public void testClusterAlias() throws IOException {
- final String clusterAlias = randomBoolean() ? null : "remote_cluster";
- QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, clusterAlias);
- IndexFieldMapper mapper = new IndexFieldMapper();
- IndexFieldData<?> forField = context.getForField(mapper.fieldType());
- String expected = clusterAlias == null ? context.getIndexSettings().getIndexMetadata().getIndex().getName()
- : clusterAlias + ":" + context.getIndexSettings().getIndex().getName();
- assertEquals(expected, ((AbstractLeafOrdinalsFieldData)forField.load(null)).getOrdinalsValues().lookupOrd(0).utf8ToString());
- }
- public void testGetFullyQualifiedIndex() {
- String clusterAlias = randomAlphaOfLengthBetween(5, 10);
- String indexUuid = randomAlphaOfLengthBetween(3, 10);
- QueryShardContext shardContext = createQueryShardContext(indexUuid, clusterAlias);
- assertThat(shardContext.getFullyQualifiedIndex().getName(), equalTo(clusterAlias + ":index"));
- assertThat(shardContext.getFullyQualifiedIndex().getUUID(), equalTo(indexUuid));
- }
- public void testIndexSortedOnField() {
- Settings settings = Settings.builder()
- .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
- .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
- .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
- .put("index.sort.field", "sort_field")
- .build();
- IndexMetadata indexMetadata = new IndexMetadata.Builder("index")
- .settings(settings)
- .build();
- IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
- QueryShardContext context = new QueryShardContext(
- 0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null,
- null, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()),
- null, null, () -> 0L, null, null, () -> true, null);
- assertTrue(context.indexSortedOnField("sort_field"));
- assertFalse(context.indexSortedOnField("second_sort_field"));
- assertFalse(context.indexSortedOnField("non_sort_field"));
- }
- public static QueryShardContext createQueryShardContext(String indexUuid, String clusterAlias) {
- IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder("index");
- indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT)
- .put("index.number_of_shards", 1)
- .put("index.number_of_replicas", 1)
- .put(IndexMetadata.SETTING_INDEX_UUID, indexUuid)
- );
- IndexMetadata indexMetadata = indexMetadataBuilder.build();
- IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
- MapperService mapperService = mock(MapperService.class);
- when(mapperService.getIndexSettings()).thenReturn(indexSettings);
- when(mapperService.index()).thenReturn(indexMetadata.getIndex());
- final long nowInMillis = randomNonNegativeLong();
- return new QueryShardContext(
- 0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null,
- (mappedFieldType, idxName) -> mappedFieldType.fielddataBuilder(idxName).build(null, null, null),
- mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()),
- null, null, () -> nowInMillis, clusterAlias, null, () -> true, null);
- }
- }
|