示例#1
0
        /// <summary>
        /// 创建索引
        /// </summary>
        public void InitIndex(object data)
        {
            var       docs = new List <SolrInputDocument>();
            DataTable list = data as DataTable;

            foreach (DataRow pro in list.Rows)
            {
                var model = new SolrInputDocument();

                PropertyInfo[] properites = typeof(IndexCustomerTagModel).GetProperties(); //得到实体类属性的集合
                //string[] dateFields = { "CreateDate" };
                string field = string.Empty;                                               //存储fieldname
                foreach (PropertyInfo propertyInfo in properites)                          //遍历数组
                {
                    object val = pro[propertyInfo.Name];
                    if (val != DBNull.Value)
                    {
                        model.Add(propertyInfo.Name, new SolrInputField(propertyInfo.Name, val));
                    }
                }
                docs.Add(model);
            }

            var result = updateOperations.Update("core1", "/update", new UpdateOptions()
            {
                Docs = docs, CommitOptions = commitOptions
            });
            var header = binaryResponseHeaderParser.Parse(result);

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            //System.Console.ReadLine();
        }
示例#2
0
        /// <summary>
        /// 创建或更新索引  travelpart
        /// </summary>
        /// <param name="parts"></param>
        /// <returns></returns>
        public static int NewTravelPartIndex(TravelParts parts)
        {
            var docs = new List <SolrInputDocument>();
            var doc  = new SolrInputDocument();

            doc.Add("id", new SolrInputField("id", "travelparts_" + parts.Id));
            doc.Add("TravelId", new SolrInputField("TravelId", parts.TravelId));
            doc.Add("UserId", new SolrInputField("UserId", parts.UserId));
            doc.Add("PartType", new SolrInputField("PartType", parts.PartType));
            doc.Add("Description", new SolrInputField("Description", parts.Description));
            doc.Add("PartUrl", new SolrInputField("PartUrl", parts.PartUrl));
            doc.Add("Longitude", new SolrInputField("Longitude", parts.Longitude));
            doc.Add("Latitude", new SolrInputField("Latitude", parts.Latitude));
            doc.Add("Height", new SolrInputField("Height", parts.Height));
            doc.Add("Area", new SolrInputField("Area", parts.Area));
            doc.Add("CreateTime", new SolrInputField("CreateTime", parts.CreateTime));
            docs.Add(doc);
            var result = updateOperations.Update(CoreName, "/update", new UpdateOptions()
            {
                OptimizeOptions = optimizeOptions, Docs = docs
            });
            var header = binaryResponseHeaderParser.Parse(result);

            return(header.Status); //返回状态码。0表示成功
        }
        public IEnumerable <SolrInputDocument> Serialize(IEnumerable <Product> objs)
        {
            IList <SolrInputDocument> docs = new List <SolrInputDocument>();

            foreach (var obj in objs)
            {
                SolrInputDocument doc = new SolrInputDocument();

                doc.Add("id", new SolrInputField("id", obj.Id));
                if (!string.IsNullOrEmpty(obj.ProductName))
                {
                    doc.Add("ProductName", new SolrInputField("ProductName", obj.ProductName));
                }
                if (obj.ProductTypeId > 0)
                {
                    doc.Add("ProductTypeId", new SolrInputField("ProductTypeId", obj.ProductTypeId));
                }
                if (obj.ProductAttributeId > 0)
                {
                    doc.Add("ProductAttributeId", new SolrInputField("ProductAttributeId", obj.ProductAttributeId));
                }
                if (obj.Rank > 0)
                {
                    doc.Add("Rank", new SolrInputField("Rank", obj.Rank));
                }
                if (obj.EffectDate > DateTime.MinValue)
                {
                    doc.Add("EffectDate", new SolrInputField("EffectDate", obj.EffectDate));
                }
                if (obj.ExpireDate > DateTime.MinValue)
                {
                    doc.Add("ExpireDate", new SolrInputField("ExpireDate", obj.ExpireDate));
                }
                if (obj.Coords != null && obj.Coords.Length > 0)
                {
                    doc.Add("Coords", new SolrInputField("Coords", obj.Coords));
                }
                if (!string.IsNullOrEmpty(obj.ProductDescription))
                {
                    doc.Add("ProductDescription", new SolrInputField("ProductDescription", obj.ProductDescription));
                }
                if (obj.TourSpotName != null && obj.TourSpotName.Length > 0)
                {
                    doc.Add("TourSpotName", new SolrInputField("TourSpotName", obj.TourSpotName));
                }
                if (obj.SalesPrice > 0)
                {
                    doc.Add("SalesPrice", new SolrInputField("SalesPrice", obj.SalesPrice));
                }

                docs.Add(doc);
            }

            return(docs);
        }
        /// <summary>
        /// 原子操作索引
        /// </summary>
        /// <param name="collectionName">操作的集合(solr中的集体名称)</param>
        /// <param name="uniqueKeyName">主键名称(solr中的主键)</param>
        /// <param name="uniqueKeyValue">主键值(solr中的主键值)</param>
        /// <param name="setItem">需要操作的集体(key为字段名称,value为值)</param>
        /// <param name="operationType">操作类型</param>
        /// <returns>操作结果</returns>
        public IndexOperationResponse AtomOperateIndex(string collectionName, string uniqueKeyName, object uniqueKeyValue, Dictionary <string, object> setItem, IndexOperationType operationType = IndexOperationType.Update)
        {
            var docs = new List <SolrInputDocument>();
            var doc  = new SolrInputDocument();

            try
            {
                doc.Add(uniqueKeyName, new SolrInputField(uniqueKeyName, uniqueKeyValue));
                foreach (var item in setItem)
                {
                    var setOper = new Hashtable();
                    switch (operationType)
                    {
                    case IndexOperationType.Add:
                        setOper.Add("add", item.Value);
                        break;

                    case IndexOperationType.Update:
                        setOper.Add("set", item.Value);
                        break;

                    default:
                        setOper.Add("set", item.Value);
                        break;
                    }
                    doc.Add(item.Key, new SolrInputField(item.Key, setOper));
                    docs.Add(doc);
                }

                collectionName = string.IsNullOrEmpty(collectionName) ? searchConfig.Collection : collectionName;
                var result = updateOperations.Update(collectionName, "/update", new UpdateOptions()
                {
                    OptimizeOptions = optimizeOptions, Docs = docs
                });
                var header = binaryResponseHeaderParser.Parse(result);
                return(new IndexOperationResponse
                {
                    IsSuccess = header.Status == 0,
                    Message = string.Empty,
                    QTime = header.QTime,
                    Status = header.Status
                });
            }
            catch (Exception ex)
            {
                return(new IndexOperationResponse
                {
                    IsSuccess = false,
                    Message = ex.Message,
                    QTime = -1,
                    Status = -1
                });
            }
        }
