private PagedCollection<IndexProduct> GetProducts(int pageindex, int pagesize, ImageSize imgSize, PrdType prdType) { var pagedProducts = _adminService.GetProductsByType(pageindex, pagesize, prdType); List<IndexProduct> indexProducts = new List<IndexProduct>(); foreach (var item in pagedProducts.Items) { var imgs = _imgService.GetImages(item._id); var selectImg = imgs.FirstOrDefault(m => m.Width == imgSize.Width && m.Height == imgSize.Height); if (selectImg == null) continue; IndexProduct idxProduct = new IndexProduct(); idxProduct.Product = item; idxProduct.Image = selectImg; indexProducts.Add(idxProduct); } return new PagedCollection<IndexProduct>(indexProducts, pagedProducts.PageIndex, pagedProducts.PageSize, pagedProducts.ItemCount); }
public IList<IndexProduct> SearchProducts(string path, string term) { FSDirectory directory = FSDirectory.Open(new DirectoryInfo(path), new NoLockFactory()); using (directory) { IndexReader reader = IndexReader.Open(directory, true); using (reader) { IndexSearcher searcher = new IndexSearcher(reader); PhraseQuery query = new PhraseQuery(); var termSplits = SplitWords(term); foreach (var t in termSplits) { query.Add(new Term(_name, t)); } query.Slop = 0; TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true); searcher.Search(query, collector); ScoreDoc[] docs = collector.TopDocs(0, collector.TotalHits).ScoreDocs; IList<IndexProduct> results = new List<IndexProduct>(); for (int i = 0; i < docs.Length; i++) { int docId = docs[i].Doc; Document doc = searcher.Doc(docId); IndexProduct idxPrd = new IndexProduct(); idxPrd.Product = new ProductRequest(); idxPrd.Product._id = doc.Get(_id) ?? ""; idxPrd.Product.Type = doc.Get(_type).ToInt(); idxPrd.Product.LinkUrl = doc.Get(_linkurl) ?? ""; idxPrd.Product.Name = HighLight(term, doc.Get(_name) ?? ""); idxPrd.Image = new ImageRequest(); idxPrd.Image.Url = doc.Get(_imgurl) ?? ""; results.Add(idxPrd); } return results; } } }