private static List <IFieldable> AddBounds(SpatialContext ctx, double lng, double lat)
        {
            SpatialPrefixTree grid        = new GeohashPrefixTree(ctx, 11);
            var strategy                  = new RecursivePrefixTreeStrategy(grid, 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);
        }
Beispiel #2
0
        public void GetFieldSettings(List <FlattenedObject> props, Document doc, List <KeyValuePair <string, Analyzer> > analyzers)
        {
            foreach (var p in props)
            {
                if (p == null)
                {
                    continue;
                }
                if (analyzers != null)
                {
                    if (p.Analyzer != null)
                    {
                        analyzers.Add(new KeyValuePair <string, Analyzer>(p.Key, p.Analyzer));
                    }
                }
                if (doc != null)
                {
                    if (p.Value is int || p.Value is int?)
                    {
                        var nf = new Int32Field(p.Key, int.Parse(p.Value.ToString()), p.FieldStoreSetting);
                        doc.Add(nf);
                    }
                    else if (p.Value is long || p.Value is long?)
                    {
                        var nf = new Int64Field(p.Key, long.Parse(p.Value.ToString()), p.FieldStoreSetting);
                        doc.Add(nf);
                    }
                    else if (p.Value is float || p.Value is float?)
                    {
                        var nf = new SingleField(p.Key, float.Parse(p.Value.ToString()), p.FieldStoreSetting);
                        doc.Add(nf);
                    }
                    else if (p.Value is double || p.Value is double?)
                    {
                        var nf = new DoubleField(p.Key, double.Parse(p.Value.ToString()), p.FieldStoreSetting);
                        doc.Add(nf);
                    }
                    else if (p.Spatial)
                    {
                        if (p.Value == null || string.IsNullOrEmpty(p.Value.ToString()))
                        {
                            continue;
                        }
                        var name               = p.Key;// p.Key.IndexOf('.')>-1?p.Key.Substring(0,p.Key.LastIndexOf('.')):p.Key;
                        int maxLevels          = 11;
                        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
                        var strat              = new RecursivePrefixTreeStrategy(grid, name);

                        //var strat = new PointVectorStrategy(ctx,name);
                        var xyArr = p.Value.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (xyArr.Length != 2)
                        {
                            continue;
                        }
                        double x;
                        double y;
                        if (!double.TryParse(xyArr[0], out x) || !double.TryParse(xyArr[1], out y))
                        {
                            continue;
                        }
                        var point = ctx.MakePoint(x, y);
                        //var point = ctx.ReadShape(p.Value.ToString());
                        var fields = strat.CreateIndexableFields(point);
                        fields.ToList().ForEach(x => doc.Add(x));

                        IPoint pt = (IPoint)point;
                        //doc.Add(new StoredField(strat.FieldName, pt.X.ToString(CultureInfo.InvariantCulture) + " " + pt.Y.ToString(CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        string value = p.Value == null ? null : (p.KeepValueCasing ? p.Value.ToString() : p.Value.ToString().ToLower());
                        Field  f     = null;
                        if (p.FieldIndexSetting == Field.Index.ANALYZED || p.FieldIndexSetting == Field.Index.ANALYZED_NO_NORMS)
                        {
                            f = new TextField(p.Key, value ?? string.Empty, p.FieldStoreSetting);
                        }
                        else
                        {
                            f = new StringField(p.Key, value ?? string.Empty, p.FieldStoreSetting);
                        }
                        doc.Add(f);
                    }
                }
            }
        }
        private Document CreateLuceneDocument(DocumentIndex documentIndex)
        {
            var doc = new Document
            {
                // Always store the content item id
                new StringField("ContentItemId", documentIndex.ContentItemId.ToString(), Field.Store.YES)
            };

            foreach (var entry in documentIndex.Entries)
            {
                var store = entry.Options.HasFlag(DocumentIndexOptions.Store)
                            ? Field.Store.YES
                            : Field.Store.NO;

                switch (entry.Type)
                {
                case DocumentIndex.Types.Boolean:
                    // Store "true"/"false" for booleans
                    doc.Add(new StringField(entry.Name, Convert.ToString(entry.Value).ToLowerInvariant(), store));
                    break;

                case DocumentIndex.Types.DateTime:
                    if (entry.Value != null)
                    {
                        if (entry.Value is DateTimeOffset)
                        {
                            doc.Add(new StringField(entry.Name, DateTools.DateToString(((DateTimeOffset)entry.Value).UtcDateTime, DateResolution.SECOND), store));
                        }
                        else
                        {
                            doc.Add(new StringField(entry.Name, DateTools.DateToString(((DateTime)entry.Value).ToUniversalTime(), DateResolution.SECOND), store));
                        }
                    }
                    else
                    {
                        doc.Add(new StringField(entry.Name, "NULL", store));
                    }
                    break;

                case DocumentIndex.Types.Integer:
                    if (entry.Value != null && Int64.TryParse(entry.Value.ToString(), out var value))
                    {
                        doc.Add(new Int64Field(entry.Name, value, store));
                    }
                    else
                    {
                        doc.Add(new StringField(entry.Name, "NULL", store));
                    }

                    break;

                case DocumentIndex.Types.Number:
                    if (entry.Value != null)
                    {
                        doc.Add(new DoubleField(entry.Name, Convert.ToDouble(entry.Value), store));
                    }
                    else
                    {
                        doc.Add(new StringField(entry.Name, "NULL", store));
                    }
                    break;

                case DocumentIndex.Types.Text:
                    if (entry.Value != null && !String.IsNullOrEmpty(Convert.ToString(entry.Value)))
                    {
                        if (entry.Options.HasFlag(DocumentIndexOptions.Analyze))
                        {
                            doc.Add(new TextField(entry.Name, Convert.ToString(entry.Value), store));
                        }
                        else
                        {
                            doc.Add(new StringField(entry.Name, Convert.ToString(entry.Value), store));
                        }
                    }
                    else
                    {
                        if (entry.Options.HasFlag(DocumentIndexOptions.Analyze))
                        {
                            doc.Add(new TextField(entry.Name, "NULL", store));
                        }
                        else
                        {
                            doc.Add(new StringField(entry.Name, "NULL", store));
                        }
                    }
                    break;

                case DocumentIndex.Types.GeoPoint:
                    var strategy = new RecursivePrefixTreeStrategy(_grid, entry.Name);
                    if (entry.Value != null && entry.Value is DocumentIndex.GeoPoint point)
                    {
                        var geoPoint = _ctx.MakePoint((double)point.Longitude, (double)point.Latitude);
                        foreach (var field in strategy.CreateIndexableFields(geoPoint))
                        {
                            doc.Add(field);
                        }

                        if (entry.Options.HasFlag(DocumentIndexOptions.Store))
                        {
                            doc.Add(new StoredField(strategy.FieldName, $"{point.Latitude},{point.Longitude}"));
                        }
                    }
                    else
                    {
                        doc.Add(new StringField(strategy.FieldName, "NULL", store));
                    }
                    break;
                }
            }

            return(doc);
        }
