Search in sources :

Example 81 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MultiFieldTests method testObjectFieldNotAllowed.

public void testObjectFieldNotAllowed() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("my_field").field("type", "text").startObject("fields").startObject("multi").field("type", "object").endObject().endObject().endObject().endObject().endObject().endObject().string();
    final DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected mapping parse failure");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("cannot be used in multi field"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Example 82 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MultiFieldTests method testNestedFieldNotAllowed.

public void testNestedFieldNotAllowed() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("my_field").field("type", "text").startObject("fields").startObject("multi").field("type", "nested").endObject().endObject().endObject().endObject().endObject().endObject().string();
    final DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected mapping parse failure");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("cannot be used in multi field"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Example 83 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MultiFieldTests method testMultiField.

private void testMultiField(String mapping) throws Exception {
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
    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().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());
    f = doc.getField("object1.multi1");
    assertThat(f.name(), equalTo("object1.multi1"));
    f = doc.getField("object1.multi1.string");
    assertThat(f.name(), equalTo("object1.multi1.string"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("2010-01-01")));
    assertThat(docMapper.mappers().getMapper("name"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.indexed"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.indexed").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed").fieldType().stored(), equalTo(false));
    assertThat(docMapper.mappers().getMapper("name.indexed").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), instanceOf(TextFieldMapper.class));
    assertEquals(IndexOptions.NONE, docMapper.mappers().getMapper("name.not_indexed").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.not_indexed").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.not_indexed").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.test1"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.test1").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().eagerGlobalOrdinals(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.test2"), instanceOf(TokenCountFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.test2").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.test2").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test2").fieldType().tokenized(), equalTo(false));
    assertThat(((TokenCountFieldMapper) docMapper.mappers().getMapper("name.test2")).analyzer(), equalTo("simple"));
    assertThat(((TokenCountFieldMapper) docMapper.mappers().getMapper("name.test2")).analyzer(), equalTo("simple"));
    assertThat(docMapper.mappers().getMapper("object1.multi1"), notNullValue());
    assertThat(docMapper.mappers().getMapper("object1.multi1"), instanceOf(DateFieldMapper.class));
    assertThat(docMapper.mappers().getMapper("object1.multi1.string"), notNullValue());
    assertThat(docMapper.mappers().getMapper("object1.multi1.string"), instanceOf(KeywordFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("object1.multi1.string").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("object1.multi1.string").fieldType().tokenized(), equalTo(false));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) BytesArray(org.elasticsearch.common.bytes.BytesArray) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) TokenCountFieldMapper(org.elasticsearch.index.mapper.TokenCountFieldMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) BytesRef(org.apache.lucene.util.BytesRef) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Example 84 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MultiFieldTests method testMultiFieldsInConsistentOrder.

// The underlying order of the fields in multi fields in the mapping source should always be consistent, if not this
// can to unnecessary re-syncing of the mappings between the local instance and cluster state
public void testMultiFieldsInConsistentOrder() throws Exception {
    String[] multiFieldNames = new String[randomIntBetween(2, 10)];
    for (int i = 0; i < multiFieldNames.length; i++) {
        multiFieldNames[i] = randomAsciiOfLength(4);
    }
    XContentBuilder builder = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("my_field").field("type", "text").startObject("fields");
    for (String multiFieldName : multiFieldNames) {
        builder = builder.startObject(multiFieldName).field("type", "text").endObject();
    }
    builder = builder.endObject().endObject().endObject().endObject().endObject();
    String mapping = builder.string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    Arrays.sort(multiFieldNames);
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(docMapper.mappingSource().compressedReference(), true, builder.contentType()).v2();
    @SuppressWarnings("unchecked") Map<String, Object> multiFields = (Map<String, Object>) XContentMapValues.extractValue("type.properties.my_field.fields", sourceAsMap);
    assertThat(multiFields.size(), equalTo(multiFieldNames.length));
    int i = 0;
    // underlying map is LinkedHashMap, so this ok:
    for (String field : multiFields.keySet()) {
        assertThat(field, equalTo(multiFieldNames[i++]));
    }
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 85 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MultiFieldTests method testMultiFieldWithDot.

public void testMultiFieldWithDot() throws IOException {
    XContentBuilder mapping = jsonBuilder();
    mapping.startObject().startObject("my_type").startObject("properties").startObject("city").field("type", "text").startObject("fields").startObject("raw.foo").field("type", "text").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().endObject();
    MapperService mapperService = createIndex("test").mapperService();
    try {
        mapperService.documentMapperParser().parse("my_type", new CompressedXContent(mapping.string()));
        fail("this should throw an exception because one field contains a dot");
    } catch (MapperParsingException e) {
        assertThat(e.getMessage(), equalTo("Field name [raw.foo] which is a multi field of [city] cannot contain '.'"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MapperService(org.elasticsearch.index.mapper.MapperService)

Aggregations

CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)366 Matchers.containsString (org.hamcrest.Matchers.containsString)223 IndexableField (org.apache.lucene.index.IndexableField)75 BytesReference (org.elasticsearch.common.bytes.BytesReference)62 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)51 IndexService (org.elasticsearch.index.IndexService)49 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)46 Settings (org.elasticsearch.common.settings.Settings)25 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)19 BytesRef (org.apache.lucene.util.BytesRef)18 BytesArray (org.elasticsearch.common.bytes.BytesArray)16 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)16 IOException (java.io.IOException)15 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)15 Map (java.util.Map)13 Document (org.elasticsearch.index.mapper.ParseContext.Document)13 MapperService (org.elasticsearch.index.mapper.MapperService)12 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)9 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)9