示例#5
0
        public IList <int> Update(DataTable objUpdateTable)
        {
            IList <int> list = new List <int>();
            List <SolrInputDocument> list2 = new List <SolrInputDocument>();

            foreach (DataRow dataRow in objUpdateTable.Rows)
            {
                SolrInputDocument solrInputDocument = new SolrInputDocument();
                foreach (DataColumn dataColumn in objUpdateTable.Columns)
                {
                    if (dataRow[dataColumn.ColumnName] != DBNull.Value)
                    {
                        solrInputDocument.Add(dataColumn.ColumnName, new SolrInputField(dataColumn.ColumnName, this.GetConvertValue(dataColumn.DataType, dataRow[dataColumn.ColumnName])));
                    }
                    else
                    {
                        solrInputDocument.Add(dataColumn.ColumnName, new SolrInputField(dataColumn.ColumnName, this.GetDefaultValue(dataColumn.DataType)));
                    }
                }
                list2.Add(solrInputDocument);
            }
            IList <int> result;

            if (list2.Count <= 0)
            {
                result = list;
            }
            else
            {
                CommitOptions value = default(CommitOptions);
                IObjectSerializer <SolrInputDocument> objectSerializer = new ObjectSerializerTable();
                ResponseHeader responseHeader = this.Update(new UpdateOptions
                {
                    Docs          = objectSerializer.Serialize(list2),
                    CommitOptions = new CommitOptions?(value)
                });
                if (responseHeader != null)
                {
                    list.Add(responseHeader.Status);
                    list.Add(responseHeader.QTime);
                    ConsoleHelper.WriteLine(string.Concat(new object[]
                    {
                        "更新Solr:",
                        this.SolrServerUrl,
                        "成功,状态:",
                        responseHeader.Status,
                        "执行时间:",
                        responseHeader.QTime
                    }), ConsoleColor.Yellow, "");
                }
                result = list;
            }
            return(result);
        }
