Example #1
0
        public void AddTest()
        {
            var p = new Product
            {
                Id          = 2300,
                Name        = "贷款的法拉利萨顶顶",
                Num         = "sh0923",
                Desc        = "<p>dfdd</p>",
                PriceMarket = 458,
                PriceShop   = 320,
                CreateOn    = DateTime.Now
            };
            var result = SolrContext.Update(p); //_solr.Add(p);

            Console.Write(result.ToJson());
        }
Example #2
0
 public SearchController(SolrContext solrContext)
 {
     _solrContext = solrContext;
 }
Example #3
0
        public IHttpActionResult Get([FromUri] Request <Info> request)
        {
            if (request.Type == -1)
            {
                MongoCollection          data           = database.GetCollection("info");
                GroupByBuilder           groupbyBuilder = new GroupByBuilder(new string[] { "CategoryCode", "CategoryName" });
                Dictionary <string, int> dic_F          = new Dictionary <string, int>();
                dic_F["num"] = 0;
                var result_F = data.Group(null, groupbyBuilder, BsonDocument.Create(dic_F), BsonJavaScript.Create("function(doc,prev){prev.num++;}"), BsonJavaScript.Create("function(doc){doc.Count=doc.num;delete doc.num;}")).ToList();

                /*List<InfoAllList> list = new List<InfoAllList>();
                 * if (result_F.Count > 0)
                 * {
                 *  foreach (var item in result_F)
                 *  {
                 *      InfoAllList temp = new InfoAllList();
                 *      temp.CategoryCode = item["CategoryCode"].AsString;
                 *      temp.CategoryName = item["CategoryName"].AsString;
                 *      temp.Count = item["Count"].AsDouble.ToString().ToInt(0);
                 *      list.Add(temp);
                 *  }
                 * }*/
                Response <List <BsonDocument> > rsp = new Response <List <BsonDocument> >();
                rsp.Data = result_F;
                return(Ok(rsp));
            }
            if (request.ID == "SearchInfo")
            {
                Response <List <InfoModel> > rsp = new Response <List <InfoModel> >();
                if (string.IsNullOrEmpty(request.Keyword))
                {
                    return(Ok(rsp));
                }

                List <InfoModel> list = new List <InfoModel>();

                #region 1、在mongodb查询匹配的Info对象
                IMongoQuery query = null;
                if (request.UserName == "Title")
                {
                    query = Query <Info> .Matches(c => c.Title, new BsonRegularExpression(new Regex(request.Keyword + ".*")));
                }
                else if (request.UserName == "Author")
                {
                    query = Query <Info> .Matches(c => c.Author, new BsonRegularExpression(new Regex(request.Keyword + ".*")));
                }
                else if (request.UserName == "Source")
                {
                    query = Query <Info> .Matches(c => c.Source, new BsonRegularExpression(new Regex(request.Keyword + ".*")));
                }
                else if (request.UserName == "Digest")
                {
                    query = Query <Info> .Matches(c => c.Digest, new BsonRegularExpression(new Regex(request.Keyword + ".*")));
                }
                else if (request.UserName == "KeyWord")
                {
                    query = Query <Info> .Matches(c => c.KeyWord, new BsonRegularExpression(new Regex(request.Keyword + ".*")));
                }
                else if (request.UserName == "CategoryCode")
                {
                    query = Query <Info> .Matches(c => c.CategoryCode, new BsonRegularExpression(new Regex(request.Keyword)));
                }

                if (query != null)
                {
                    var data = database.GetCollection("info").Find(query).ToList();
                    foreach (var item in data)
                    {
                        InfoModel temp = new InfoModel();
                        temp.InfoId = item.GetValue("_id").ToString();
                        temp.Title  = item.GetValue("Title").ToString();
                        Dictionary dic = getArticleClassNameFoo(item.GetValue("CategoryCode").ToString());
                        if (dic != null)
                        {
                            temp.ArticleClass = dic.value;
                        }
                        temp.ArticleCategory = item.GetValue("CategoryName").ToString();
                        temp.Author          = item.GetValue("Author").ToString();
                        string   extStr = item.GetValue("ArticleType").ToString();
                        string[] arr    = extStr.Split('、');
                        if (arr.Length > 0)
                        {
                            temp.FileType = arr[0];
                        }
                        else
                        {
                            temp.FileType = "1176708";
                        }

                        string fileUrl    = "";
                        string dbFileName = item.GetValue("FileName").AsString.Trim();
                        if (!string.IsNullOrEmpty(dbFileName))
                        {
                            fileUrl = "/Upload/" + dbFileName;
                        }

                        /*if (item.GetValue("FileName").ToString().Trim().EndsWith(".xls") || item.GetValue("FileName").ToString().Trim().EndsWith(".xlsx"))
                         * {
                         *  fileUrl += "XLS/";
                         * }
                         * fileUrl += item.GetValue("FileId").ToString().Trim() + "/" + item.GetValue("FileName").ToString().Trim();*/
                        temp.FilePath    = fileUrl;
                        temp.PublishTime = item.GetValue("PublishTime").AsDateTime;
                        temp.ArticleType = item.GetValue("ArticleType").AsString;
                        temp.ModifyTime  = item.GetValue("ModifyTime").AsDateTime;
                        temp.ModifyUser  = item.GetValue("ModifyUser").AsString;

                        list.Add(temp);
                    }
                }
                #endregion

                if (request.UserName == "Content")
                {
                    #region 2、查询solr服务器来搜索文件内容
                    string      q      = "text:" + request.Keyword;
                    string      url    = SolrUrl + "select?q=" + HttpUtility.UrlEncode(q) + "&rows=1000000&wt=json&_=" + StringExtension.getTimeStamp(DateTime.Now);
                    HttpClient  client = new HttpClient();
                    string      result = client.GetStringAsync(url).Result;
                    SolrContext sc     = result.JsonDeserialize <SolrContext>();
                    #endregion

                    #region 3、通过solr搜索结果来匹配数据库
                    foreach (var item in sc.response.docs)
                    {
                        FileInfo fi      = new FileInfo(item.uri);
                        string   dirName = fi.Directory.Name;
                        var      query2  = Query.EQ("FileId", dirName);
                        Info     info    = database.GetCollection("info").FindAs <Info>(query2).FirstOrDefault();
                        if (info == null)
                        {
                            continue;
                        }
                        if (info.FileName != fi.Name)
                        {
                            continue;
                        }

                        InfoModel temp = new InfoModel();
                        temp.InfoId = info._id.ToString();
                        temp.Title  = info.Title;
                        Dictionary dic = getArticleClassNameFoo(info.CategoryCode);
                        if (dic != null)
                        {
                            temp.ArticleClass = dic.value;
                        }
                        temp.ArticleCategory = info.CategoryName;
                        temp.Author          = info.Author;

                        string   extStr = info.ArticleType;
                        string[] arr    = extStr.Split('、');
                        if (arr.Length > 0)
                        {
                            temp.FileType = arr[0];
                        }
                        else
                        {
                            temp.FileType = "1176708";
                        }

                        string fileUrl = "/Upload/";
                        if (fi.Extension == ".xls" || fi.Extension == ".xlsx")
                        {
                            fileUrl += "XLS/";
                        }
                        fileUrl      += info.FileId + "/" + info.FileName;
                        temp.FilePath = fileUrl;

                        if (list.Contains(temp))
                        {
                            continue;
                        }
                        list.Add(temp);
                    }
                    #endregion
                }

                rsp.Data = list;
                if (request.UserName == "CategoryCode")
                {
                    List <InfoModel> result = list.Skip((request.CurrentPage - 1) * PageSize).Take(PageSize).ToList();
                    int count = list.Count / PageSize;
                    if (list.Count % PageSize > 0)
                    {
                        count += 1;
                    }
                    rsp.PagesCount = count;
                    rsp.Data       = result;
                }

                return(Ok(rsp));
            }
            else
            {
                Guid g = new Guid();
                if (Guid.TryParse(request.Keyword, out g))
                {
                    Dictionary    dic     = _dictionary.GetDictionaryById(g.ToString());
                    List <string> nodeIds = new List <string>();
                    nodeIds.Add(dic.nodeId);
                    Foo(nodeIds, dic);

                    Response <List <Info> > response = new Response <List <Info> >();
                    var         list   = database.GetCollection <Info>("info").FindAll().ToList();
                    List <Info> result = new List <Info>();
                    foreach (string item in nodeIds)
                    {
                        Info temp = list.Find(p => p.CategoryCode == item);
                        if (temp != null)
                        {
                            result.Add(temp);
                        }
                    }
                    response.Data = result;
                    return(Ok(response));
                }
                else
                {
                    Response <List <Info> > response = new Response <List <Info> >();
                    var list = database.GetCollection <Info>("info").FindAll().ToList();
                    if (!string.IsNullOrEmpty(request.Keyword))
                    {
                        list = list.FindAll(p => p.Title.Contains(request.Keyword) || p.Html.Contains(request.Keyword) || p.Author.Contains(request.Keyword));
                    }
                    response.Data = list;
                    return(Ok(response));
                }
            }
        }
Example #4
0
 public SolrTest()
 {
     _solr = ServiceLocator.Current.GetInstance <ISolrOperations <Product> >();
     SolrContext.SetConfig("http://192.168.157.130:8080/solr", "test", "json");
 }