示例#1
0
        private static SortField GetSortField(SortingField field, ICollection <string> availableFields)
        {
            var dataType = availableFields.Contains(LuceneSearchHelper.GetFacetableFieldName(field.FieldName)) ? SortField.DOUBLE : SortField.STRING;
            var result   = new SortField(LuceneSearchHelper.ToLuceneFieldName(field.FieldName), dataType, field.IsDescending);

            return(result);
        }
        private static string GetFacetFieldName(string originalName, ICollection <string> availableFields)
        {
            var result = LuceneSearchHelper.GetFacetableFieldName(originalName);

            if (!availableFields.Contains(result))
            {
                result = LuceneSearchHelper.ToLuceneFieldName(originalName);
            }

            return(result);
        }
        private static SearchDocument ToSearchDocument(Document providerDocument, ICollection <string> availableFields)
        {
            var result = new SearchDocument();

            var documentFields = providerDocument.GetFields();

            foreach (var field in documentFields)
            {
                var stringValue = field.StringValue;

                if (field.Name.EqualsInvariant(LuceneSearchHelper.KeyFieldName))
                {
                    result.Id = stringValue;
                }
                else
                {
                    var isDateTimeField = availableFields.Contains(LuceneSearchHelper.GetDateTimeFieldName(field.Name));
                    if (isDateTimeField)
                    {
                        var ticks = long.Parse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
                        stringValue = new DateTime(ticks, DateTimeKind.Utc).ToString("O");
                    }

                    if (result.ContainsKey(field.Name)) // convert to array
                    {
                        var newValues = new List <object>();

                        var currentValue  = result[field.Name];
                        var currentValues = currentValue as object[];

                        if (currentValues != null)
                        {
                            newValues.AddRange(currentValues);
                        }
                        else
                        {
                            newValues.Add(currentValue);
                        }

                        newValues.Add(stringValue);
                        result[field.Name] = newValues.ToArray();
                    }
                    else
                    {
                        result.Add(field.Name, stringValue);
                    }
                }
            }

            return(result);
        }
        private static AggregationResponse CreateRangeAggregationResponse(RangeAggregationRequest rangeAggregationRequest, IndexSearcher searcher, ICollection <string> availableFields)
        {
            AggregationResponse result = null;

            if (rangeAggregationRequest != null)
            {
                var fieldName    = LuceneSearchHelper.ToLuceneFieldName(rangeAggregationRequest.FieldName);
                var valueFilters = rangeAggregationRequest.Values?.ToDictionary(v => v.Id, v => LuceneSearchFilterBuilder.CreateRangeFilterForValue(fieldName, v.Lower, v.Upper, v.IncludeLower, v.IncludeUpper));

                result = GetAggregation(rangeAggregationRequest, valueFilters, searcher, false, null, availableFields);
            }

            return(result);
        }
示例#5
0
        private static Filter CreateTermFilter(TermFilter termFilter, ICollection <string> availableFields)
        {
            Filter result = null;

            if (termFilter?.FieldName != null && termFilter.Values != null)
            {
                var isBooleanField = availableFields.Contains(LuceneSearchHelper.GetBooleanFieldName(termFilter.FieldName));
                var values         = termFilter.Values.Select(v => GetFilterValue(v, isBooleanField)).ToArray();

                var fieldName = LuceneSearchHelper.ToLuceneFieldName(termFilter.FieldName);
                result = CreateTermsFilter(fieldName, values);
            }

            return(result);
        }
示例#6
0
        private static Filter CreateRangeFilter(RangeFilter rangeFilter)
        {
            Filter result = null;

            if (rangeFilter?.FieldName != null && rangeFilter.Values != null)
            {
                var fieldName = LuceneSearchHelper.ToLuceneFieldName(rangeFilter.FieldName);

                var childFilters = rangeFilter.Values.Select(v => CreateRangeFilterForValue(fieldName, v))
                                   .Where(f => f != null)
                                   .ToArray();

                result = JoinNonEmptyFilters(childFilters, Occur.SHOULD);
            }

            return(result);
        }
示例#7
0
        private static Filter CreateGeoDistanceFilter(GeoDistanceFilter geoDistanceFilter)
        {
            Filter result = null;

            if (geoDistanceFilter?.FieldName != null && geoDistanceFilter.Location != null)
            {
                var spatialContext = SpatialContext.GEO;
                var distance       = DistanceUtils.Dist2Degrees(geoDistanceFilter.Distance, DistanceUtils.EARTH_MEAN_RADIUS_KM);
                var searchArea     = spatialContext.MakeCircle(geoDistanceFilter.Location.Longitude, geoDistanceFilter.Location.Latitude, distance);
                var spatialArgs    = new SpatialArgs(SpatialOperation.Intersects, searchArea);

                var fieldName = LuceneSearchHelper.ToLuceneFieldName(geoDistanceFilter.FieldName);
                var strategy  = new PointVectorStrategy(spatialContext, fieldName);
                result = strategy.MakeFilter(spatialArgs);
            }

            return(result);
        }
