private void button2_Click(object sender, EventArgs e) { //FSDirectory dir = FSDirectory.Open(PublicConst.LucenePath); //IndexSearcher searcher = new IndexSearcher(dir);//查找器 //{ // TermQuery query = new TermQuery(new Term("Name", "配件"));//包含 // TopDocs docs = searcher.Search(query, null, 1000);//找到的数据 // foreach (ScoreDoc sd in docs.ScoreDocs) // { // Document doc = searcher.Doc(sd.Doc); // Console.WriteLine("***************************************"); // Console.WriteLine(string.Format("Id={0}", doc.Get("Id"))); // Console.WriteLine(string.Format("Code={0}", doc.Get("Code"))); // Console.WriteLine(string.Format("PCode={0}", doc.Get("PCode"))); // Console.WriteLine(string.Format("Name={0}", doc.Get("Name"))); // Console.WriteLine(string.Format("Levels={0}", doc.Get("Levels"))); // } // Console.WriteLine("一共命中了{0}个", docs.TotalHits); //} ILuceneQuery query = new LuceneQuery(); var list = query.QueryIndex("手机"); foreach (var item in list) { Console.WriteLine($"Title{item.Title} Price{item.Price} ProductId{item.ProductId} CateId{item.CateId} "); } }
/// <summary> /// 用lucene进行商品查询 /// </summary> /// <param name="keyword"></param> /// <param name="categoryIdList"></param> /// <returns></returns> public static List <Commodity> QueryCommodity(string keyword) { try { if (string.IsNullOrWhiteSpace(keyword)) { return(null); } ILuceneQuery luceneQuery = new LuceneQuery(); string queryString = string.IsNullOrWhiteSpace(keyword) ? "" : string.Format(" +{0}", AnalyzerKeyword(keyword)); return(luceneQuery.QueryIndex(queryString)); } catch (Exception ex) { logger.Error(string.Format("QueryCommodity参数为{0}出现异常", keyword), ex); return(null); } }
static void Main(string[] args) { do { Console.WriteLine("======================================================"); Console.WriteLine("分词列名:"); string fieldName = Console.ReadLine(); Console.WriteLine("查询的词:"); string keyword = Console.ReadLine(); Console.WriteLine(); CommodityRepository repository = new CommodityRepository(); //IList<EntryDataModel<Commodity>> entryDataModelList = repository.GetEntryDataModelList(); //IndexBuilderThread.BuildIndexThread<Commodity>(entryDataModelList); ILuceneQuery <Commodity> luceneQuery = new LuceneQuery <Commodity>(); List <Commodity> modelList = luceneQuery.QueryIndex(keyword, fieldName, repository.GetFieldModelList()); ShowDataList(modelList); } while (true); }