Beispiel #4
0
        public bool Index(string indexName, IEnumerable <IDictionary <string, object> > items, bool archive = true)
        {
            if (!IndexExists(indexName))
            {
                throw new Exception("Index not exists");
            }

            if (items == null || items.Count() == 0)
            {
                return(true);
            }

            var mapping = _resources.GetMapping(indexName);

            List <Document> docs = new List <Document>();

            foreach (var item in items)
            {
                if (item == null)
                {
                    continue;
                }

                var itemType = item.GetType();
                var doc      = new Document();

                foreach (var field in mapping.Fields)
                {
                    #region AutoSetValues

                    switch (field.FieldType)
                    {
                    case FieldTypes.GuidType:
                        doc.Add(new StringField(
                                    field.Name,
                                    Guid.NewGuid().ToString("N").ToLower(),
                                    Field.Store.YES));
                        continue;
                    }

                    #endregion

                    if (!item.ContainsKey(field.Name))
                    {
                        continue;
                    }

                    var value = item[field.Name];
                    if (value == null)
                    {
                        continue;
                    }

                    switch (field.FieldType)
                    {
                    case FieldTypes.StringType:
                        if (field.Index)
                        {
                            doc.Add(new StringField(
                                        field.Name,
                                        value.ToString(),
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, value.ToString()));
                        }
                        break;

                    case FieldTypes.TextType:
                        if (field.Index)
                        {
                            doc.Add(new TextField(
                                        field.Name,
                                        value.ToString(),
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, value.ToString()));
                        }
                        break;

                    case FieldTypes.Int32Type:
                        if (field.Index)
                        {
                            doc.Add(new Int32Field(
                                        field.Name,
                                        value.ToInt32(),
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, Convert.ToInt32(value)));
                        }
                        break;

                    case FieldTypes.DoubleType:
                        if (field.Index)
                        {
                            doc.Add(new DoubleField(
                                        field.Name,
                                        value.ToDouble(),
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, Convert.ToDouble(value)));
                        }
                        break;

                    case FieldTypes.SingleType:
                        if (field.Index)
                        {
                            doc.Add(new SingleField(
                                        field.Name,
                                        value.ToSingle(),
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, Convert.ToSingle(value)));
                        }
                        break;

                    case FieldTypes.DateTimeType:
                        if (field.Index)
                        {
                            value = DateTools.DateToString(Convert.ToDateTime(value.ToString()).ToUniversalTime(), DateTools.Resolution.SECOND);
                            doc.Add(new StringField(
                                        field.Name,
                                        (string)value,
                                        field.Store ? Field.Store.YES : Field.Store.NO));
                        }
                        else
                        {
                            doc.Add(new Lucene.Net.Documents.StoredField(field.Name, value.ToString()));
                        }
                        break;

                    case FieldTypes.GeoType:
                        InitSpatial();

                        GeoType geoValue = GeoType.Parse(value);

                        if (geoValue is GeoPoint && geoValue.IsValid())
                        {
                            var geoPoint = (GeoPoint)geoValue;
                            var strategy = new RecursivePrefixTreeStrategy(_tree, field.Name);
                            var point    = _spatialContext.MakePoint(geoPoint.Longidute, geoPoint.Latitude);
                            foreach (var f in strategy.CreateIndexableFields(point))
                            {
                                doc.Add(f);
                            }
                            if (field.Store)
                            {
                                doc.Add(new Lucene.Net.Documents.StoredField(field.Name, geoPoint.ToString()));
                            }
                        }
                        break;
                    }
                }
                docs.Add(doc);
            }

            //lock (_writeLocker)
            {
                var writer = _resources.GetIndexWriter(indexName);
                writer.AddDocuments(docs);

                writer.Flush(triggerMerge: false, applyAllDeletes: false);
            }

            if (archive)
            {
                _archive.Index(indexName, items);
            }

            return(true);
        }