示例#6
0
文件: Program.cs 项目: hiiru/Mizore
        private static void CreateDoc(ISolrServerHandler server, string docId)
        {
            var doc = new SolrInputDocument();

            doc.Fields.Add("id", new SolrInputField("id", docId));
            doc.Fields.Add("name", new SolrInputField("name", "Test Document with ID " + docId));

            var updateRequest = new UpdateRequest(server.GetUriBuilder()).Add(doc).Commit(true);

            server.Request <UpdateResponse>(updateRequest);
            Console.WriteLine("doc " + docId + " added");
        }
示例#7
0
        public UpdateRequest Add(SolrInputDocument doc)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            if (_documents == null)
            {
                _documents = new List <SolrInputDocument>();
            }
            _documents.Add(doc);
            _changed = true;
            return(this);
        }
示例#8
0
        public SolrInputDocument GetDocument(T obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            var doc = new SolrInputDocument();

            foreach (var member in mapping)
            {
                if (member.ReadOnly)
                {
                    continue;
                }
                var value = member.Get(obj);
                doc.Fields.Add(member.SolrField, new SolrInputField(member.SolrField, value, member.SolrFieldBoost.HasValue ? member.SolrFieldBoost.Value : 1f));
            }
            return(doc);
        }
示例#9
0
        public static int NewTravelIndex(Travels travle)
        {
            var docs = new List <SolrInputDocument>();
            var doc  = new SolrInputDocument();

            doc.Add("id", new SolrInputField("id", "travels_" + travle.Id));  //索引中Id不能重复
            doc.Add("userid", new SolrInputField("userid", travle.UserId));
            doc.Add("TravelName", new SolrInputField("TravelName", travle.TravelName));
            doc.Add("CreateTime", new SolrInputField("CreateTime", travle.CreateTime));
            doc.Add("UpdateTime", new SolrInputField("UpdateTime", travle.UpdateTime));
            doc.Add("CoverImage", new SolrInputField("CoverImage", travle.CoverImage));
            docs.Add(doc);
            var result = updateOperations.Update(CoreName, "/update", new UpdateOptions()
            {
                OptimizeOptions = optimizeOptions, Docs = docs
            });
            var header = binaryResponseHeaderParser.Parse(result);

            return(header.Status); //返回状态码。0表示成功
        }
        private IList <NamedList> SolrInputDocumentToList(SolrInputDocument doc)
        {
            var l = new List <NamedList>();

            var nl = new NamedList();

            nl.Add("boost", doc.Boost == 1.0f ? null : doc.Boost);
            l.Add(nl);

            foreach (var field in doc.Values)
            {
                nl = new NamedList();
                nl.Add("name", field.Name);
                nl.Add("val", field.Value);
                nl.Add("boost", field.Boost);

                l.Add(nl);
            }

            return(l);
        }
示例#11
0
        private void WriteSolrInputDocument(SolrInputDocument sid, SolrBinaryStream stream)
        {
            var length = sid.Fields.Count + (sid.ChildDocuments != null ? sid.ChildDocuments.Count : 0);

            WriteTag(SOLRINPUTDOC, stream, length);
            WriteFloat(sid.DocBoost.HasValue ? sid.DocBoost.Value : 1f, stream);
            foreach (SolrInputField field in sid.Fields.Values)
            {
                if (field.Boost != 1.0f)
                {
                    WriteFloat(field.Boost, stream);
                }
                WriteExternString(field.Name, stream);
                WriteVal(field.Value, stream);
            }
            if (sid.ChildDocuments != null)
            {
                foreach (var doc in sid.ChildDocuments)
                {
                    WriteVal(doc, stream);
                }
            }
        }
