Search in sources :

Example 1 with MapBuilder

use of org.elasticsearch.common.collect.MapBuilder in project elasticsearch by elastic.

the class TransportGetFieldMappingsIndexAction method findFieldMappingsByType.

private Map<String, FieldMappingMetaData> findFieldMappingsByType(DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {
    MapBuilder<String, FieldMappingMetaData> fieldMappings = new MapBuilder<>();
    final DocumentFieldMappers allFieldMappers = documentMapper.mappers();
    for (String field : request.fields()) {
        if (Regex.isMatchAllPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
            }
        } else if (Regex.isSimpleMatchPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                if (Regex.simpleMatch(field, fieldMapper.fieldType().name())) {
                    addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
                }
            }
        } else {
            // not a pattern
            FieldMapper fieldMapper = allFieldMappers.smartNameFieldMapper(field);
            if (fieldMapper != null) {
                addFieldMapper(field, fieldMapper, fieldMappings, request.includeDefaults());
            } else if (request.probablySingleFieldRequest()) {
                fieldMappings.put(field, FieldMappingMetaData.NULL);
            }
        }
    }
    return fieldMappings.immutableMap();
}
Also used : FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) MapBuilder(org.elasticsearch.common.collect.MapBuilder) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 2 with MapBuilder

use of org.elasticsearch.common.collect.MapBuilder in project crate by crate.

the class TransportShardUpsertActionTest method testGeneratedColumnsValidationWorksForArrayColumns.

@Test
public void testGeneratedColumnsValidationWorksForArrayColumns() throws Exception {
    // test no exception are thrown when validating array generated columns
    Map<String, Object> updatedColumns = MapBuilder.<String, Object>newMapBuilder().put("obj", MapBuilder.<String, Object>newMapBuilder().put("arr", new Object[] { 1 }).map()).map();
    Map<String, Object> updatedGeneratedColumns = MapBuilder.<String, Object>newMapBuilder().put("arr", new Object[] { 1 }).map();
    DocTableInfo docTableInfo = new TestingTableInfo.Builder(new TableIdent(null, "generated_column"), new Routing(Collections.<String, Map<String, List<Integer>>>emptyMap())).add("obj", DataTypes.OBJECT, null).add("obj", new ArrayType(DataTypes.INTEGER), Arrays.asList("arr")).addGeneratedColumn("arr", new ArrayType(DataTypes.INTEGER), "obj['arr']", false).build(getFunctions());
    transportShardUpsertAction.processGeneratedColumns(docTableInfo, updatedColumns, updatedGeneratedColumns, false);
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) MapBuilder(org.elasticsearch.common.collect.MapBuilder) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with MapBuilder

use of org.elasticsearch.common.collect.MapBuilder in project bboss-elastic by bbossgroups.

the class ElasticSearchLogStashEventSerializer method appendHeaders.

private void appendHeaders(XContentBuilder builder, Event event) throws IOException {
    // Map<String, String> headers = Maps.newHashMap(event.getHeaders());
    MapBuilder mapBuilder = MapBuilder.newMapBuilder();
    // mapBuilder.putAll(event.getHeaders());
    // Map<String, String> headers = mapBuilder.map();
    Map<String, String> headers = event.getHeaders();
    if (headers == null || headers.size() == 0) {
        return;
    }
    String timestamp = headers.remove("timestamp");
    if (!SimpleStringUtil.isEmpty(timestamp) && SimpleStringUtil.isEmpty(headers.get("@timestamp"))) {
        long timestampMs = Long.parseLong(timestamp);
        builder.field("@timestamp", new Date(timestampMs));
    }
    String source = headers.remove("source");
    if (!SimpleStringUtil.isEmpty(source) && SimpleStringUtil.isEmpty(headers.get("@source"))) {
        ContentBuilderUtil.appendField(builder, "@source", source.getBytes(charset));
    }
    String type = headers.remove("type");
    if (!SimpleStringUtil.isEmpty(type) && SimpleStringUtil.isEmpty(headers.get("@type"))) {
        ContentBuilderUtil.appendField(builder, "@type", type.getBytes(charset));
    }
    String host = headers.remove("host");
    if (!SimpleStringUtil.isEmpty(host) && SimpleStringUtil.isEmpty(headers.get("@source_host"))) {
        ContentBuilderUtil.appendField(builder, "@source_host", host.getBytes(charset));
    }
    String srcPath = headers.remove("src_path");
    if (!SimpleStringUtil.isEmpty(srcPath) && SimpleStringUtil.isEmpty(headers.get("@source_path"))) {
        ContentBuilderUtil.appendField(builder, "@source_path", srcPath.getBytes(charset));
    }
    builder.startObject("@fields");
    for (String key : headers.keySet()) {
        byte[] val = headers.get(key).getBytes(charset);
        ContentBuilderUtil.appendField(builder, key, val);
    }
    builder.endObject();
}
Also used : MapBuilder(org.elasticsearch.common.collect.MapBuilder) Date(java.util.Date)

