public async Task <IReadOnlyCollection <Meeting> > Handle(SearchMeetings query, CancellationToken cancellationToken)
        {
            var response = await elasticClient.SearchAsync <Meeting>(
                s => s.Query(q => q.QueryString(d => d.Query(query.Filter))).Size(MaxItemsCount), cancellationToken);

            return(response.Documents);
        }
Exemplo n.º 2
0
        public async Task <IQueryResult <T> > Query <T>(IDefinition definition) where T : class
        {
            Func <Nest.SearchDescriptor <T>, Nest.ISearchRequest> searchSelector = x =>
            {
                x = x.Query(q =>
                {
                    foreach (var group in definition.Operations)
                    {
                        var option = Group.FromValue(group.Group);
                        if (option == Group.All)
                        {
                            Nest.QueryContainer final = new Nest.MatchAllQuery();
                            foreach (var match in group.Definitions)
                            {
                                final = final && addOperation(q, match);
                            }

                            q.Bool(b => b.Must(final));
                        }
                        else if (option == Group.Not)
                        {
                            Nest.QueryContainer final = new Nest.MatchAllQuery();
                            foreach (var match in group.Definitions)
                            {
                                final = final && addOperation(q, match);
                            }

                            q.Bool(b => b.Must(!final));
                        }
                        else if (option == Group.Any)
                        {
                            Nest.QueryContainer final = new Nest.MatchAllQuery();
                            foreach (var match in group.Definitions)
                            {
                                final = final || addOperation(q, match);
                            }

                            q.Bool(b => b.Must(final));
                        }
                    }

                    return(q);
                });

                return(x);
            };


            var documents = new List <T>();
            var elapsedMs = 0L;

            Nest.ISearchResponse <T> searchResult = await _client.SearchAsync <T>(x =>
            {
                x = x.Index(typeof(T).FullName.ToLower());
                x = x.SearchType(SearchType.QueryThenFetch);
                x = x.Scroll("4s");

                return(searchSelector(x));
            }).ConfigureAwait(false);

            if (!searchResult.IsValid)
            {
                _logger.WarnEvent("Invalid", "Elastic request: {Request}", searchResult.DebugInformation);
                throw new StorageException($"Invalid elastic request: {searchResult.DebugInformation}");
            }

            do
            {
                documents.AddRange(searchResult.Documents);
                elapsedMs += searchResult.Took;

                searchResult = await _client.ScrollAsync <T>("4s", searchResult.ScrollId).ConfigureAwait(false);

                if (!searchResult.IsValid)
                {
                    throw new StorageException($"Invalid elastic request: {searchResult.DebugInformation}");
                }
            } while (searchResult.Documents.Any());

            return(new QueryResult <T>
            {
                Records = documents.ToArray(),
                Total = searchResult.Total,
                ElapsedMs = elapsedMs
            });
        }