示例#12
0
        /// <summary>
        /// 创建索引
        /// </summary>
        public static string Index()
        {
            var docs = new List <SolrInputDocument>();
            var doc  = new SolrInputDocument();

            doc.Add("id", new SolrInputField("id", "SOLR1000"));
            doc.Add("name", new SolrInputField("name", "Solr, the Enterprise Search Server"));
            doc.Add("features", new SolrInputField("features", new String[] { "Advanced Full-Text Search Capabilities using Lucene", "Optimized for High Volume Web Traffic", "Standards Based Open Interfaces - XML and HTTP", "Comprehensive HTML Administration Interfaces", "Scalability - Efficient Replication to other Solr Search Servers", "Flexible and Adaptable with XML configuration and Schema", "Good unicode support: h&#xE9;llo (hello with an accent over the e)" }));
            doc.Add("price", new SolrInputField("price", 0.0f));
            doc.Add("popularity", new SolrInputField("popularity", 10));
            doc.Add("inStock", new SolrInputField("inStock", true));
            doc.Add("incubationdate_dt", new SolrInputField("incubationdate_dt", new DateTime(2006, 1, 17, 0, 0, 0, DateTimeKind.Utc)));

            docs.Add(doc);

            var result = updateOperations.Update("collection1", "/update", new UpdateOptions()
            {
                OptimizeOptions = optimizeOptions, Docs = docs
            });
            var header = binaryResponseHeaderParser.Parse(result);

            return(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
        }
示例#13
0
        private object ReadSolrInputDocument(SolrBinaryStream stream)
        {
            int sz  = ReadVInt(stream);
            var sid = new SolrInputDocument();

            sid.DocBoost = (float?)ReadVal(stream);
            for (int i = 0; i < sz; i++)
            {
                var    boost = 1.0f;
                string name;
                var    obj = ReadVal(stream); // could be a boost, a field name, or a child document
                if (obj is float)
                {
                    boost = (float)obj;
                    name  = (string)ReadVal(stream);
                }
                else if (obj is SolrInputDocument)
                {
                    if (sid.ChildDocuments == null)
                    {
                        sid.ChildDocuments = new List <SolrInputDocument>();
                    }
                    sid.ChildDocuments.Add(obj as SolrInputDocument);
                    continue;
                }
                else
                {
                    name = (string)obj;
                }
                var value = ReadVal(stream);
                sid.Fields.Add(name, new SolrInputField(name)
                {
                    Boost = boost, Value = value
                });
            }
            return(sid);
        }
示例#14
0
 public void WriteSolrInputDocument(SolrInputDocument sdoc)
 {
     WriteTag(SOLRINPUTDOC, sdoc.Count);
     WriteFloat(sdoc.Boost.Value);
     foreach (SolrInputField inputField in sdoc.Values)
     {
         if (inputField.Boost != 1.0f)
         {
             WriteFloat(inputField.Boost.Value);
         }
         WriteExternString(inputField.Name);
         WriteVal(inputField.Value);
     }
 }
示例#15
0
 public SolrInputDocument ReadSolrInputDocument(FastInputStream dis)
 {
     int sz = ReadVInt(dis);
     float? docBoost = (float?)ReadVal(dis);
     SolrInputDocument sdoc = new SolrInputDocument();
     sdoc.Boost = docBoost;
     for (int i = 0; i < sz; i++)
     {
         float boost = 1.0f;
         String fieldName;
         Object boostOrFieldName = ReadVal(dis);
         if (boostOrFieldName is float)
         {
             boost = (float)boostOrFieldName;
             fieldName = (String)ReadVal(dis);
         }
         else
         {
             fieldName = (String)boostOrFieldName;
         }
         Object fieldVal = ReadVal(dis);
         sdoc[fieldName] = new SolrInputField(fieldName, fieldVal, boost);
     }
     return sdoc;
 }