Example 4 with MapBuilder

use of org.elasticsearch.common.collect.MapBuilder in project elasticsearch by elastic.

the class TransportGetFieldMappingsIndexAction method shardOperation.

@Override
protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexRequest request, ShardId shardId) {
    assert shardId != null;
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    Collection<String> typeIntersection;
    if (request.types().length == 0) {
        typeIntersection = indexService.mapperService().types();
    } else {
        typeIntersection = indexService.mapperService().types().stream().filter(type -> Regex.simpleMatch(request.types(), type)).collect(Collectors.toCollection(ArrayList::new));
        if (typeIntersection.isEmpty()) {
            throw new TypeMissingException(shardId.getIndex(), request.types());
        }
    }
    MapBuilder<String, Map<String, FieldMappingMetaData>> typeMappings = new MapBuilder<>();
    for (String type : typeIntersection) {
        DocumentMapper documentMapper = indexService.mapperService().documentMapper(type);
        Map<String, FieldMappingMetaData> fieldMapping = findFieldMappingsByType(documentMapper, request);
        if (!fieldMapping.isEmpty()) {
            typeMappings.put(type, fieldMapping);
        }
    }
    return new GetFieldMappingsResponse(singletonMap(shardId.getIndexName(), typeMappings.immutableMap()));
}
Also used : IndexService(org.elasticsearch.index.IndexService) FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) TypeMissingException(org.elasticsearch.indices.TypeMissingException) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ArrayList(java.util.ArrayList) MapBuilder(org.elasticsearch.common.collect.MapBuilder) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 5 with MapBuilder

use of org.elasticsearch.common.collect.MapBuilder in project bboss-elastic by bbossgroups.

the class ElasticSearchJSONEventSerializer method appendHeaders.

private void appendHeaders(Map<String, Object> builder, Event event) throws IOException {
    // Map<String, String> headers = Maps.newHashMap(event.getHeaders());
    MapBuilder mapBuilder = MapBuilder.newMapBuilder();
    // mapBuilder.putAll(event.getHeaders());
    // Map<String, String> headers = mapBuilder.map();
    Map<String, String> headers = event.getHeaders();
    if (headers == null || headers.size() == 0) {
        return;
    }
    String timestamp = headers.remove("timestamp");
    if (!SimpleStringUtil.isEmpty(timestamp) && SimpleStringUtil.isEmpty(headers.get("@timestamp"))) {
        long timestampMs = Long.parseLong(timestamp);
        builder.put("@timestamp", new Date(timestampMs));
    }
    String source = headers.remove("source");
    if (!SimpleStringUtil.isEmpty(source) && SimpleStringUtil.isEmpty(headers.get("@source"))) {
        builder.put("@source", source);
    }
    String type = headers.remove("type");
    if (!SimpleStringUtil.isEmpty(type) && SimpleStringUtil.isEmpty(headers.get("@type"))) {
        builder.put("@type", type);
    }
    String host = headers.remove("host");
    if (!SimpleStringUtil.isEmpty(host) && SimpleStringUtil.isEmpty(headers.get("@source_host"))) {
        builder.put("beat.host", host);
    }
    String srcPath = headers.remove("src_path");
    if (!SimpleStringUtil.isEmpty(srcPath) && SimpleStringUtil.isEmpty(headers.get("@source_path"))) {
        builder.put("@source_path", srcPath);
    }
    for (String key : headers.keySet()) {
        String val = headers.get(key);
        builder.put(key, val);
    }
}
Also used : MapBuilder(org.elasticsearch.common.collect.MapBuilder) Date(java.util.Date)

Aggregations

MapBuilder (org.elasticsearch.common.collect.MapBuilder)5 Date (java.util.Date)2 FieldMappingMetaData (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1 ArrayType (io.crate.types.ArrayType)1 ArrayList (java.util.ArrayList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Map (java.util.Map)1 IndexService (org.elasticsearch.index.IndexService)1 DocumentFieldMappers (org.elasticsearch.index.mapper.DocumentFieldMappers)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 TypeMissingException (org.elasticsearch.indices.TypeMissingException)1 Test (org.junit.Test)1