Ejemplo n.º 1
0
        public override void Visit(TermNode node, IQueryVisitorContext context)
        {
            var    validationInfo = context.GetValidationInfo();
            string field          = null;

            if (!String.IsNullOrEmpty(node.Field))
            {
                field = String.Equals(validationInfo.QueryType, QueryType.Query) ? node.GetFullName() : node.Field;
                if (!field.StartsWith("@"))
                {
                    validationInfo.ReferencedFields.Add(field);
                }
            }
            else
            {
                if (String.Equals(validationInfo.QueryType, QueryType.Aggregation))
                {
                    validationInfo.IsValid = false;
                }

                var nameParts = node.GetNameParts();
                if (nameParts.Length == 0)
                {
                    validationInfo.ReferencedFields.Add("_all");
                }
            }

            AddOperation(validationInfo, node.GetOperationType(), field);
        }
        public static QueryBase GetDefaultQuery(this TermNode node, IQueryVisitorContext context)
        {
            if (!(context is IElasticQueryVisitorContext elasticContext))
            {
                throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context));
            }

            QueryBase query;
            string    nodeFullName = node.GetFullName();

            if (elasticContext.IsPropertyAnalyzed(nodeFullName))
            {
                var fields = !String.IsNullOrEmpty(nodeFullName) ? new[] { nodeFullName } : elasticContext.DefaultFields;

                if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*"))
                {
                    query = new QueryStringQuery {
                        Fields = fields,
                        AllowLeadingWildcard = false,
                        AnalyzeWildcard      = true,
                        Query = node.UnescapedTerm
                    };
                }
                else
                {
                    if (fields.Length == 1)
                    {
                        if (node.IsQuotedTerm)
                        {
                            query = new MatchPhraseQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                        else
                        {
                            query = new MatchQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                    }
                    else
                    {
                        query = new MultiMatchQuery {
                            Fields = fields,
                            Query  = node.UnescapedTerm
                        };
                        if (node.IsQuotedTerm)
                        {
                            (query as MultiMatchQuery).Type = TextQueryType.Phrase;
                        }
                    }
                }
            }
            else
            {
                query = new TermQuery {
                    Field = nodeFullName,
                    Value = node.UnescapedTerm
                };
            }

            return(query);
        }