示例#1
0
        private ElasticQuery BuildQueryForIds(IList <int> ids, Paging p)
        {
            var query = new ElasticQuery
            {
                SearchParameters = new SearchParameters
                {
                    Paging  = p,
                    Options = new SearchOptions
                    {
                        EnableExplanations = false,
                        EnableHighlighting = false,
                        EnableAggregations = false
                    }
                }
            };

            if (ids.Count != 0)
            {
                query.Query = new TermsQuery
                {
                    Field = elasticSettings.IdField,
                    Terms = ids.Select(i => i.ToStringInvariant())
                };
            }
            else
            {
                query.Query = new MatchNoneQuery();
            }

            return(query);
        }
示例#2
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);
        }
        private SearchRequest <ElasticArchiveRecord> BuildSearchRequest(ElasticQuery query, UserAccess access)
        {
            var request = new SearchRequest <ElasticArchiveRecord>(elasticSettings.DefaultIndex);

            var queryWithSecurity = GetQueryWithSecurity(query.Query, access);

            request.Query      = queryWithSecurity;
            request.PostFilter = CreateQuery(query.SearchParameters.FacetsFilters);

            var parameters = query.SearchParameters;
            var options    = parameters?.Options;

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

            if (options?.EnableAggregations ?? false)
            {
                AddAggregations(request, parameters?.FacetsFilters);
            }

            if (options?.EnableHighlighting ?? false)
            {
                AddHighlighting(request);
            }

            if (options?.EnableExplanations ?? false)
            {
                request.Explain = true;
            }

            return(request);
        }
示例#4
0
        public ElasticQueryResult <T> QueryForId <T>(int id, UserAccess access) where T : TreeRecord
        {
            var query = new ElasticQuery
            {
                SearchParameters = new SearchParameters
                {
                    Options = new SearchOptions
                    {
                        EnableExplanations = false,
                        EnableHighlighting = false,
                        EnableAggregations = false
                    }
                },
                Query = new TermQuery
                {
                    Field = elasticSettings.IdField,
                    Value = id.ToStringInvariant()
                }
            };

            return(RunQuery <T>(query, access));
        }
示例#5
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);
        }