public MappingDefinition()
		{
			RoutingDefinition = new RoutingDefinition();
			All = new MappingAll();
			Source = new MappingSource();
			Analyzer = new MappingAnalyzer();
		}
Exemple #2
0
 public EntityContextInfo()
 {
     RoutingDefinition = new RoutingDefinition();
 }
        public async Task <ResultDetails <bool> > DocumentExistsAsync <T>(object entityId, RoutingDefinition routingDefinition)
        {
            var elasticSearchMapping = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(typeof(T));

            _traceProvider.Trace(TraceEventType.Verbose, "ElasticsearchContextExists: IndexExistsAsync for Type:{0}, Index: {1}, IndexType: {2}, Entity {3}",
                                 typeof(T),
                                 elasticSearchMapping.GetIndexForType(typeof(T)),
                                 elasticSearchMapping.GetDocumentType(typeof(T)),
                                 entityId
                                 );

            var elasticsearchUrlForHeadRequest = string.Format("{0}/{1}/{2}/", _connectionString,
                                                               elasticSearchMapping.GetIndexForType(typeof(T)), elasticSearchMapping.GetDocumentType(typeof(T)));

            var uri = new Uri(elasticsearchUrlForHeadRequest + entityId + RoutingDefinition.GetRoutingUrl(routingDefinition));

            return(await ExistsHeadRequest.ExistsAsync(uri));
        }
Exemple #4
0
        private void MapIEnumerableEntities(ElasticsearchCrudJsonWriter elasticsearchCrudJsonWriter, IEnumerable ienumerable, EntityContextInfo parentEntityInfo, bool createPropertyMappings)
        {
            string json = null;
            bool   isSimpleArrayOrCollection = true;
            bool   doProccessingIfTheIEnumerableHasAtLeastOneItem = false;

            if (ienumerable != null)
            {
                var sbCollection = new StringBuilder();
                sbCollection.Append("[");
                foreach (var item in ienumerable)
                {
                    doProccessingIfTheIEnumerableHasAtLeastOneItem = true;

                    var childElasticsearchCrudJsonWriter = new ElasticsearchCrudJsonWriter(sbCollection);
                    elasticsearchCrudJsonWriter.ElasticsearchCrudJsonWriterChildItem = childElasticsearchCrudJsonWriter;

                    var typeofArrayItem = item.GetType();
                    if (typeofArrayItem.GetTypeInfo().IsClass&& typeofArrayItem.FullName != "System.String" &&
                        typeofArrayItem.FullName != "System.Decimal")
                    {
                        isSimpleArrayOrCollection = false;
                        // collection of Objects
                        childElasticsearchCrudJsonWriter.JsonWriter.WriteStartObject();
                        // Do class mapping for nested type
                        var routingDefinition = new RoutingDefinition {
                            ParentId = parentEntityInfo.Id, RoutingId = parentEntityInfo.RoutingDefinition.RoutingId
                        };
                        var child = new EntityContextInfo {
                            Document = item, RoutingDefinition = routingDefinition, EntityType = item.GetType(), DeleteDocument = parentEntityInfo.DeleteDocument
                        };
                        MapEntityValues(child, childElasticsearchCrudJsonWriter, false, createPropertyMappings);
                        childElasticsearchCrudJsonWriter.JsonWriter.WriteEndObject();

                        // Add as separate document later
                    }
                    else
                    {
                        // collection of simple types, serialize all items in one go and break from the loop
                        json = JsonConvert.SerializeObject(ienumerable);

                        break;
                    }
                    sbCollection.Append(",");
                }

                if (isSimpleArrayOrCollection && doProccessingIfTheIEnumerableHasAtLeastOneItem)
                {
                    elasticsearchCrudJsonWriter.JsonWriter.WriteRawValue(json);
                }
                else
                {
                    if (doProccessingIfTheIEnumerableHasAtLeastOneItem)

                    {
                        sbCollection.Remove(sbCollection.Length - 1, 1);
                    }

                    sbCollection.Append("]");
                    elasticsearchCrudJsonWriter.JsonWriter.WriteRawValue(sbCollection.ToString());
                }
            }
            else
            {
                elasticsearchCrudJsonWriter.JsonWriter.WriteRawValue("");
            }
        }
