public void ConfigureElasticSearchTestNullConfigurationThrows()
        {
            Action act = () => ElasticSearchExtensions.ConfigureElasticSearch(new ServiceCollection(), null);

            act.Should().Throw <ArgumentNullException>();
        }
Example #2
0
        private static Func <QueryContainerDescriptor <TIndexItem>, QueryContainer> WildcardTextSearch(string searchText,
                                                                                                       Collection <TextSearchField <TIndexItem> > textSearchFields = null)
        {
            textSearchFields ??= new Collection <TextSearchField <TIndexItem> >
            {
                new TextSearchField <TIndexItem>
                {
                    Field = search => ElasticSearchExtensions.GetField <TIndexItem>("Name")
                }
            };

            var queries = new Collection <Func <QueryContainerDescriptor <TIndexItem>, QueryContainer> >();

            foreach (var field in textSearchFields)
            {
                Func <QueryContainerDescriptor <TIndexItem>, QueryContainer> stringQuerySelector;

                if (field.Strict)
                {
                    stringQuerySelector = MatchExactValue(field);
                }
                else
                {
                    queries.Add(MatchExactValue(field));

                    stringQuerySelector = q => q.MultiMatch(MultiMatchQuerySelector(field.Field));
                }

                if (field.Path == null)
                {
                    queries.Add(stringQuerySelector);
                }
                else
                {
                    QueryContainer NestedQuery(QueryContainerDescriptor <TIndexItem> q) => q
                    .Nested(n => n
                            .Path(field.Path)
                            .Query(stringQuerySelector)
                            );

                    queries.Add(NestedQuery);
                }
            }

            return(descriptor => descriptor.Bool(b => b.Should(queries)));

            Func <MultiMatchQueryDescriptor <TIndexItem>, IMultiMatchQuery> MultiMatchQuerySelector(Expression <Func <TIndexItem, Field> > field)
            {
                return(m => m
                       .Query(searchText)
                       .Boost(0.7)
                       .Fields(f => f
                               .Field(ff => field)
                               .Field("*.ngram")
                               )
                       .Lenient()
                       .MinimumShouldMatch(MinimumShouldMatch.Percentage(80))
                       .TieBreaker(0.1)
                       .Type(TextQueryType.BestFields)
                       .Operator(Operator.And));
            }

            Func <QueryContainerDescriptor <TIndexItem>, QueryContainer> MatchExactValue(TextSearchField <TIndexItem> field)
            {
                return(q => q.QueryString(q1 => q1
                                          .Query(searchText)
                                          .Boost(1.1)
                                          .Fields(field.Field)
                                          .Analyzer("keyword_analyzer")
                                          .DefaultOperator(Operator.And)
                                          .TieBreaker(0.1)
                                          .QuoteFieldSuffix("")
                                          .Rewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(5))
                                          .Lenient()
                                          .Escape()
                                          ));
            }
        }
        public void ConfigureElasticSearchTestNullServicesThrows()
        {
            Action act = () => ElasticSearchExtensions.ConfigureElasticSearch((IServiceCollection)null, _mockConfiguration.Object);

            act.Should().Throw <ArgumentNullException>();
        }