コード例 #1
0
        public ElasticQueryResult <T> RunQueryWithoutSecurityFilters <T>(ElasticQuery query) where T : TreeRecord
        {
            var info = StringHelper.AddToString(BaseUrl, "/", elasticSettings.DefaultIndex);

            info = StringHelper.AddToString(info, "/", elasticSettings.DefaultTypeName);
            var result = new ElasticQueryResult <T>
            {
                RequestInfo        = info,
                Status             = 0,
                TimeInMilliseconds = 0
            };

            var client = clientProvider.GetElasticClient(elasticSettings, result);

            try
            {
                var started = DateTime.Now;

                var request = new SearchRequest <ElasticArchiveRecord> {
                    Query = query.Query
                };
                var p = query.SearchParameters;

                AddPaging(p?.Paging, request);
                AddSort(p?.Paging?.OrderBy, p?.Paging?.SortOrder, request);
                ExcludeUnwantedFields(request);

                result.Response           = client.Search <T>(request);
                result.TimeInMilliseconds = (int)Math.Round((DateTime.Now - started).TotalMilliseconds);
                result.Status             = (int)HttpStatusCode.OK;

                ProcessQueryResult(result, null, null, client.SourceSerializer);
            }
            catch (Exception ex)
            {
                var statusCode = (ex as ElasticsearchClientException)?.Response?.HttpStatusCode;

                if (statusCode.HasValue)
                {
                    Log.Warning(ex, "Exception on Elastic query: {0}", result.RequestInfo);
                    result.Status = statusCode.Value;
                }
                else
                {
                    Log.Error(ex, "Exception on Elastic query: {0}", result.RequestInfo);
                    result.Status = (int)HttpStatusCode.InternalServerError;
                }

                var debugInformation = (ex as ElasticsearchClientException)?.DebugInformation;

                if (!string.IsNullOrEmpty(debugInformation))
                {
                    Log.Information(ex, "Additional information about the prior exception. Debug information: {0}", debugInformation);
                }

                result.Exception = ex;
            }

            return(result);
        }
コード例 #2
0
        private void ProcessQueryResult <T>(ElasticQueryResult <T> result, FacetFilters[] facetsFilters, UserAccess access,
                                            IElasticsearchSerializer serializer) where T : TreeRecord
        {
            var response = result.Response;

            var hits = response?.Hits ?? new List <IHit <T> >();

            result.TotalNumberOfHits = response?.HitsMetadata != null ? (int)response.HitsMetadata.Total.Value : -1;
            var entries = new List <Entity <T> >();

            foreach (var hit in hits)
            {
                var data = JsonConvert.DeserializeObject <T>(serializer.SerializeToString(hit.Source));

                var entry = new Entity <T>
                {
                    Data        = data,
                    Highlight   = GetHighlightingObj(hit, access),
                    Explanation = GetExplanationObj(hit, serializer)
                };

                if (access != null)
                {
                    data.Translate(access.Language);
                    entry.IsDownloadAllowed = access.HasAnyTokenFor(data.PrimaryDataDownloadAccessTokens);
                }

                entries.Add(entry);
            }

            var entityResult = new EntityResult <T> {
                Items = entries
            };

            if (response?.Aggregations.Any() != null)
            {
                var filteredAggregations = GetfilteredAggregations(response.Aggregations, facetsFilters, out var chosenCreationPeriodAggregation);
                var facette = filteredAggregations.CreateSerializableAggregations();

                ComplementAggregations(facette, chosenCreationPeriodAggregation);
                result.Facets = facette;
            }

            result.Data = entityResult;
        }
コード例 #3
0
        public ElasticQueryResult <T> RunQuery <T>(ElasticQuery query, UserAccess access) where T : TreeRecord
        {
            var stopwatch = new Stopwatch();
            var info      = StringHelper.AddToString(BaseUrl, "/", elasticSettings.DefaultIndex);

            if (!string.IsNullOrEmpty(elasticSettings.DefaultTypeName))
            {
                info += "/" + elasticSettings.DefaultTypeName;
            }

            var result = new ElasticQueryResult <T>
            {
                RequestInfo        = info,
                Status             = 0,
                TimeInMilliseconds = 0,
                EnableExplanations = query.SearchParameters?.Options?.EnableExplanations ?? false
            };

            var client = clientProvider.GetElasticClient(elasticSettings, result);

            try
            {
                stopwatch.Start();
                var searchRequest = BuildSearchRequest(query, access);
                result.Response = client.Search <T>(searchRequest);

                var json = client.RequestResponseSerializer.SerializeToString(searchRequest, SerializationFormatting.Indented);
                Log.Debug(json);

                stopwatch.Stop();
                result.TimeInMilliseconds = stopwatch.ElapsedMilliseconds;
                Debug.WriteLine($"Fetched record from web in  {stopwatch.ElapsedMilliseconds}ms");
                result.Status = (int)HttpStatusCode.OK;

                ProcessQueryResult(result, query.SearchParameters?.FacetsFilters, access, client.SourceSerializer);
            }
            catch (Exception ex)
            {
                var statusCode = (ex as ElasticsearchClientException)?.Response?.HttpStatusCode;

                if (statusCode.HasValue)
                {
                    Log.Warning(ex, "Exception on Elastic query: {0}", result.RequestInfo);
                    result.Status = statusCode.Value;
                }
                else
                {
                    Log.Error(ex, "Exception on Elastic query: {0}", result.RequestInfo);
                    result.Status = (int)HttpStatusCode.InternalServerError;
                }

                var debugInformation = (ex as ElasticsearchClientException)?.DebugInformation;

                if (!string.IsNullOrEmpty(debugInformation))
                {
                    Log.Information(ex, "Additional information about the prior exception. Debug information: {0}",
                                    debugInformation);
                }

                result.Exception = ex;
            }
            finally
            {
                Log.Debug("RunQueryCompleted: {RequestInfo}, {RequestRaw}, {ResponseRaw}", result.RequestInfo, result.RequestRaw, result.ResponseRaw);
            }

            return(result);
        }