Search in sources :

Example 21 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class MultiFieldTests method testBuildThenParse.

public void testBuildThenParse() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapper builderDocMapper = new DocumentMapper.Builder(new RootObjectMapper.Builder("person").add(new TextFieldMapper.Builder("name").store(true).addMultiField(new TextFieldMapper.Builder("indexed").index(true).tokenized(true)).addMultiField(new TextFieldMapper.Builder("not_indexed").index(false).store(true))), indexService.mapperService()).build(indexService.mapperService());
    String builtMapping = builderDocMapper.mappingSource().string();
    //        System.out.println(builtMapping);
    // reparse it
    DocumentMapper docMapper = indexService.mapperService().documentMapperParser().parse("person", new CompressedXContent(builtMapping));
    BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/test-data.json"));
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    f = doc.getField("name.indexed");
    assertThat(f.name(), equalTo("name.indexed"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().tokenized(), equalTo(true));
    assertThat(f.fieldType().stored(), equalTo(false));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    f = doc.getField("name.not_indexed");
    assertThat(f.name(), equalTo("name.not_indexed"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    assertEquals(IndexOptions.NONE, f.fieldType().indexOptions());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) RootObjectMapper(org.elasticsearch.index.mapper.RootObjectMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) Document(org.elasticsearch.index.mapper.ParseContext.Document) IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Example 22 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class PathMatchDynamicTemplateTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json");
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", new BytesArray(json));
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("top_level"));
    assertThat(f.fieldType().stored(), equalTo(false));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(false));
    f = doc.getField("obj1.name");
    assertThat(f.name(), equalTo("obj1.name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("obj1.name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(true));
    f = doc.getField("obj1.obj2.name");
    assertThat(f.name(), equalTo("obj1.obj2.name"));
    assertThat(f.fieldType().stored(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("obj1.obj2.name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(false));
    // verify more complex path_match expressions
    fieldMapper = docMapper.mappers().getMapper("obj3.obj4.prop1");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 23 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class JavaMultiFieldMergeTests method testMergeMultiField.

public void testMergeMultiField() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue());
    BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes();
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping2.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    doc = docMapper.parse("test", "person", "1", json).rootDoc();
    f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, notNullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping3.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping4.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), notNullValue());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 24 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class InternalEngineTests method testDocumentWithTextField.

private static Document testDocumentWithTextField() {
    Document document = testDocument();
    document.add(new TextField("value", "test", Field.Store.YES));
    return document;
}
Also used : TextField(org.apache.lucene.document.TextField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 25 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class BooleanFieldMapperTests method testDocValues.

public void testDocValues() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("bool1").field("type", "boolean").endObject().startObject("bool2").field("type", "boolean").field("index", false).endObject().startObject("bool3").field("type", "boolean").field("index", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument parsedDoc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("bool1", true).field("bool2", true).field("bool3", true).endObject().bytes());
    Document doc = parsedDoc.rootDoc();
    IndexableField[] fields = doc.getFields("bool1");
    assertEquals(2, fields.length);
    assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
    fields = doc.getFields("bool2");
    assertEquals(1, fields.length);
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[0].fieldType().docValuesType());
    fields = doc.getFields("bool3");
    assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Aggregations

Document (org.elasticsearch.index.mapper.ParseContext.Document)25 IndexableField (org.apache.lucene.index.IndexableField)12 BytesArray (org.elasticsearch.common.bytes.BytesArray)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)10 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)8 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)7 TextField (org.apache.lucene.document.TextField)7 IndexService (org.elasticsearch.index.IndexService)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Field (org.apache.lucene.document.Field)5 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)4 LongPoint (org.apache.lucene.document.LongPoint)3 StoredField (org.apache.lucene.document.StoredField)3 Term (org.apache.lucene.index.Term)3 BytesRef (org.apache.lucene.util.BytesRef)3 Index (org.elasticsearch.index.Index)3 ArrayList (java.util.ArrayList)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2