Example #1
0
 private static IQuery Transform(ConstantScoreQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
 {
     return(new ConstantScoreQuery(query.Query.Transform(aliases, qe)));
 }
Example #2
0
 private static IQuery Transform(FunctionScoreQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
 {
     return(new FunctionScoreQuery(query.Query.Transform(aliases, qe), query.Functions));
 }
Example #3
0
        private static IQuery Transform(BoolQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
        {
            var must   = query.MustQueries == null || query.MustQueries.Length == 0 ? null : query.MustQueries.Select(e => e.Transform(aliases, qe)).ToArray();
            var not    = query.NotQueries == null || query.NotQueries.Length == 0 ? null : query.NotQueries.Select(e => e.Transform(aliases, qe)).ToArray();
            var filter = query.FilterQueries == null || query.FilterQueries.Length == 0 ? null : query.FilterQueries.Select(e => e.Transform(aliases, qe)).ToArray();
            var should = query.ShouldQueries == null || query.ShouldQueries.Length == 0 ? null : query.ShouldQueries.Select(e => e.Transform(aliases, qe)).ToArray();

            return(new BoolQuery(must, not, should, filter));
        }
Example #4
0
        private static IQuery Transform(QueryStringQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
        {
            bool qsq  = false;
            bool sqsq = false;

            var qs = query.Query.ToQueryString().Transform(aliases, ref qsq, ref sqsq);

            if (qsq)
            {
                return(new QueryStringQuery(qs.ToString(), query.Fields, query.IsAnd));
            }
            else
            {
                if (sqsq)
                {
                    return(new SimpleQueryStringQuery(qs.ToString(), query.Fields, query.IsAnd));
                }
                else
                {
                    var terms = qs.GetTerms();
                    if (terms.Length == 0)
                    {
                        return(new MatchAllQuery());
                    }
                    else
                    {
                        var match = new MultiMatchQuery(string.Join(" ", terms), query.Fields, MultiMatchType.BestFields, query.IsAnd);
                        return(qe == null ? match : qe.Expand(match));
                    }
                }
            }
        }
Example #5
0
        private static IQuery Transform(MultiMatchQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
        {
            bool qsq  = false;
            bool sqsq = false;

            var qs = query.Query.ToQueryString().Transform(aliases, ref qsq, ref sqsq);

            if (qsq)
            {
                return(new QueryStringQuery(qs.ToString(), query.Fields, query.IsAnd));
            }

            if (sqsq)
            {
                return(new SimpleQueryStringQuery(qs.ToString(), query.Fields, query.IsAnd));
            }

            return(qe == null ? query : qe.Expand(query));
        }
Example #6
0
        public static IQuery Transform(this IQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe)
        {
            if (query is BoolQuery)
            {
                return(Transform(query as BoolQuery, aliases, qe));
            }

            if (query is MultiMatchQuery)
            {
                return(Transform(query as MultiMatchQuery, aliases, qe));
            }

            if (query is QueryStringQuery)
            {
                return(Transform(query as QueryStringQuery, aliases, qe));
            }

            if (query is TermQuery)
            {
                var q = query as TermQuery;
                return(SetAliases(q, aliases, (f) => new TermQuery(f, q.Value)));
            }

            if (query is TermsQuery)
            {
                var q = query as TermsQuery;
                return(SetAliases(q, aliases, (f) => new TermsQuery(f, q.Values)));
            }

            if (query is RangeQuery)
            {
                var q = query as RangeQuery;
                return(SetAliases(q, aliases, (f) => new RangeQuery(f, q.Values)));
            }

            if (query is MatchQuery)
            {
                var    q     = query as MatchQuery;
                string value = (string)q.Value;
                return(SetAliases(q, aliases, (f) => new MatchQuery(f, value, q.IsAnd, q.IsZeroTerms)));
            }

            if (query is PrefixQuery)
            {
                var    q     = query as PrefixQuery;
                string value = (string)q.Value;
                return(SetAliases(q, aliases, (f) => new PrefixQuery(f, value)));
            }

            if (query is WildcardQuery)
            {
                var    q     = query as WildcardQuery;
                string value = (string)q.Value;
                return(SetAliases(q, aliases, (f) => new WildcardQuery(f, value)));
            }

            if (query is ExistsQuery)
            {
                var q = query as ExistsQuery;
                return(SetAliases(q, aliases, (f) => new ExistsQuery(f)));
            }

            if (query is MatchPhrasePrefixQuery)
            {
                var    q     = query as MatchPhrasePrefixQuery;
                string value = (string)q.Value;
                return(SetAliases(q, aliases, (f) => new MatchPhrasePrefixQuery(f, value)));
            }

            if (query is MatchPhraseQuery)
            {
                var    q     = query as MatchPhraseQuery;
                string value = (string)q.Value;
                return(SetAliases(q, aliases, (f) => new MatchPhraseQuery(f, value)));
            }

            if (query is FunctionScoreQuery)
            {
                return(Transform(query as FunctionScoreQuery, aliases, qe));
            }

            if (query is ConstantScoreQuery)
            {
                return(Transform(query as ConstantScoreQuery, aliases, qe));
            }

            if (query is NestedQuery)
            {
                var q = query as NestedQuery;
                return(new NestedQuery(q.Path, q.Query.Transform(aliases, qe)));
            }

            return(query);
        }
Example #7
0
        public SearchTemplate(JsonObject jo)
        {
            Id          = jo.Property <string>(ID);
            Name        = jo.Property <string>(NAME);
            Description = jo.Property <string>(DESCRIPTION);
            Index       = jo.Property <string>(INDEX);
            Type        = jo.Property <string>(TYPE);

            var es = jo.Property(ES).Value;

            if (es != null)
            {
                if (es.Type == JsonType.String)
                {
                    RequestBody = JsonObject.Parse(es.Get <string>());
                }
                else
                {
                    RequestBody = es.Get <JsonObject>();
                }
            }

            var jp = jo.Property(FIELD_ALIASES);

            if (jp != null)
            {
                IDictionary <string, string[]> aliases = null;
                JsonObject values = null;

                if (jp.Value.Type == JsonType.String)
                {
                    string value = jp.Value.Get <string>();
                    if (value.StartsWith("{"))
                    {
                        values = JsonObject.Parse(value);
                    }
                    else
                    {
                        aliases = FieldAliasesRepository.GetAliases(value);
                    }
                }
                else if (jp.Value.Type == JsonType.Object)
                {
                    values = jp.Value.Get <JsonObject>();
                }

                if (values != null)
                {
                    aliases = FieldAliasesRepository.GetAliases(values);
                    jp      = new JsonProperty(FIELD_ALIASES, values);
                }

                if (aliases != null)
                {
                    FieldAliases = aliases;
                    _aliases     = jp;
                }
            }

            jp = jo.Property(QUERY_EXPAND);
            if (jp != null)
            {
                IQueryExpander qe     = null;
                JsonObject     values = null;

                if (jp.Value.Type == JsonType.String)
                {
                    string value = jp.Value.Get <string>();
                    if (value.StartsWith("{"))
                    {
                        values = JsonObject.Parse(value);
                    }
                    else
                    {
                        qe = QueryExpanderRepository.GetQueryExpander(value);
                    }
                }
                else if (jp.Value.Type == JsonType.Object)
                {
                    values = jp.Value.Get <JsonObject>();
                }

                if (values != null)
                {
                    qe = QueryExpanderRepository.GetQueryExpander(values);
                    jp = new JsonProperty(QUERY_EXPAND, values);
                }

                if (qe != null)
                {
                    QueryExpander = qe;
                    _qe           = jp;
                }
            }
        }