public ActionResult Search(SearchDocument doc) { #region "old codes" doc.Tags = doc.Tags ?? String.Empty; if (doc.Tags.Contains("年份")) { doc.OrderByField = SearchDocument.Field.Year; } if (doc.Tags.Contains("使用频率")) { doc.OrderByField = SearchDocument.Field.Year; } if (doc.Tags.Contains("销量")) { doc.OrderByField = SearchDocument.Field.Year; } doc.Tags = System.Text.RegularExpressions.Regex.Replace(doc.Tags, "(年份|使用频率|销量)", ""); int totalCount = 0; List<Clothes> clothes = SaveClothesHelper.Search(doc, out totalCount); //return View(clothes); #endregion //IEnumerable<Clothes> clothes = DbContext.Clothes.Where(t => t.IsDeleted == false); //if (!string.IsNullOrEmpty(doc.NO)) clothes = clothes.Where(t => t.ProductNO.Contains(doc.NO) || t.SampleNO.Contains(doc.NO)); //var totalCount = clothes.Count(); //clothes = clothes.OrderByDescending(t => t.Id).Skip((doc.PageIndex - 1) * PAGE_SIZE).Take(PAGE_SIZE); return View(clothes.ToPageList<Clothes>(doc.PageIndex, SearchDocument.PAGE_SIZE, totalCount)); }
public static List<Clothes> Search(SearchDocument searchCondition, out int totalCount) { List<Clothes> clothes = new List<Clothes>(); if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(INDEX_PATH)) || System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath(INDEX_PATH)).Length == 0) { totalCount = 0; return clothes; } QueryParser parser = new QueryParser(version, Fields.Tags.ToString(), analyzer); parser.DefaultOperator = QueryParser.Operator.OR; var searchQuery = searchCondition.ToSearchDocument(); Query query = parser.Parse(searchQuery); IndexSearcher searcher = new IndexSearcher(directory); var sortBy = new Sort(new SortField(Fields.Id.ToString(), SortField.INT, true)); switch (searchCondition.OrderByField) { case SearchDocument.Field.SaledCount: sortBy = new Sort(new SortField(Fields.SaledCount.ToString(), SortField.INT, true)); break; case SearchDocument.Field.ViewedCount: sortBy = new Sort(new SortField(Fields.UsedCount.ToString(), SortField.INT, true)); break; case SearchDocument.Field.Year: sortBy = new Sort(new SortField(Fields.Year.ToString(), SortField.STRING, true)); break; } var collector = TopFieldCollector.Create(sortBy, 100000, true, true, true, true); searcher.Search(query, collector); totalCount = collector.TotalHits; var docs = collector.TopDocs((searchCondition.PageIndex - 1) * SearchDocument.PAGE_SIZE, SearchDocument.PAGE_SIZE); foreach (var t in docs.ScoreDocs) { if (t != null) { clothes.Add(new Clothes() { Id = int.Parse(searcher.Doc(t.Doc).GetField(Fields.Id.ToString()).StringValue), ProductNO = searcher.Doc(t.Doc).GetField(Fields.ProductNO.ToString()).StringValue, SampleNO = searcher.Doc(t.Doc).GetField(Fields.SampleNO.ToString()).StringValue, ClothesPics = searcher.Doc(t.Doc).GetField(Fields.ClothesPics.ToString()).StringValue, StylePics = searcher.Doc(t.Doc).GetField(Fields.StylePics.ToString()).StringValue, ModelVersionPics = searcher.Doc(t.Doc).GetField(Fields.ModelVersionPics.ToString()).StringValue }); } } return clothes; }