/// <summary> ///上传货物信息 /// </summary> /// <param name="model"></param> /// <param name="imageAdress"></param> /// <returns></returns> public bool UpLoadGoodInfo(ViewModel.UpLoadGoodModel model, string imageAdress) { try { T007店铺货物表 newGood = new T007店铺货物表(); newGood.GoodIntroduction = model.Introduction; newGood.GoodName = model.GoodName; newGood.GoodPhoto = imageAdress; newGood.GoodPrice = model.GoodPrice; newGood.ShopID = model.shopId; newGood.GoodNumber = model.GoodNumber; //将记录加入数据库 operateContext.BLLSession.IT007店铺货物表BLL.Add(newGood); ///下面是创建索引的代码。 JobInfo jobinfo = new JobInfo { GoodId = newGood.GoodID, JobType = JobType.Add }; IndexManager.Instance.PutJob(jobinfo);//把索引任务放入队列。 return(true); } catch (Exception) { throw; } }
/// <summary> /// 获取搜索到的商品数据 /// </summary> /// <param name="keyWords"></param> /// <param name="pageSize"></param> /// <param name="page"></param> /// <returns></returns> public GoodsListModel getRightGoods(string keyWords, int pageSize, int page) { ///从词库中获得分词 List <string> kw = splitWordsHelper.SplitWords(keyWords) as List <string>; FSDirectory directory = FSDirectory.Open(new DirectoryInfo(@"G:\index"), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); PhraseQuery queryMsg = new PhraseQuery();//查询条件 foreach (string word in kw) { queryMsg.Add(new Term("GoodName", word)); //Contains("body",word) } queryMsg.SetSlop(100); //词的距离超过100就不匹配 PhraseQuery queryTitle = new PhraseQuery(); //查询条件 foreach (string word in kw) { queryTitle.Add(new Term("GoodIntroduction", word)); //Contains("body",word) } queryTitle.SetSlop(100); //词的距离超过100就不匹配 //复合查询,在标题和正文中搜索 //Should是Or,Must是And BooleanQuery boolQuery = new BooleanQuery(); boolQuery.Add(queryMsg, BooleanClause.Occur.SHOULD); boolQuery.Add(queryTitle, BooleanClause.Occur.SHOULD); //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); //foreach (string word in kw.Split(' '))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业” //{ // query.Add(new Term("msg", word));//Contains("body",word) //} //BooleanQuery //where Contains("body","计算机") and Contains("body","专业") TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); //盛放搜索结果的容器 searcher.Search(boolQuery, null, collector); //用query这个查询条件进行搜索,搜索结果放入collector容器中 List <T007店铺货物表> list = new List <T007店铺货物表>(); // collector.GetTotalHits()查询结果的总条数 ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs; for (int i = 0; i < docs.Length; i++) { int docId = docs[i].doc; //文档编号(lucene.net内部分配的,和number无关) Document doc = searcher.Doc(docId); //根据文档编号拿到文档对象 int id = Convert.ToInt32(doc.Get("GoodID")); //取出文档的number字段的值。必须是Field.Store.YES才能取出来 string goodIntroduction = doc.Get("GoodIntroduction"); string goodName = doc.Get("GoodName"); int goodNumber = Convert.ToInt32(doc.Get("GoodNumber")); string goodPhotoUrl = doc.Get("GoodPhoto"); decimal goodPrice = Convert.ToDecimal(doc.Get("GoodPrice")); int shopid = Convert.ToInt32(doc.Get("ShopID")); T007店铺货物表 good = new T007店铺货物表(); good.GoodID = id; good.GoodName = goodName; good.GoodIntroduction = goodIntroduction; good.GoodNumber = goodNumber; good.GoodPhoto = goodPhotoUrl; good.GoodPrice = goodPrice; good.ShopID = shopid; //sr.Id = id; //sr.Msg = HighLight(kw, msg); //sr.Title = title; //sr.Url = url; list.Add(good); } GoodsListModel goodlist = new GoodsListModel() { goodsList = list, pageInfo = new PagingInfo { currentpage = page, itemperpage = pageSize, Totalitems = list.Count() } }; return(goodlist); // return operateContext.BLLSession.IT007店铺货物表BLL.GetListBy(m => m.GoodName==keyWords).Skip((page-1)*pageSize).Take(pageSize).ToList(); }