|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.analysis.CannedTokenStream; |
| 12 | +import org.apache.lucene.analysis.Token; |
| 13 | +import org.apache.lucene.analysis.TokenStream; |
| 14 | +import org.apache.lucene.index.DocValuesType; |
| 15 | +import org.apache.lucene.index.IndexOptions; |
| 16 | +import org.apache.lucene.index.IndexableField; |
| 17 | +import org.apache.lucene.index.IndexableFieldType; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 20 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 21 | +import org.elasticsearch.index.query.SearchExecutionContext; |
| 22 | +import org.elasticsearch.plugins.Plugin; |
| 23 | +import org.hamcrest.Matchers; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.containsString; |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | +import static org.hamcrest.Matchers.instanceOf; |
| 33 | + |
| 34 | +public class MatchOnlyTextFieldMapperTests extends MapperTestCase { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Collection<Plugin> getPlugins() { |
| 38 | + return List.of(new MapperExtrasPlugin()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected Object getSampleValueForDocument() { |
| 43 | + return "value"; |
| 44 | + } |
| 45 | + |
| 46 | + public final void testExists() throws IOException { |
| 47 | + MapperService mapperService = createMapperService(fieldMapping(b -> { minimalMapping(b); })); |
| 48 | + assertExistsQuery(mapperService); |
| 49 | + assertParseMinimalWarnings(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void registerParameters(ParameterChecker checker) throws IOException { |
| 54 | + checker.registerUpdateCheck(b -> { |
| 55 | + b.field("meta", Collections.singletonMap("format", "mysql.access")); |
| 56 | + }, m -> assertEquals(Collections.singletonMap("format", "mysql.access"), m.fieldType().meta())); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void minimalMapping(XContentBuilder b) throws IOException { |
| 61 | + b.field("type", "match_only_text"); |
| 62 | + } |
| 63 | + |
| 64 | + public void testDefaults() throws IOException { |
| 65 | + DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); |
| 66 | + assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString()); |
| 67 | + |
| 68 | + ParsedDocument doc = mapper.parse(source(b -> b.field("field", "1234"))); |
| 69 | + IndexableField[] fields = doc.rootDoc().getFields("field"); |
| 70 | + assertEquals(1, fields.length); |
| 71 | + assertEquals("1234", fields[0].stringValue()); |
| 72 | + IndexableFieldType fieldType = fields[0].fieldType(); |
| 73 | + assertThat(fieldType.omitNorms(), equalTo(true)); |
| 74 | + assertTrue(fieldType.tokenized()); |
| 75 | + assertFalse(fieldType.stored()); |
| 76 | + assertThat(fieldType.indexOptions(), equalTo(IndexOptions.DOCS)); |
| 77 | + assertThat(fieldType.storeTermVectors(), equalTo(false)); |
| 78 | + assertThat(fieldType.storeTermVectorOffsets(), equalTo(false)); |
| 79 | + assertThat(fieldType.storeTermVectorPositions(), equalTo(false)); |
| 80 | + assertThat(fieldType.storeTermVectorPayloads(), equalTo(false)); |
| 81 | + assertEquals(DocValuesType.NONE, fieldType.docValuesType()); |
| 82 | + } |
| 83 | + |
| 84 | + public void testNullConfigValuesFail() throws MapperParsingException { |
| 85 | + Exception e = expectThrows( |
| 86 | + MapperParsingException.class, |
| 87 | + () -> createDocumentMapper(fieldMapping(b -> b.field("type", "match_only_text").field("meta", (String) null))) |
| 88 | + ); |
| 89 | + assertThat(e.getMessage(), containsString("[meta] on mapper [field] of type [match_only_text] must not have a [null] value")); |
| 90 | + } |
| 91 | + |
| 92 | + public void testSimpleMerge() throws IOException { |
| 93 | + XContentBuilder startingMapping = fieldMapping(b -> b.field("type", "match_only_text")); |
| 94 | + MapperService mapperService = createMapperService(startingMapping); |
| 95 | + assertThat(mapperService.documentMapper().mappers().getMapper("field"), instanceOf(MatchOnlyTextFieldMapper.class)); |
| 96 | + |
| 97 | + merge(mapperService, startingMapping); |
| 98 | + assertThat(mapperService.documentMapper().mappers().getMapper("field"), instanceOf(MatchOnlyTextFieldMapper.class)); |
| 99 | + |
| 100 | + XContentBuilder newField = mapping(b -> { |
| 101 | + b.startObject("field") |
| 102 | + .field("type", "match_only_text") |
| 103 | + .startObject("meta") |
| 104 | + .field("key", "value") |
| 105 | + .endObject() |
| 106 | + .endObject(); |
| 107 | + b.startObject("other_field").field("type", "keyword").endObject(); |
| 108 | + }); |
| 109 | + merge(mapperService, newField); |
| 110 | + assertThat(mapperService.documentMapper().mappers().getMapper("field"), instanceOf(MatchOnlyTextFieldMapper.class)); |
| 111 | + assertThat(mapperService.documentMapper().mappers().getMapper("other_field"), instanceOf(KeywordFieldMapper.class)); |
| 112 | + } |
| 113 | + |
| 114 | + public void testDisabledSource() throws IOException { |
| 115 | + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("_doc"); |
| 116 | + { |
| 117 | + mapping.startObject("properties"); |
| 118 | + { |
| 119 | + mapping.startObject("foo"); |
| 120 | + { |
| 121 | + mapping.field("type", "match_only_text"); |
| 122 | + } |
| 123 | + mapping.endObject(); |
| 124 | + } |
| 125 | + mapping.endObject(); |
| 126 | + |
| 127 | + mapping.startObject("_source"); |
| 128 | + { |
| 129 | + mapping.field("enabled", false); |
| 130 | + } |
| 131 | + mapping.endObject(); |
| 132 | + } |
| 133 | + mapping.endObject().endObject(); |
| 134 | + |
| 135 | + MapperService mapperService = createMapperService(mapping); |
| 136 | + MappedFieldType ft = mapperService.fieldType("foo"); |
| 137 | + SearchExecutionContext context = createSearchExecutionContext(mapperService); |
| 138 | + TokenStream ts = new CannedTokenStream(new Token("a", 0, 3), new Token("b", 4, 7)); |
| 139 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.phraseQuery(ts, 0, true, context)); |
| 140 | + assertThat(e.getMessage(), Matchers.containsString("cannot run positional queries since [_source] is disabled")); |
| 141 | + |
| 142 | + // Term queries are ok |
| 143 | + ft.termQuery("a", context); // no exception |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + protected Object generateRandomInputValue(MappedFieldType ft) { |
| 148 | + assumeFalse("We don't have a way to assert things here", true); |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected void randomFetchTestFieldConfig(XContentBuilder b) throws IOException { |
| 154 | + assumeFalse("We don't have a way to assert things here", true); |
| 155 | + } |
| 156 | +} |
0 commit comments