예제 #1
0
        private static List <IFieldable> AddPoint(double lng, double lat)
        {
            SpatialContext ctx = SpatialContext.GEO;

            var strategy = new PointVectorStrategy(ctx, Sitecore.ContentSearch.Spatial.Common.Constants.LocationFieldName);//var strategy = new PrefixTreeStrategy(ctx, Sitecore.ContentSearch.Spatial.Common.Constants.LocationFieldName);

            List <IFieldable> pointFields = new List <IFieldable>();
            Point             shape       = ctx.MakePoint(lng, lat);

            foreach (var f in strategy.CreateIndexableFields(shape))
            {
                if (f != null)
                {
                    pointFields.Add(f);
                }
            }
            return(pointFields);
        }
예제 #2
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);
        }