EasyNet.Solr是简单易用的Solr .Net客户端。

使用指南(基于Solr 4.4.0的例子)

初始化

        static OptimizeOptions optimizeOptions = new OptimizeOptions();
        static ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
        static IUpdateParametersConvert<NamedList> updateParametersConvert = new BinaryUpdateParametersConvert();
        static ISolrUpdateConnection<NamedList, NamedList> solrUpdateConnection = new SolrUpdateConnection<NamedList, NamedList>() { ServerUrl = "http://localhost:8983/solr/" };
        static ISolrUpdateOperations<NamedList> updateOperations = new SolrUpdateOperations<NamedList, NamedList>(solrUpdateConnection, updateParametersConvert) { ResponseWriter = "javabin" };

        static ISolrQueryConnection<NamedList> connection = new SolrQueryConnection<NamedList>() { ServerUrl = "http://localhost:8983/solr/" };
        static ISolrQueryOperations<NamedList> operations = new SolrQueryOperations<NamedList>(connection) { ResponseWriter = "javabin" };

        static IObjectDeserializer<Example> exampleDeserializer = new ExampleDeserializer();
        static ISolrResponseParser<NamedList, QueryResults<Example>> binaryQueryResultsParser = new BinaryQueryResultsParser<Example>(exampleDeserializer);


创建索引

        /// <summary>
        /// 创建索引
        /// </summary>
        static void 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);

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            System.Console.ReadLine();
        }


Set原子操作

        /// <summary>
        /// Set原子操作
        /// </summary>
        static void AtomSet()
        {
            var docs = new List<SolrInputDocument>();
            var doc = new SolrInputDocument();
            doc.Add("id", new SolrInputField("id", "SOLR1000"));

            var setOper = new Hashtable();
            setOper.Add("set", "Solr");

            doc.Add("name", new SolrInputField("name", setOper));

            docs.Add(doc);

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

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            System.Console.ReadLine();
        }


Add原子操作

        /// <summary>
        /// Add原则操作
        /// </summary>
        static void AtomAdd()
        {
            var docs = new List<SolrInputDocument>();
            var doc = new SolrInputDocument();
            doc.Add("id", new SolrInputField("id", "SOLR1000"));

            var addOper = new Hashtable();
            addOper.Add("add", "add a test feature ");

            doc.Add("features", new SolrInputField("features", addOper));

            docs.Add(doc);

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

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            System.Console.ReadLine();
        }


根据id删除操作
        /// <summary>
        /// 根据id删除
        /// </summary>
        static void Delete()
        {
            var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, DelById = new string[] { "SOLR1000" } });
            var header = binaryResponseHeaderParser.Parse(result);

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            System.Console.ReadLine();
        }


查询

        /// <summary>
        /// 查询
        /// </summary>
        static void Query()
        {
            var result = operations.Query("collection1", "/select", SolrQuery.All, null);
            var header = binaryResponseHeaderParser.Parse(result);

            var examples = binaryQueryResultsParser.Parse(result);

            System.Console.WriteLine(string.Format("Query Status:{0} QTime:{1} Total:{2}", header.Status, header.QTime, examples.NumFound));
            System.Console.ReadLine();
        }


实体类及反序列化器

    /// <summary>
    /// 实体类
    /// </summary>
    class Example
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public string[] Features { get; set; }

        public float Price { get; set; }

        public int Popularity { get; set; }

        public bool InStock { get; set; }

        public DateTime IncubationDate { get; set; }
    }

    /// <summary>
    /// 反序列化器
    /// </summary>
    class ExampleDeserializer : IObjectDeserializer<Example>
    {
        public IEnumerable<Example> Deserialize(SolrDocumentList result)
        {
            foreach (SolrDocument doc in result)
            {
                yield return new Example()
                {
                    Id = doc["id"].ToString(),
                    Name = doc["name"].ToString(),
                    Features = (string[])((ArrayList)doc["features"]).ToArray(typeof(string)),
                    Price = (float)doc["price"],
                    Popularity = (int)doc["popularity"],
                    InStock = (bool)doc["inStock"],
                    IncubationDate = (DateTime)doc["incubationdate_dt"]
                };
            }
        }
    }


Move TO:https://github.com/easynet-cn
QQ:124244236
Email:easynet.cn@gmail.com liangtianyu@gmail.com

Last edited May 7, 2015 at 12:49 AM by TerryLiang, version 19