Esempio n. 1
0
        /// <summary>
        ///     Create a new index for the parent document
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="mappingDefinition"> mapping definitions for the index type </param>
        private void IndexCreateCreatePropertyMappingForEntityForParentDocument(EntityContextInfo entityInfo,
                                                                                ElasticMapping elasticMapping, MappingDefinition mappingDefinition, ElasticJsonWriter elasticCrudJsonWriter)
        {
            var itemType = elasticMapping.GetDocumentType(entityInfo.EntityType);

            if (_processedItems.Contains("_mapping" + itemType))
            {
                return;
            }
            _processedItems.Add("_mapping" + itemType);

            //elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticCrudJsonWriter.JsonWriter.WritePropertyName("mappings");
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

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

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

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

            if (entityInfo.RoutingDefinition.ParentId != null)
            {
                CreateParentMappingForDocument(
                    elasticCrudJsonWriter,
                    elasticMapping.GetDocumentType(entityInfo.ParentEntityType));
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, entityInfo, elasticMapping);
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            if (_elasticSerializerConfiguration.ProcessChildDocumentsAsSeparateChildIndex)
            {
                if (elasticMapping.ChildIndexEntities.Count > 0)
                {
                    elasticMapping.SaveChildObjectsAsWellAsParent = false;
                    foreach (var item in elasticMapping.ChildIndexEntities)
                    {
                        IndexCreateCreatePropertyMappingForChildDocument(elasticCrudJsonWriter, entityInfo,
                                                                         elasticMapping, item, mappingDefinition);
                    }
                }
            }

            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            //elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            //CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(), mappingDefinition.Index, itemType);
        }
Esempio n. 2
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");
        }
        public static void GetDocumentType(bool camelCaseTypeNames, bool pluralizeTypeNames, Type type, string expected)
        {
            var mapping = new ElasticMapping(camelCaseTypeNames: camelCaseTypeNames, pluralizeTypeNames: pluralizeTypeNames);

            var actual = mapping.GetDocumentType(type);

            Assert.Equal(expected, actual);
        }
Esempio n. 4
0
        /// <summary>
        ///     Create a new index for the parent document
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="mappingDefinition"> mapping definitions for the index type </param>
        private void CreatePropertyMappingForEntityForParentDocument(EntityContextInfo entityInfo,
                                                                     ElasticMapping elasticMapping, MappingDefinition mappingDefinition)
        {
            var itemType = elasticMapping.GetDocumentType(entityInfo.EntityType);

            if (_processedItems.Contains("_mapping" + itemType))
            {
                return;
            }
            _processedItems.Add("_mapping" + itemType);

            var elasticCrudJsonWriter = new ElasticJsonWriter();

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

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

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

            if (entityInfo.RoutingDefinition.ParentId != null)
            {
                CreateParentMappingForDocument(
                    elasticCrudJsonWriter,
                    elasticMapping.GetDocumentType(entityInfo.ParentEntityType));
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, entityInfo, elasticMapping);

            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(), mappingDefinition.Index,
                                                         itemType);
        }
Esempio n. 5
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));
        }
        public static void GetDocumentType_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.GetDocumentType(null));
        }