隐藏

Solr 查询、 添加、修改、删除功能

发布:2019/12/30 14:16:46作者:管理员 来源:本站 浏览次数:1307

---------------------查询----------------------------

ResultsPage<List<SolrDTO>> results = new ResultsPage<List<SolrDTO>> { Code = -1, Msg = "异常错误", Success = false, Value = null };

            //定义solr
            ISolrOperations<SolrDTO> solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDTO>>();
            QueryOptions options = new QueryOptions();
            options.Rows = solrParams.PageSize;//数据条数
            //options.Start = solrParams.PageIndex;//开始项
            options.StartOrCursor = new StartOrCursor.Start(solrParams.PageIndex * solrParams.PageSize);
            var high = new HighlightingParameters();
            high.Fields = new List<string> { "askquestion_title" };
            high.BeforeTerm = "<font color='red'><b>";
            high.AfterTerm = "</b></font>";
            options.Highlight = high;

            //创建条件集合
            List<ISolrQuery> query = new List<ISolrQuery>();
            if (!string.IsNullOrEmpty(solrParams.Keyword))
            {
                List<ISolrQuery> ar = new List<ISolrQuery>();
                string[] keywords = solrParams.Keyword.Split(new char[] { ',' });
                foreach (string a in keywords)
                {
                    ar.Add(new SolrQueryByField("askquestion_title", a));
                }
                //创建id 条件集合的关系,是OR还是AND
                var kw = new SolrMultipleCriteriaQuery(ar, "OR");
                //添加至条件集合
                query.Add(kw);
            }
            //条件集合之间的关系
            var qTBO = new SolrMultipleCriteriaQuery(query, "AND");
            List<KeyValuePair<string, string>> ss = new List<KeyValuePair<string, string>>();
            ss.Add(KV.Create("wt", "xml"));
            options.ExtraParams = ss;
            //执行查询
            SolrQueryResults<SolrDTO> result = solr.Query(qTBO, options);

            var highlights = result.Highlights;
            foreach (var item in result)
            {
                var t = highlights[item.id.ToString()].Values.ToList()[0].ToList()[0];
                item.Title = t;
            }
            List<SolrDTO> list = new List<SolrDTO>();
            // 显示查询结果
            foreach (SolrDTO p in result)
            {
                list.Add(p);
                //Console.WriteLine("id:{0}   name:{1}   color:{2}     price:{3}", p.id, p.title, p.seo_title, p.content);
            }
            // 得到返回的数据总条数和total和 总页数 用于分页显示,
            var total = result.NumFound;
            var pageCount = total / solrParams.PageSize + 1;
            results.TotlCount = total;
            results.TotlPageCount = pageCount;
            results.Value = list;
            results.Msg = "获取成功";
            results.Success = true;
            results.Code = 0;
            return results;

------------------查询,修改--------------------------
ISolrOperations<SolrDTO> solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDTO>>();
        QueryOptions options = new QueryOptions();
        options.Rows = 1;//数据条数
                         //options.Start = solrParams.PageIndex;//开始项
        options.StartOrCursor = new StartOrCursor.Start(0);
        var high = new HighlightingParameters();
        high.Fields = new List<string> { "askquestion_title" };
        high.BeforeTerm = "<font color='red'><b>";
        high.AfterTerm = "</b></font>";
        options.Highlight = high;

        //创建条件集合
        List<ISolrQuery> query = new List<ISolrQuery>();
        var aid = context.Request["aid"];
        if (!string.IsNullOrEmpty(aid))
        {
            List<ISolrQuery> ar = new List<ISolrQuery>();
            ar.Add(new SolrQueryByField("askquestion_aid", aid));
            //string[] keywords = solrParams.Keyword.Split(new char[] { ',' });
            //foreach (string a in keywords)
            //{
            //    ar.Add(new SolrQueryByField("askquestion_title", a));
            //}
            //创建id 条件集合的关系,是OR还是AND
            var kw = new SolrMultipleCriteriaQuery(ar, "OR");
            //添加至条件集合
            query.Add(kw);
        }
        //条件集合之间的关系
        var qTBO = new SolrMultipleCriteriaQuery(query, "AND");
        List<KeyValuePair<string, string>> ss = new List<KeyValuePair<string, string>>();
        ss.Add(KV.Create("wt", "xml"));
        options.ExtraParams = ss;
        //执行查询
        SolrQueryResults<SolrDTO> result = solr.Query(qTBO, options);
        if (result.Count > 0)
        {
            var row = result[0];
            row.FavorCount = row.FavorCount + 1;
            solr.Add(row);
            solr.Commit();

        }



-----------------添加-------------------------------

var title = txt_Title.Value;
        var AskContentb = txtnewstextSub.Value;
        var _ImgUrl = Maticsoft.Common.Tool.GetImgUrl(AskContentb);
        var json = string.Empty;
        var Keyword = string.Empty;
        int usertype = 0;
        var userid = GetCurrentUserId(out usertype);
        using (var client = new HttpClient())
        {
            try
            {
                var url = string.Format("http://webapi.zybw.com/api/GetAnalysis?keyword={0}&count=2&type=index",
                    HttpUtility.UrlEncode(title, Encoding.UTF8));
                client.DefaultRequestHeaders.Add("Token", "iojkljfds6fdsa546fdsaf8556fdsa656a64");
                var responseString = client.GetStringAsync(url);
                json = responseString.Result;
                JObject jo = JObject.Parse(json);
                JArray jsonColleagues = (JArray)jo["Value"];
                Keyword = jsonColleagues.ToString();
            }
            catch { }
        }
        EHS_AskQuestion model = new EHS_AskQuestion
        {
            Title = title,
            AskContent = AskContentb,
            CoverImage = _ImgUrl,
            AskState = 1,
            ClickCount = 0,
            CreateTime = DateTime.Now,
            Display = 1,
            IsHot = 0,
            IsRed = 0,
            IsShow = 1,
            IsTop = 0,
            Keyword = Keyword,
            Priority = 1,
            Solutions = 0,
            UserID = userid,
            VideoUrl = "",
            UserType = usertype
        };
        long aid = _bll_EHS_AskQuestion.Add(model);
        if (aid > 0)
        {
            
            var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDTO>>();
            solr.Add(new SolrDTO()
            {
                id = int.Parse(aid.ToString()),
                Aid = int.Parse(aid.ToString()),
                Title = title,
                AskContent = AskContentb,
                CoverImage = _ImgUrl,
                AskState = 1,
                ClickCount = 0,
                CreateTime = DateTime.Now,
                Display = 1,
                IsHot = 0,
                IsRed = 0,
                IsShow = 1,
                IsTop = 0,
                Keyword = Keyword,
                Priority = 1,
                Solutions = 0,
                UserID = userid,
                VideoUrl = "",
                UserType = usertype
            });
            
            solr.Commit();
            Response.Redirect("index.html");
        }