Ejemplo n.º 1
0
        private void CreateBulkContentForParentDocument(EntityContextInfo entityInfo, ElasticMapping elasticMapping)
        {
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            _elasticCrudJsonWriter.JsonWriter.WritePropertyName("index");
            // Write the batch "index" operation header
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            WriteValue("_index", elasticMapping.GetIndexForType(entityInfo.EntityType));
            WriteValue("_type", elasticMapping.GetDocumentType(entityInfo.EntityType));
            WriteValue("_id", entityInfo.Id);
            if (entityInfo.RoutingDefinition.ParentId != null &&
                _elasticSerializerConfiguration.ProcessChildDocumentsAsSeparateChildIndex)
            {
                WriteValue("_parent", entityInfo.RoutingDefinition.ParentId);
            }
            if (entityInfo.RoutingDefinition.RoutingId != null &&
                _elasticSerializerConfiguration.UserDefinedRouting)
            {
                WriteValue("_routing", entityInfo.RoutingDefinition.RoutingId);
            }
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n"); //ES requires this \n separator
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticMapping.MapEntityValues(entityInfo, _elasticCrudJsonWriter, true);

            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Create a new mapping for the child type in the parent document index
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="item">             </param>
        /// <param name="mappingDefinition"> definition for the type mappings </param>
        private void CreatePropertyMappingForChildDocument(EntityContextInfo entityInfo, ElasticMapping elasticMapping,
                                                           EntityContextInfo item, MappingDefinition mappingDefinition)
        {
            var childMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(item.EntityType);

            var parentMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(item.ParentEntityType);

            var childType  = childMapping.GetDocumentType(item.EntityType);
            var parentType = parentMapping.GetDocumentType(item.ParentEntityType);

            var processedId = childType + "_" + parentType;

            if (_processedItems.Contains(childType))
            {
                var test = CommandTypes.Find(t => t.StartsWith(childType));
                if (test != processedId)
                {
                    throw new ElasticException("InitMappings: Not supported, child documents can only have one parent");
                }
                return;
            }
            _processedItems.Add(childType);
            CommandTypes.Add(processedId);

            var elasticCrudJsonWriter = new ElasticJsonWriter();

            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticCrudJsonWriter.JsonWriter.WritePropertyName(childType);
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            mappingDefinition.Source.WriteJson(elasticCrudJsonWriter);
            mappingDefinition.All.WriteJson(elasticCrudJsonWriter);

            CreateParentMappingForDocument(
                elasticCrudJsonWriter,
                elasticMapping.GetDocumentType(item.ParentEntityType));

            if (item.RoutingDefinition.RoutingId != null && _elasticSerializerConfiguration.UserDefinedRouting)
            {
                CreateForceRoutingMappingForDocument(elasticCrudJsonWriter);
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, item, childMapping);
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(),
                                                         elasticMapping.GetIndexForType(entityInfo.EntityType), childMapping.GetDocumentType(item.EntityType));
        }