Exemple #5
0
        private void MapIEnumerableEntitiesForMapping(ElasticsearchCrudJsonWriter elasticsearchCrudJsonWriter,
                                                      EntityContextInfo parentEntityInfo, PropertyInfo prop, bool createPropertyMappings)
        {
            object item;

            if (prop.PropertyType.FullName == "System.String[]")
            {
                item = string.Empty;
            }
            else if (prop.PropertyType.GenericTypeArguments.Length == 0)
            {
                item = Activator.CreateInstance(prop.PropertyType.GetElementType());
            }
            else if (prop.PropertyType.GenericTypeArguments[0].FullName == "System.String")
            {
                item = string.Empty;
            }
            else
            {
                item = Activator.CreateInstance(prop.PropertyType.GenericTypeArguments[0]);
            }

            var typeofArrayItem = item.GetType();

            if (typeofArrayItem.GetTypeInfo().IsClass&& typeofArrayItem.FullName != "System.String" &&
                typeofArrayItem.FullName != "System.Decimal")
            {
                // collection of Objects
                elasticsearchCrudJsonWriter.JsonWriter.WriteStartObject();

                //if (property.GetCustomAttribute(typeof(ElasticsearchCoreTypes)) != null)
                //{
                //    var propertyName = property.Name.ToLower();

                //    IEnumerable<Attribute> attrs = property.GetCustomAttributes(typeof(ElasticsearchCoreTypes), true);

                //    if ((attrs.FirstOrDefault() as ElasticsearchCoreTypes) != null)
                //    {
                //        elasticsearchCrudJsonWriter.JsonWriter.WritePropertyName(propertyName);
                //        elasticsearchCrudJsonWriter.JsonWriter.WriteRawValue((attrs.FirstOrDefault() as ElasticsearchCoreTypes).JsonString());
                //    }
                //}

                if (prop.GetCustomAttribute(typeof(ElasticsearchNestedAttribute)) != null)
                {
                    elasticsearchCrudJsonWriter.JsonWriter.WritePropertyName("type");
                    elasticsearchCrudJsonWriter.JsonWriter.WriteValue("nested");

                    IEnumerable <Attribute> attrs = prop.GetCustomAttributes(typeof(ElasticsearchNestedAttribute), true);

                    if ((attrs.FirstOrDefault() as ElasticsearchNestedAttribute) != null)
                    {
                        (attrs.FirstOrDefault() as ElasticsearchNestedAttribute).WriteJson(elasticsearchCrudJsonWriter);
                    }
                }

                // "properties": {
                elasticsearchCrudJsonWriter.JsonWriter.WritePropertyName("properties");
                elasticsearchCrudJsonWriter.JsonWriter.WriteStartObject();


                // Do class mapping for nested type
                var routingDefinition = new RoutingDefinition
                {
                    ParentId  = parentEntityInfo.Id,
                    RoutingId = parentEntityInfo.RoutingDefinition.RoutingId
                };
                var child = new EntityContextInfo
                {
                    Document          = item,
                    RoutingDefinition = routingDefinition,
                    EntityType        = item.GetType(),
                    DeleteDocument    = parentEntityInfo.DeleteDocument
                };
                MapEntityValues(child, elasticsearchCrudJsonWriter, false, createPropertyMappings);
                elasticsearchCrudJsonWriter.JsonWriter.WriteEndObject();
                elasticsearchCrudJsonWriter.JsonWriter.WriteEndObject();
            }
            else
            {
                // {"type": "ienumerable"}
                // collection of simple types
                elasticsearchCrudJsonWriter.JsonWriter.WriteStartObject();
                elasticsearchCrudJsonWriter.JsonWriter.WritePropertyName("type");
                elasticsearchCrudJsonWriter.JsonWriter.WriteValue(GetElasticsearchType(item.GetType()));
                elasticsearchCrudJsonWriter.JsonWriter.WriteEndObject();
            }
        }
Exemple #6
0
        public async Task <ResultDetails <T> > GetDocumentAsync <T>(object entityId, RoutingDefinition routingDefinition)
        {
            _traceProvider.Trace(TraceEventType.Verbose, "{2}: Request for select document with id: {0}, Type: {1}",
                                 entityId, typeof(T), "ElasticSearchContextGet");
            var resultDetails = new ResultDetails <T> {
                Status = HttpStatusCode.InternalServerError
            };

            try
            {
                var elasticSearchMapping = _elasticSerializerConfiguration.ElasticMappingResolver
                                           .GetElasticSearchMapping(typeof(T));
                var elasticUrlForEntityGet = string.Format("{0}/{1}/{2}/", _connectionString,
                                                           elasticSearchMapping.GetIndexForType(typeof(T)), elasticSearchMapping.GetDocumentType(typeof(T)));

                var uri = new Uri(
                    elasticUrlForEntityGet + entityId + RoutingDefinition.GetRoutingUrl(routingDefinition));
                _traceProvider.Trace(TraceEventType.Verbose, "{1}: Request HTTP GET uri: {0}", uri.AbsoluteUri,
                                     "ElasticSearchContextGet");
                var response = await _client.GetAsync(uri, _cancellationTokenSource.Token).ConfigureAwait(true);

                resultDetails.RequestUrl = uri.OriginalString;

                resultDetails.Status = response.StatusCode;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    _traceProvider.Trace(TraceEventType.Warning, "{2}: GetDocumentAsync response status code: {0}, {1}",
                                         response.StatusCode, response.ReasonPhrase, "ElasticSearchContextGet");
                    if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        var errorInfo = await response.Content.ReadAsStringAsync().ConfigureAwait(true);

                        resultDetails.Description = errorInfo;
                        if (errorInfo.Contains("RoutingMissingException"))
                        {
                            throw new ElasticException(
                                      "HttpStatusCode.BadRequest: RoutingMissingException, adding the parent Id if this is a child item...");
                        }

                        return(resultDetails);
                    }
                }

                var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(true);

                _traceProvider.Trace(TraceEventType.Verbose, "{1}: Get Request response: {0}", responseString,
                                     "ElasticSearchContextGet");
                var responseObject = JObject.Parse(responseString);

                var source = responseObject["_source"];
                if (source != null)
                {
                    var result = _elasticSerializerConfiguration.ElasticMappingResolver
                                 .GetElasticSearchMapping(typeof(T)).ParseEntity(source, typeof(T));
                    resultDetails.PayloadResult = (T)result;
                }

                return(resultDetails);
            }
            catch (OperationCanceledException oex)
            {
                _traceProvider.Trace(TraceEventType.Verbose, oex, "{1}: Get Request OperationCanceledException: {0}",
                                     oex.Message, "ElasticSearchContextGet");
                return(resultDetails);
            }
        }
Exemple #7
0
 public MappingDefinition()
 {
     RoutingDefinition = new RoutingDefinition();
     All    = new MappingAll();
     Source = new MappingSource();
 }