private static ISort GetSortingField(SortingField field) { ISort result; var geoSorting = field as GeoDistanceSortingField; if (geoSorting != null) { result = new GeoDistanceSort { Field = ElasticSearchHelper.ToElasticFieldName(field.FieldName), Points = new[] { geoSorting.Location.ToGeoLocation() } }; } else { result = new SortField { Field = ElasticSearchHelper.ToElasticFieldName(field.FieldName), Order = field.IsDescending ? SortOrder.Descending : SortOrder.Ascending, Missing = "_last", UnmappedType = FieldType.Long, }; } return(result); }
private static AggregationDictionary GetAggregations(SearchRequest request, Properties <IProperties> availableFields) { var result = new Dictionary <string, AggregationContainer>(); if (request?.Aggregations != null) { foreach (var aggregation in request.Aggregations) { var aggregationId = aggregation.Id ?? aggregation.FieldName; var fieldName = ElasticSearchHelper.ToElasticFieldName(aggregation.FieldName); var filter = GetFilterQueryRecursive(aggregation.Filter, availableFields); var termAggregationRequest = aggregation as TermAggregationRequest; var rangeAggregationRequest = aggregation as RangeAggregationRequest; if (termAggregationRequest != null) { AddTermAggregationRequest(result, aggregationId, fieldName, filter, termAggregationRequest); } else if (rangeAggregationRequest != null) { AddRangeAggregationRequest(result, aggregationId, fieldName, rangeAggregationRequest.Values); } } } return(result.Any() ? new AggregationDictionary(result) : null); }
private static QueryContainer CreateGeoDistanceFilter(GeoDistanceFilter geoDistanceFilter) { return(new GeoDistanceQuery { Field = ElasticSearchHelper.ToElasticFieldName(geoDistanceFilter.FieldName), Location = geoDistanceFilter.Location.ToGeoLocation(), Distance = new Distance(geoDistanceFilter.Distance, DistanceUnit.Kilometers), }); }
protected virtual SearchDocument ConvertToProviderDocument(IndexDocument document, Properties <IProperties> properties, string documentType) { var result = new SearchDocument { Id = document.Id }; foreach (var field in document.Fields.OrderBy(f => f.Name)) { var fieldName = ElasticSearchHelper.ToElasticFieldName(field.Name); if (result.ContainsKey(fieldName)) { var newValues = new List <object>(); var currentValue = result[fieldName]; var currentValues = currentValue as object[]; if (currentValues != null) { newValues.AddRange(currentValues); } else { newValues.Add(currentValue); } newValues.AddRange(field.Values); result[fieldName] = newValues.ToArray(); } else { var dictionary = properties as IDictionary <PropertyName, IProperty>; if (dictionary != null && !dictionary.ContainsKey(fieldName)) { // Create new property mapping var providerField = CreateProviderField(field, documentType); ConfigureProperty(providerField, field, documentType); properties.Add(fieldName, providerField); } var isCollection = field.IsCollection || field.Values.Count > 1; var point = field.Value as GeoPoint; var value = point != null ? (isCollection ? field.Values.Select(v => ((GeoPoint)v).ToElasticValue()).ToArray() : point.ToElasticValue()) : (isCollection ? field.Values : field.Value); result.Add(fieldName, value); } } return(result); }
private static QueryContainer CreateRangeFilter(RangeFilter rangeFilter) { QueryContainer result = null; var fieldName = ElasticSearchHelper.ToElasticFieldName(rangeFilter.FieldName); foreach (var value in rangeFilter.Values) { result |= CreateTermRangeQuery(fieldName, value); } return(result); }
private static QueryContainer CreateTermFilter(TermFilter termFilter, Properties <IProperties> availableFields) { var termValues = termFilter.Values; var field = availableFields.Where(kvp => kvp.Key.Name.EqualsInvariant(termFilter.FieldName)).Select(kvp => kvp.Value).FirstOrDefault(); if (field?.Type?.Name?.EqualsInvariant("boolean") == true) { termValues = termValues.Select(v => v.ToLowerInvariant()).ToArray(); } return(new TermsQuery { Field = ElasticSearchHelper.ToElasticFieldName(termFilter.FieldName), Terms = termValues }); }