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         = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(typeof(T));
                var elasticsearchUrlForEntityGet = string.Format("{0}/{1}/{2}/", _connectionString, elasticSearchMapping.GetIndexForType(typeof(T)), elasticSearchMapping.GetDocumentType(typeof(T)));

                var uri = new Uri(elasticsearchUrlForEntityGet + 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(false);

                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(false);

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

                        return(resultDetails);
                    }
                }

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

                _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 = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.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);
            }
        }
Ejemplo n.º 2
0
        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));
        }