Criteria that specifies one or more possible values that a field must match in order to select a document.
Inheritance: ElasticLinq.Request.Criteria.SingleFieldCriteria, ITermsCriteria
Esempio n. 1
0
        /// <summary>
        /// Takes a collection of <see cref="ICriteria" /> and if they are all
        /// <see cref="ITermsCriteria" /> for the same field replaces them with a single
        /// <see cref="ITermsCriteria" /> containing all terms for that field.
        /// </summary>
        /// <param name="criteria">collection of <see cref="ICriteria" /> that might be combined.</param>
        /// <returns><see cref="ITermsCriteria" /> containing all terms for that field or null if they can not be combined.</returns>
        static ICriteria CombineTermsForSameField(ICollection <ICriteria> criteria)
        {
            var termCriteria   = criteria.OfType <ITermsCriteria>().ToArray();
            var areAllSameTerm = termCriteria.Length == criteria.Count &&
                                 termCriteria.Select(f => f.Field).Distinct().Count() == 1 &&
                                 termCriteria.All(f => f.IsOrCriteria);

            return(areAllSameTerm
                ? TermsCriteria.Build(termCriteria[0].Field, termCriteria[0].Member, termCriteria.SelectMany(f => f.Values).Distinct())
                : null);
        }
        JObject Build(TermsCriteria criteria)
        {
            var termsCriteria = new JObject(
                new JProperty(criteria.Field,
                    new JArray(criteria.Values.Select(x => mapping.FormatValue(criteria.Member, x)).Cast<object>().ToArray())));

            if (criteria.ExecutionMode.HasValue)
                termsCriteria.Add(new JProperty("execution", criteria.ExecutionMode.GetValueOrDefault().ToString()));

            return new JObject(new JProperty(criteria.Name, termsCriteria));
        }