电商论道---(3).net开发solr

2020-12-13 01:47

阅读:515

标签:des   datagridview   Lucene   style   blog   class   

           这一节我们看下怎样把查询数据放到服务器端存储,这里我们需要使用客户端工具来操作与服务端数据打交道,网上有好多基于.NET开发的SOLR客户端,我们这里选择easynet.solr,很方便的一个DLL,

      下载地址:http://easynet.codeplex.com/SourceControl/latest

      这一节主要包括Solr数据索引的添加,修改,删除,查询等简单功能。

      我们先来看下效果图

      soscw.com,搜素材

      1.创建对象

 

       

 public class Product
    {

        /// 
        /// 产品编码
        /// 
        public string ProductCode { get; set; }

        /// 
        /// 产品名称
        /// 
        public string ProductName { get; set; }
    }


 

 

 

      2.配置schema.xml

         找到tomcat下的schema.xml配置文件 ,具体位置:C:\Program Files\Apache Software Foundation\Tomcat 8.0\solr\collection1\conf

 

         添加字段节点,这里我们设置ProductCode为主键,注意设置 

ProductCode
        

ProductCode


          3.创建序列化器

            

 public class Product
    {

        /// 
        /// 产品编码
        /// 
        public string ProductCode { get; set; }

        /// 
        /// 产品名称
        /// 
        public string ProductName { get; set; }
    }

    /// 
    /// 反序列化器
    /// 
    class ProductDeserializer : IObjectDeserializer
    {
        public IEnumerable Deserialize(SolrDocumentList result)
        {
            foreach (SolrDocument doc in result)
            {
                yield return new Product()
                {
                    ProductCode = doc["ProductCode"].ToString(),
                    ProductName = doc["ProductName"].ToString()

                };
            }
        }
    }


          4.查询数据

            

        static OptimizeOptions optimizeOptions = new OptimizeOptions();
        static ISolrResponseParser binaryResponseHeaderParser = new BinaryResponseHeaderParser();
        static IUpdateParametersConvert updateParametersConvert = new BinaryUpdateParametersConvert();
        static ISolrUpdateConnection solrUpdateConnection = new SolrUpdateConnection() { ServerUrl = "http://localhost:8040/Solr/" };
        static ISolrUpdateOperations updateOperations = new SolrUpdateOperations(solrUpdateConnection, updateParametersConvert) { ResponseWriter = "javabin" };

        static ISolrQueryConnection connection = new SolrQueryConnection() { ServerUrl = "http://localhost:8040/Solr/" };
        static ISolrQueryOperations operations = new SolrQueryOperations(connection) { ResponseWriter = "javabin" };

        static IObjectDeserializer exampleDeserializer = new ProductDeserializer();
        static ISolrResponseParser> binaryQueryResultsParser = new BinaryQueryResultsParser(exampleDeserializer);

        public void Query()
        {
            //查询
            var result = operations.Query("collection1", "/select", SolrQuery.All, null);
            var header = binaryResponseHeaderParser.Parse(result);
            var examples = binaryQueryResultsParser.Parse(result);
            this.dataGridView1.DataSource = examples.ToList();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Query();

        }


          5.添加索引数据

          

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text)||string.IsNullOrWhiteSpace(textBox2.Text))
            {
                MessageBox.Show("编码和名称不能为空");
                return;
            }

            var docs = new List();
            var doc = new SolrInputDocument();
            doc.Add("ProductCode", new SolrInputField("ProductCode",textBox1.Text));
            doc.Add("ProductName", new SolrInputField("ProductName",textBox2.Text));
            docs.Add(doc);
            var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
            var header = binaryResponseHeaderParser.Parse(result);

            this.DialogResult = DialogResult.OK;
        }


       6.删除索引数据

      

   private void button3_Click(object sender, EventArgs e)
        {
            //删除
            if (this.dataGridView1.SelectedRows.Count != 1)
            {
                return;
            }

            string code = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, DelById = new string[] { code } });
            var header = binaryResponseHeaderParser.Parse(result);

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


              有兴趣的同学可以加QQ群

             Demo下载:http://download.csdn.net/detail/zx13525079024/7296129

 

 

电商论道---(3).net开发solr,搜素材,soscw.com

电商论道---(3).net开发solr

标签:des   datagridview   Lucene   style   blog   class   

原文地址:http://blog.csdn.net/zx13525079024/article/details/25034089


评论


亲,登录后才可以留言!