Ejemplo n.º 1
0
        public bool PostSearchExists <T>(string jsonContent, SearchUrlParameters searchUrlParameters)
        {
            var syncExecutor = new SyncExecute(_traceProvider);

            return(syncExecutor.ExecuteResultDetails(() => PostSearchExistsAsync <T>(jsonContent, searchUrlParameters))
                   .PayloadResult);
        }
Ejemplo n.º 2
0
        public async Task <ResultDetails <T> > SearchByIdAsync <T>(object entityId, SearchUrlParameters searchUrlParameters)
        {
            var elasticSearchMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(typeof(T));
            var index = elasticSearchMapping.GetIndexForType(typeof(T));
            var type  = elasticSearchMapping.GetDocumentType(typeof(T));

            _traceProvider.Trace(TraceEventType.Verbose,
                                 string.Format("ElasticContextSearch: Searching for document id: {0}, index {1}, type {2}", entityId,
                                               index, type));

            var resultDetails = new ResultDetails <T> {
                Status = HttpStatusCode.InternalServerError
            };

            var search = new SearchRequest(_traceProvider, _cancellationTokenSource, _elasticSerializerConfiguration,
                                           _client, _connectionString);

            var result = await search.PostSearchAsync <T>(BuildSearchById(entityId), null, null, searchUrlParameters);

            resultDetails.RequestBody = result.RequestBody;
            resultDetails.RequestUrl  = result.RequestUrl;

            if (result.Status == HttpStatusCode.OK && result.PayloadResult.Hits.Total > 0)
            {
                resultDetails.PayloadResult = result.PayloadResult.Hits.HitsResult.First().Source;
                return(resultDetails);
            }

            if (result.Status == HttpStatusCode.OK && result.PayloadResult.Hits.Total == 0)
            {
                resultDetails.Status      = HttpStatusCode.NotFound;
                resultDetails.Description = string.Format("No document found id: {0}, index {1}, type {2}", entityId,
                                                          index, type);
                _traceProvider.Trace(TraceEventType.Information,
                                     string.Format("ElasticContextSearch: No document found id: {0},, index {1}, type {2}", entityId,
                                                   index, type));
                return(resultDetails);
            }

            resultDetails.Status      = result.Status;
            resultDetails.Description = result.Description;
            _traceProvider.Trace(TraceEventType.Error,
                                 string.Format("ElasticContextSearch: No document found id: {0},  index {1}, type {2}", entityId, index,
                                               type));
            return(resultDetails);
        }
Ejemplo n.º 3
0
        public T SearchById <T>(object entityId, SearchUrlParameters searchUrlParameters)
        {
            var syncExecutor = new SyncExecute(_traceProvider);
            var result       = syncExecutor.ExecuteResultDetails(() => SearchByIdAsync <T>(entityId, searchUrlParameters));

            if (result.Status == HttpStatusCode.NotFound)
            {
                _traceProvider.Trace(TraceEventType.Warning, "ElasticsearchContextSearch: HttpStatusCode.NotFound");
                throw new ElasticsearchCrudException("ElasticsearchContextSearch: HttpStatusCode.NotFound");
            }
            if (result.Status == HttpStatusCode.BadRequest)
            {
                _traceProvider.Trace(TraceEventType.Warning, "ElasticsearchContextSearch: HttpStatusCode.BadRequest");
                throw new ElasticsearchCrudException("ElasticsearchContextSearch: HttpStatusCode.BadRequest" + result.Description);
            }

            return(result.PayloadResult);
        }
Ejemplo n.º 4
0
        public async Task <ResultDetails <bool> > PostSearchExistsAsync <T>(string jsonContent, SearchUrlParameters searchUrlParameters)
        {
            _traceProvider.Trace(TraceEventType.Verbose, "{2}: Request for search exists: {0}, content: {1}", typeof(T), jsonContent, "Search");
            var resultDetails = new ResultDetails <bool>
            {
                Status      = HttpStatusCode.InternalServerError,
                RequestBody = jsonContent
            };

            var urlParams = "";

            if (searchUrlParameters != null)
            {
                urlParams = searchUrlParameters.GetUrlParameters();
            }

            try
            {
                var elasticSearchMapping            = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(typeof(T));
                var elasticsearchUrlForSearchExists = string.Format("{0}/{1}/{2}/_search/exists{3}", _connectionString, elasticSearchMapping.GetIndexForType(typeof(T)), elasticSearchMapping.GetDocumentType(typeof(T)), urlParams);

                var content = new StringContent(jsonContent);
                var uri     = new Uri(elasticsearchUrlForSearchExists);
                _traceProvider.Trace(TraceEventType.Verbose, "{1}: Request HTTP Post uri: {0}", uri.AbsoluteUri, "Search");

                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                resultDetails.RequestUrl    = elasticsearchUrlForSearchExists;
                var response = await _client.PostAsync(uri, content, _cancellationTokenSource.Token).ConfigureAwait(true);

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

                        resultDetails.Description = errorInfo;
                        return(resultDetails);
                    }

                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        var errorInfo = await response.Content.ReadAsStringAsync().ConfigureAwait(true);

                        resultDetails.Description = errorInfo;
                        return(resultDetails);
                    }
                }

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

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


                var source = responseObject["exists"];

                resultDetails.PayloadResult = (bool)source;
                return(resultDetails);
            }
            catch (OperationCanceledException oex)
            {
                _traceProvider.Trace(TraceEventType.Verbose, oex, "{1}: Get Request OperationCanceledException: {0}", oex.Message, "Search");
                return(resultDetails);
            }
        }
Ejemplo n.º 5
0
        public ResultDetails <SearchResult <T> > PostSearch <T>(string jsonContent, string scrollId, ScanAndScrollConfiguration scanAndScrollConfiguration, SearchUrlParameters searchUrlParameters)
        {
            var syncExecutor = new SyncExecute(_traceProvider);

            return(syncExecutor.ExecuteResultDetails(() => PostSearchAsync <T>(jsonContent, scrollId, scanAndScrollConfiguration, searchUrlParameters)));
        }
Ejemplo n.º 6
0
        public async Task <ResultDetails <SearchResult <T> > > PostSearchAsync <T>(string jsonContent, string scrollId, ScanAndScrollConfiguration scanAndScrollConfiguration, SearchUrlParameters searchUrlParameters)
        {
            _traceProvider.Trace(TraceEventType.Verbose, "{2}: Request for search: {0}, content: {1}", typeof(T), jsonContent, "Search");

            var urlParams = "";

            if (searchUrlParameters != null)
            {
                urlParams = searchUrlParameters.GetUrlParameters();
            }
            var elasticSearchMapping         = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(typeof(T));
            var elasticsearchUrlForEntityGet = string.Format("{0}/{1}/{2}/_search{3}", _connectionString, elasticSearchMapping.GetIndexForType(typeof(T)), elasticSearchMapping.GetDocumentType(typeof(T)), urlParams);

            if (!string.IsNullOrEmpty(scrollId))
            {
                elasticsearchUrlForEntityGet = string.Format("{0}/{1}{2}", _connectionString, scanAndScrollConfiguration.GetScrollScanUrlForRunning(), scrollId);
            }

            var uri = new Uri(elasticsearchUrlForEntityGet);

            var result = await PostInteranlSearchAsync <T>(jsonContent, uri);

            return(result);
        }