Ejemplo n.º 1
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);
        }