Search in sources :

Example 11 with DocumentMapper

use of org.opensearch.index.mapper.DocumentMapper in project OpenSearch by opensearch-project.

the class ParentJoinFieldMapperTests method testParentIdSpecifiedAsNumber.

public void testParentIdSpecifiedAsNumber() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("join_field").field("type", "join").startObject("relations").field("parent", "child").endObject().endObject().endObject().endObject());
    IndexService service = createIndex("test");
    DocumentMapper docMapper = service.mapperService().merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", 1).endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    doc = docMapper.parse(new SourceToParse("test", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", 1.0).endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("1.0", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
}
Also used : ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 12 with DocumentMapper

use of org.opensearch.index.mapper.DocumentMapper in project OpenSearch by opensearch-project.

the class ParentJoinFieldMapperTests method testMultipleLevels.

public void testMultipleLevels() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("join_field").field("type", "join").startObject("relations").field("parent", "child").field("child", "grand_child").endObject().endObject().endObject().endObject());
    IndexService service = createIndex("test");
    DocumentMapper docMapper = service.mapperService().merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
    assertTrue(docMapper.mappers().getMapper("join_field") == ParentJoinFieldMapper.getMapper(service.mapperService()));
    // Doc without join
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "0", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON));
    assertNull(doc.rootDoc().getBinaryValue("join_field"));
    // Doc parent
    doc = docMapper.parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "parent").endObject()), XContentType.JSON));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("parent", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Doc child
    doc = docMapper.parse(new SourceToParse("test", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", "1").endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("2", doc.rootDoc().getBinaryValue("join_field#child").utf8ToString());
    assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Doc child missing parent
    MapperException exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "child").endObject()), XContentType.JSON, "1")));
    assertThat(exc.getRootCause().getMessage(), containsString("[parent] is missing for join field [join_field]"));
    // Doc child missing routing
    exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", "1").endObject().endObject()), XContentType.JSON)));
    assertThat(exc.getRootCause().getMessage(), containsString("[routing] is missing for join field [join_field]"));
    // Doc grand_child
    doc = docMapper.parse(new SourceToParse("test", "3", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "grand_child").field("parent", "2").endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("2", doc.rootDoc().getBinaryValue("join_field#child").utf8ToString());
    assertEquals("grand_child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Unknown join name
    exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "unknown").endObject()), XContentType.JSON)));
    assertThat(exc.getRootCause().getMessage(), containsString("unknown join name [unknown] for field [join_field]"));
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) MapperException(org.opensearch.index.mapper.MapperException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 13 with DocumentMapper

use of org.opensearch.index.mapper.DocumentMapper in project OpenSearch by opensearch-project.

the class Murmur3FieldMapperTests method testDefaults.

public void testDefaults() throws Exception {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
    ParsedDocument parsedDoc = mapper.parse(source(b -> b.field("field", "value")));
    IndexableField[] fields = parsedDoc.rootDoc().getFields("field");
    assertNotNull(fields);
    assertEquals(Arrays.toString(fields), 1, fields.length);
    IndexableField field = fields[0];
    assertEquals(IndexOptions.NONE, field.fieldType().indexOptions());
    assertEquals(DocValuesType.SORTED_NUMERIC, field.fieldType().docValuesType());
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Arrays(java.util.Arrays) DocValuesType(org.apache.lucene.index.DocValuesType) IndexableField(org.apache.lucene.index.IndexableField) MapperTestCase(org.opensearch.index.mapper.MapperTestCase) Collection(java.util.Collection) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) MapperMurmur3Plugin(org.opensearch.plugin.mapper.MapperMurmur3Plugin) IOException(java.io.IOException) IndexOptions(org.apache.lucene.index.IndexOptions) Plugin(org.opensearch.plugins.Plugin) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) DocumentMapper(org.opensearch.index.mapper.DocumentMapper)

Example 14 with DocumentMapper

use of org.opensearch.index.mapper.DocumentMapper in project OpenSearch by opensearch-project.

the class SizeMappingTests method testSizeNotSet.

