Пример #1
0
        public IndexSearchRequestBuilder ParseFacets(ISearchPhraseParser phraseParser, string facetPhrase, IList <AggregationRequest> predefinedAggregations = null)
        {
            if (phraseParser == null)
            {
                throw new ArgumentNullException(nameof(phraseParser));
            }
            SearchRequest.Aggregations = predefinedAggregations ?? new List <AggregationRequest>();

            if (string.IsNullOrEmpty(facetPhrase))
            {
                return(this);
            }

            //TODO: Support aliases for Facet expressions e.g price.usd[TO 200) as price_below_200
            //TODO: Need to create a new  Antlr file with g4-lexer rules and generate parser especially for facets expression that will return proper AggregationRequests objects
            var parseResult = phraseParser.Parse(facetPhrase);

            //Term facets
            if (!string.IsNullOrEmpty(parseResult.Keyword))
            {
                var termFacetExpressions = parseResult.Keyword.Split(" ");
                parseResult.Filters.AddRange(termFacetExpressions.Select(x => new TermFilter
                {
                    FieldName = x,
                    Values    = new List <string>()
                }));
            }

            SearchRequest.Aggregations = parseResult.Filters
                                         .Select <IFilter, AggregationRequest>(filter =>
            {
                FilterSyntaxMapper.MapFilterAdditionalSyntax(filter);

                return(filter switch
                {
                    RangeFilter rangeFilter => new RangeAggregationRequest
                    {
                        Id = filter.Stringify(),
                        FieldName = rangeFilter.FieldName,
                        Values = rangeFilter.Values.Select(x => new RangeAggregationRequestValue
                        {
                            Id = x.Stringify(),
                            Lower = x.Lower,
                            Upper = x.Upper,
                            IncludeLower = x.IncludeLower,
                            IncludeUpper = x.IncludeUpper
                        }).ToList()
                    },
                    TermFilter termFilter => new TermAggregationRequest
                    {
                        FieldName = termFilter.FieldName,
                        Id = filter.Stringify(),
                        Filter = termFilter
                    },
                    _ => null,
                });
            })
        public IndexSearchRequestBuilder ParseFilters(ISearchPhraseParser phraseParser, string filterPhrase)
        {
            if (phraseParser == null)
            {
                throw new ArgumentNullException(nameof(phraseParser));
            }

            if (string.IsNullOrEmpty(filterPhrase))
            {
                return(this);
            }

            var parseResult = phraseParser.Parse(filterPhrase);

            var filters = new List <IFilter>();

            foreach (var filter in parseResult.Filters)
            {
                FilterSyntaxMapper.MapFilterAdditionalSyntax(filter);

                if (filter is TermFilter termFilter)
                {
                    var wildcardValues = termFilter.Values.Where(x => new[] { "?", "*" }.Any(x.Contains)).ToArray();

                    if (wildcardValues.Any())
                    {
                        var orFilter = new OrFilter
                        {
                            ChildFilters = new List <IFilter>()
                        };

                        var wildcardTermFilters = wildcardValues.Select(x => new WildCardTermFilter
                        {
                            FieldName = termFilter.FieldName,
                            Value     = x
                        }).ToList();

                        orFilter.ChildFilters.AddRange(wildcardTermFilters);

                        termFilter.Values = termFilter.Values.Except(wildcardValues).ToList();

                        if (termFilter.Values.Any())
                        {
                            orFilter.ChildFilters.Add(termFilter);
                        }

                        filters.Add(orFilter);
                    }
                    else
                    {
                        filters.Add(termFilter);
                    }
                }
                else
                {
                    filters.Add(filter);
                }
            }

            AddFiltersToSearchRequest(filters.ToArray());

            return(this);
        }