public static IQueriedSearch <TSource, MinShouldMatchQueryStringQuery> MinimumShouldMatch <TSource>(this IQueriedSearch <TSource> search, string minMatch)
        {
            return(new Search <TSource, MinShouldMatchQueryStringQuery>(search, context =>
            {
                var originalQuery = (QueryStringQuery)context.RequestBody.Query;
                var query = new MinShouldMatchQueryStringQuery(originalQuery.Query);
                query.RawQuery = originalQuery.RawQuery;
                query.AllowLeadingWildcard = originalQuery.AllowLeadingWildcard;
                query.AnalyzeWildcard = originalQuery.AnalyzeWildcard;
                query.Analyzer = originalQuery.Analyzer;
                query.AutoGeneratePhraseQueries = originalQuery.AutoGeneratePhraseQueries;
                query.Boost = originalQuery.Boost;
                query.DefaultOperator = originalQuery.DefaultOperator;
                query.EnablePositionIncrements = originalQuery.EnablePositionIncrements;
                query.FuzzyMinSim = originalQuery.FuzzyMinSim;
                query.FuzzyPrefixLength = originalQuery.FuzzyPrefixLength;
                query.LowercaseExpandedTerms = originalQuery.LowercaseExpandedTerms;
                query.PhraseSlop = originalQuery.PhraseSlop;
                query.DefaultField = originalQuery.DefaultField;
                var multiFieldQuery = context.RequestBody.Query as MultiFieldQueryStringQuery;
                if (multiFieldQuery != null)
                {
                    query.Fields = multiFieldQuery.Fields;
                }
                query.MinimumShouldMatch = minMatch;

                context.RequestBody.Query = query;
            }));
        }
Example #2
0
        public static bool TryGetMinShouldMatchQueryStringQuery(IQuery query, out MinShouldMatchQueryStringQuery currentMinShouldMatchQueryStringQuery)
        {
            currentMinShouldMatchQueryStringQuery = query as MinShouldMatchQueryStringQuery;
            if (currentMinShouldMatchQueryStringQuery == null)
            {
                return(false);
            }

            return(true);
        }
        private static MinShouldMatchQueryStringQuery CreateQuery(string phrase, MultiFieldQueryStringQuery currentQueryStringQuery, string minShouldMatch)
        {
            string phrasesQuery        = QueryHelpers.EscapeElasticSearchQuery(phrase);
            var    minShouldMatchQuery = new MinShouldMatchQueryStringQuery(phrasesQuery);

            minShouldMatchQuery.RawQuery                  = currentQueryStringQuery.RawQuery;
            minShouldMatchQuery.AllowLeadingWildcard      = currentQueryStringQuery.AllowLeadingWildcard;
            minShouldMatchQuery.AnalyzeWildcard           = currentQueryStringQuery.AnalyzeWildcard;
            minShouldMatchQuery.Analyzer                  = currentQueryStringQuery.Analyzer;
            minShouldMatchQuery.AutoGeneratePhraseQueries = currentQueryStringQuery.AutoGeneratePhraseQueries;
            minShouldMatchQuery.Boost = currentQueryStringQuery.Boost;
            minShouldMatchQuery.EnablePositionIncrements = currentQueryStringQuery.EnablePositionIncrements;
            minShouldMatchQuery.FuzzyMinSim            = currentQueryStringQuery.FuzzyMinSim;
            minShouldMatchQuery.FuzzyPrefixLength      = currentQueryStringQuery.FuzzyPrefixLength;
            minShouldMatchQuery.LowercaseExpandedTerms = currentQueryStringQuery.LowercaseExpandedTerms;
            minShouldMatchQuery.PhraseSlop             = currentQueryStringQuery.PhraseSlop;
            minShouldMatchQuery.DefaultField           = currentQueryStringQuery.DefaultField;
            minShouldMatchQuery.Fields = currentQueryStringQuery.Fields;

            minShouldMatchQuery.MinimumShouldMatch = minShouldMatch.IsNotNullOrEmpty() ? minShouldMatch : "1";
            minShouldMatchQuery.DefaultOperator    = BooleanOperator.Or;

            return(minShouldMatchQuery);
        }
        public static IQueriedSearch <TSource, QueryStringQuery> WildcardMatch <TSource>(this IQueriedSearch <TSource> search, params Expression <Func <TSource, string> >[] fieldSelectors)
        {
            return(new Search <TSource, QueryStringQuery>(search, context =>
            {
                BoolQuery currentBoolQuery;
                BoolQuery newBoolQuery = new BoolQuery();
                MinShouldMatchQueryStringQuery currentQueryStringQuery;

                if (QueryHelpers.TryGetBoolQuery(context.RequestBody.Query, out currentBoolQuery))
                {
                    if (!QueryHelpers.TryGetMinShouldMatchQueryStringQuery(currentBoolQuery.Should[0], out currentQueryStringQuery))
                    {
                        return;
                    }
                    newBoolQuery = currentBoolQuery;
                }
                else
                {
                    if (!QueryHelpers.TryGetMinShouldMatchQueryStringQuery(context.RequestBody.Query, out currentQueryStringQuery))
                    {
                        return;
                    }
                    newBoolQuery.Should.Add(currentQueryStringQuery);
                }

                var query = QueryHelpers.GetRawQueryString(currentQueryStringQuery);
                if (query.IsNullOrEmpty())
                {
                    return;
                }

                // Do not trigger on synonym expansions which are parenthesized and not on quoted searches.
                if (IsParenthesized(query) || QueryHelpers.IsStringQuoted(query))
                {
                    return;
                }

                var terms = QueryHelpers.GetQueryPhrases(query);

                // Only take terms > 2 chars and take max 3 of these
                string[] candidateTerms = terms.Where(x => x.Length > 2).Take(3).Select(x => string.Format("{0}{1}", x, "*")).ToArray();
                if (candidateTerms.Count() == 0)
                {
                    return;
                }

                List <string> fieldNames = new List <string>();
                foreach (var fieldSelector in fieldSelectors)
                {
                    string fieldName = search.Client.Conventions
                                       .FieldNameConvention
                                       .GetFieldNameForAnalyzed(fieldSelector);
                    fieldNames.Add(fieldName);
                }

                var wildcardQueryString = string.Join(" ", candidateTerms);
                var wildcardQuery = new MinShouldMatchQueryStringQuery(wildcardQueryString)
                {
                    Fields = fieldNames,
                    DefaultOperator = currentQueryStringQuery.DefaultOperator,
                    MinimumShouldMatch = currentQueryStringQuery.MinimumShouldMatch,
                    Boost = 0.2
                };

                newBoolQuery.Should.Add(wildcardQuery);

                context.RequestBody.Query = newBoolQuery;
            }));
        }