public void testSizeNotSet() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME);
    DocumentMapper docMapper = service.mapperService().documentMapper();
    BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject());
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "1", source, XContentType.JSON));
    assertThat(doc.rootDoc().getField("_size"), nullValue());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) SourceToParse(org.opensearch.index.mapper.SourceToParse)

Example 15 with DocumentMapper

use of org.opensearch.index.mapper.DocumentMapper in project OpenSearch by opensearch-project.

the class AnnotatedTextFieldMapperTests method testDefaults.

public void testDefaults() throws IOException {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
    ParsedDocument doc = mapper.parse(source(b -> b.field("field", "1234")));
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertEquals("1234", fields[0].stringValue());
    IndexableFieldType fieldType = fields[0].fieldType();
    assertThat(fieldType.omitNorms(), equalTo(false));
    assertTrue(fieldType.tokenized());
    assertFalse(fieldType.stored());
    assertThat(fieldType.indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS));
    assertThat(fieldType.storeTermVectors(), equalTo(false));
    assertThat(fieldType.storeTermVectorOffsets(), equalTo(false));
    assertThat(fieldType.storeTermVectorPositions(), equalTo(false));
    assertThat(fieldType.storeTermVectorPayloads(), equalTo(false));
    assertEquals(DocValuesType.NONE, fieldType.docValuesType());
}
Also used : Arrays(java.util.Arrays) IndexableField(org.apache.lucene.index.IndexableField) ToXContent(org.opensearch.common.xcontent.ToXContent) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) TokenFilterFactory(org.opensearch.index.analysis.TokenFilterFactory) HashMap(java.util.HashMap) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) MapperService(org.opensearch.index.mapper.MapperService) IndexableFieldType(org.apache.lucene.index.IndexableFieldType) TermsEnum(org.apache.lucene.index.TermsEnum) CharFilterFactory(org.opensearch.index.analysis.CharFilterFactory) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) StandardTokenizerFactory(org.opensearch.index.analysis.StandardTokenizerFactory) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CustomAnalyzer(org.opensearch.index.analysis.CustomAnalyzer) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) AnnotatedTextPlugin(org.opensearch.plugin.mapper.AnnotatedTextPlugin) StopFilter(org.apache.lucene.analysis.StopFilter) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) TokenStream(org.apache.lucene.analysis.TokenStream) BytesRef(org.apache.lucene.util.BytesRef) MapperTestCase(org.opensearch.index.mapper.MapperTestCase) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Plugin(org.opensearch.plugins.Plugin) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) DocValuesType(org.apache.lucene.index.DocValuesType) Matchers.equalTo(org.hamcrest.Matchers.equalTo) LeafReader(org.apache.lucene.index.LeafReader) IndexSettings(org.opensearch.index.IndexSettings) IndexOptions(org.apache.lucene.index.IndexOptions) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexableFieldType(org.apache.lucene.index.IndexableFieldType)

Aggregations

DocumentMapper (org.opensearch.index.mapper.DocumentMapper)55 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)23 Matchers.containsString (org.hamcrest.Matchers.containsString)22 CompressedXContent (org.opensearch.common.compress.CompressedXContent)19 SourceToParse (org.opensearch.index.mapper.SourceToParse)19 IndexableField (org.apache.lucene.index.IndexableField)17 MapperService (org.opensearch.index.mapper.MapperService)17 HashMap (java.util.HashMap)14 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)14 Mapper (org.opensearch.index.mapper.Mapper)12 TextFieldMapper (org.opensearch.index.mapper.TextFieldMapper)12 IOException (java.io.IOException)11 Map (java.util.Map)11 IndexService (org.opensearch.index.IndexService)11 IndexSettings (org.opensearch.index.IndexSettings)11 Collections (java.util.Collections)8 BytesRef (org.apache.lucene.util.BytesRef)8 Arrays (java.util.Arrays)7 DocValuesType (org.apache.lucene.index.DocValuesType)7 IndexOptions (org.apache.lucene.index.IndexOptions)7