.net+solr的使用从删库到跑路

引用SolrNet.dll,Microsoft.Practices.ServiceLocation.dll,HttpWebAdapters.dll
新建class

		[SolrUniqueKey("id")]
        public string id { get; set; }

        [SolrField("a100")]
        public string a100 { get; set; }

        [SolrField("a001")]
        public string a001 { get; set; }

        [SolrField("a205")]
        public string a205 { get; set; }

简单调用

Startup.Init("http://localhost:8080/solr/new_core");
ISolrOperations solr = ServiceLocator.Current.GetInstance>();

                SolrQueryResults phoneTaggedArticles = solr.Query(new SolrQuery("id:0060607D78BB4D97AB90C90EE037A276"));

                foreach (Class1 p in phoneTaggedArticles)
                {
                    rt += string.Format("{0}: {1}", p.id, p.a001);
                }

模糊查询,多条件查询

public string search() {

            string rt = "";
            string area = "是,否";
            string time = "0";//七天内 0是所有
            //定义solr
            ISolrOperations<zwfcmd> solr = ServiceLocator.Current.GetInstance<ISolrOperations<zwfcmd>>();

            //建立排序,条件.
            QueryOptions options = new QueryOptions();
            options.Rows = 10;//数据条数
            options.Start = 0;//开始项


            //创建条件集合
            List<ISolrQuery> query = new List<ISolrQuery>();
            var qTB = new SolrQuery("a298:*牢度:旋转*");
            query.Add(qTB);

            //多条件查询
            if (area != "")
            {
                List<ISolrQuery> ar = new List<ISolrQuery>();
                string[] ares = area.Split(new char[] { ',' });
                foreach (string a in ares)
                {
                    //存储
                    ar.Add(new SolrQueryByField("a846", a));
                }
                //创建查询条件之间的关系,是OR还是AND
                var qArea = new SolrMultipleCriteriaQuery(ar, "OR");
                //添加至条件集合
                query.Add(qArea);
            }

            //创建时间范围实例
            SolrQueryByRange<DateTime> qDateRange = null;
            //七天内数据
            if (time == "-7")
            {
                //后两个参数,一个是开始时间,一个是结束时时间
                qDateRange = new SolrQueryByRange<DateTime>("a205", Convert.ToDateTime(DateTime.Now.AddDays(int.Parse(time)).ToString("yyyy-MM-dd")), Convert.ToDateTime(DateTime.MaxValue.ToString("yyyy-MM-dd")));
            }
            else if (time == "0")//所有数据
            {
                qDateRange = new SolrQueryByRange<DateTime>("a205", Convert.ToDateTime(DateTime.MinValue.ToString("yyyy-MM-dd")), Convert.ToDateTime(DateTime.MaxValue.ToString("yyyy-MM-dd")));
            }
            else//其他
            {
                qDateRange = new SolrQueryByRange<DateTime>("a205", Convert.ToDateTime(DateTime.Today.AddMonths(int.Parse(time)).ToString("yyyy-MM-dd")), Convert.ToDateTime(DateTime.MaxValue.ToString("yyyy-MM-dd")));
            }

            //加入集合
            query.Add(qDateRange);


            //按照时间倒排序.
            options.AddOrder(new SolrNet.SortOrder("a205", Order.DESC));


            //条件集合之间的关系
            var qTBO = new SolrMultipleCriteriaQuery(query, "AND");

            //执行查询,有5个重载
            SolrQueryResults<zwfcmd> results = solr.Query(qTBO, options);

            //this.total = results.NumFound;
            //maxNum = total / pageNum + 1;

            Response.Write(t.Count);

            //Result.Columns.Add("id", typeof(string));
            //Result.Columns.Add("title", typeof(string));
            //Result.Columns.Add("columns", typeof(string));
            //Result.Columns.Add("orderColumn", typeof(string));
            //Result.Columns.Add("area", typeof(string));
            //Result.Columns.Add("industry", typeof(string));
            Result.Columns.Add("body", typeof(string));
            //Result.Columns.Add("addDate", typeof(string));
            //Result.Columns.Add("url", typeof(string));

            //foreach (Article article in results)
            //{
            //    string id = article.id.ToString();
            //    string title = article.title;
            //    string orderColumn = article.orderColumn;
            //    string url = "";
            //    DataRow row = this.Result.NewRow();
            //    row["id"] = id;
            //    row["title"] = title;
            //    row["orderColumn"] = article.orderColumn;
            //    row["area"] = article.area;
            //    row["industry"] = article.industry;
            //    //row["body"] = OptimizeHighlight(body, keyword);
            //    row["addDate"] = article.adddate.ToShortDateString();
            //    row["url"] = url;

            //    this.Result.Rows.Add(row);
            //}
            return rt;
        }

你可能感兴趣的:(solr)