示例#8
0
        protected virtual IList <IIndexableField> ConvertToProviderFields(IndexDocumentField field)
        {
            // TODO: Introduce and use metadata describing value type

            var result = new List <IIndexableField>();

            var fieldName = LuceneSearchHelper.ToLuceneFieldName(field.Name);
            var store     = field.IsRetrievable ? Field.Store.YES : Field.Store.NO;

            //var index = field.IsSearchable ? Field.Index.ANALYZED : field.IsFilterable ? Field.Index.NOT_ANALYZED : Field.Index.NO;

            if (field.Value is string)
            {
                foreach (var value in field.Values)
                {
                    result.Add(new StringField(fieldName, (string)value, store));

                    if (field.IsSearchable)
                    {
                        result.Add(new StringField(LuceneSearchHelper.SearchableFieldName, (string)value, Field.Store.NO));
                    }
                }
            }
            else if (field.Value is bool)
            {
                var booleanFieldName = LuceneSearchHelper.GetBooleanFieldName(field.Name);

                foreach (var value in field.Values)
                {
                    var stringValue = value.ToStringInvariant();
                    result.Add(new StringField(fieldName, stringValue, store));
                    result.Add(new StringField(booleanFieldName, stringValue, Field.Store.NO));
                }
            }
            else if (field.Value is DateTime)
            {
                var dateTimeFieldName = LuceneSearchHelper.GetDateTimeFieldName(field.Name);

                foreach (var value in field.Values)
                {
                    //TODO
                    //var numericField = new NumericField(fieldName, store, index != Field.Index.NO);
                    //numericField.SetLongValue(((DateTime)value).Ticks);
                    //result.Add(numericField);
                    result.Add(new StringField(dateTimeFieldName, value.ToStringInvariant(), Field.Store.NO));
                }
            }
            else if (field.Value is GeoPoint)
            {
                var geoPoint = (GeoPoint)field.Value;

                result.Add(new StringField(fieldName, geoPoint.ToString(), Field.Store.YES));

                var shape    = _spatialContext.MakePoint(geoPoint.Longitude, geoPoint.Latitude);
                var strategy = new PointVectorStrategy(_spatialContext, fieldName);

                foreach (var f in strategy.CreateIndexableFields(shape))
                {
                    result.Add(f);
                }
            }
            else
            {
                double t;
                if (double.TryParse(field.Value.ToStringInvariant(), NumberStyles.Float, CultureInfo.InvariantCulture, out t))
                {
                    var facetableFieldName = LuceneSearchHelper.GetFacetableFieldName(field.Name);

                    foreach (var value in field.Values)
                    {
                        var stringValue = value.ToStringInvariant();

                        //TODO
                        //var numericField = new NumericField(fieldName, store, index != Field.Index.NO);
                        //numericField.SetDoubleValue(double.Parse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture));
                        //result.Add(numericField);

                        result.Add(new StringField(facetableFieldName, stringValue, Field.Store.NO));
                    }
                }
                else
                {
                    result.AddRange(field.Values.Select(value => new StringField(fieldName, value.ToStringInvariant(), store)));
                }
            }

            return(result);
        }
        protected virtual IList <IIndexableField> ConvertToProviderFields(IndexDocumentField field)
        {
            var result = new List <IIndexableField>();

            var fieldName = LuceneSearchHelper.ToLuceneFieldName(field.Name);
            var store     = field.IsRetrievable ? Field.Store.YES : Field.Store.NO;

            switch (field.Value)
            {
            case string _:
                foreach (var value in field.Values)
                {
                    result.Add(new StringField(fieldName, (string)value, store));

                    if (field.IsSearchable)
                    {
                        result.Add(new StringField(LuceneSearchHelper.SearchableFieldName, (string)value, Field.Store.NO));
                    }
                }
                break;

            case bool _:
                var booleanFieldName = LuceneSearchHelper.GetBooleanFieldName(field.Name);

                foreach (var value in field.Values)
                {
                    var stringValue = value.ToStringInvariant();
                    result.Add(new StringField(fieldName, stringValue, store));
                    result.Add(new StringField(booleanFieldName, stringValue, Field.Store.NO));
                }
                break;

            case DateTime _:
                var dateTimeFieldName = LuceneSearchHelper.GetDateTimeFieldName(field.Name);

                foreach (var value in field.Values)
                {
                    var numericField = new Int64Field(fieldName, ((DateTime)value).Ticks, store);
                    result.Add(numericField);
                    result.Add(new StringField(dateTimeFieldName, value.ToStringInvariant(), Field.Store.NO));
                }
                break;

            case GeoPoint _:
                var geoPoint = (GeoPoint)field.Value;

                result.Add(new StringField(fieldName, geoPoint.ToString(), Field.Store.YES));

                var shape    = _spatialContext.MakePoint(geoPoint.Longitude, geoPoint.Latitude);
                var strategy = new PointVectorStrategy(_spatialContext, fieldName);

                result.AddRange(strategy.CreateIndexableFields(shape));
                break;

            default:
                if (double.TryParse(field.Value.ToStringInvariant(), NumberStyles.Float, CultureInfo.InvariantCulture, out _))
                {
                    var facetableFieldName = LuceneSearchHelper.GetFacetableFieldName(field.Name);

                    foreach (var value in field.Values)
                    {
                        var stringValue = value.ToStringInvariant();

                        var doubleField = new DoubleField(fieldName, double.Parse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture), store);

                        result.Add(doubleField);

                        result.Add(new StringField(facetableFieldName, stringValue, Field.Store.NO));
                    }
                }
                else
                {
                    result.AddRange(field.Values.Select(value => new StringField(fieldName, value.ToStringInvariant(), store)));
                }
                break;
            }

            return(result);
        }