forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
085751e
commit 03423e3
Showing
2 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ugin/ml/src/test/java/org/elasticsearch/xpack/ml/mapper/SemanticTextFieldMapperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.mapper; | ||
|
||
import org.apache.lucene.index.IndexableField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.mapper.DocumentMapper; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.MapperParsingException; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.index.mapper.MapperTestCase; | ||
import org.elasticsearch.index.mapper.ParsedDocument; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.ml.MachineLearning; | ||
import org.junit.AssumptionViolatedException; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class SemanticTextFieldMapperTests extends MapperTestCase { | ||
|
||
public void testDefaults() throws Exception { | ||
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); | ||
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString()); | ||
|
||
ParsedDocument doc1 = mapper.parse(source(this::writeField)); | ||
List<IndexableField> fields = doc1.rootDoc().getFields("field"); | ||
|
||
// No indexable fields | ||
assertTrue(fields.isEmpty()); | ||
} | ||
|
||
public void testModelIdNotPresent() throws IOException { | ||
Exception e = expectThrows( | ||
MapperParsingException.class, | ||
() -> createMapperService(fieldMapping(b -> b.field("type", "semantic_text"))) | ||
); | ||
assertThat(e.getMessage(), containsString("field [model_id] must be specified")); | ||
} | ||
|
||
public void testCannotBeUsedInMultiFields() { | ||
Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> { | ||
b.field("type", "text"); | ||
b.startObject("fields"); | ||
b.startObject("semantic"); | ||
b.field("type", "semantic_text"); | ||
b.endObject(); | ||
b.endObject(); | ||
}))); | ||
assertThat(e.getMessage(), containsString("Field [semantic] of type [semantic_text] can't be used in multifields")); | ||
} | ||
|
||
public void testUpdatesToModelIdNotSupported() throws IOException { | ||
MapperService mapperService = createMapperService( | ||
fieldMapping(b -> b.field("type", "semantic_text").field("model_id", "test_model")) | ||
); | ||
Exception e = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> merge( | ||
mapperService, | ||
fieldMapping( | ||
b -> b.field("type", "semantic_text") | ||
.field("model_id", "another_model") | ||
) | ||
) | ||
); | ||
assertThat(e.getMessage(), containsString("Cannot update parameter [model_id] from [test_model] to [another_model]")); | ||
} | ||
|
||
@Override | ||
protected Collection<? extends Plugin> getPlugins() { | ||
return singletonList(new MachineLearning(Settings.EMPTY)); | ||
} | ||
|
||
|
||
@Override | ||
protected void minimalMapping(XContentBuilder b) throws IOException { | ||
b.field("type", "semantic_text").field("model_id", "test_model"); | ||
} | ||
|
||
@Override | ||
protected Object getSampleValueForDocument() { | ||
return "value"; | ||
} | ||
|
||
@Override | ||
protected boolean supportsIgnoreMalformed() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean supportsStoredFields() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void registerParameters(ParameterChecker checker) throws IOException { | ||
} | ||
|
||
@Override | ||
protected Object generateRandomInputValue(MappedFieldType ft) { | ||
assumeFalse("doc_values are not supported in semantic_text", true); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed) { | ||
throw new AssumptionViolatedException("not supported"); | ||
} | ||
|
||
@Override | ||
protected IngestScriptSupport ingestScriptSupport() { | ||
throw new AssumptionViolatedException("not supported"); | ||
} | ||
} |