/// <summary> /// 为keyword做盘古分词 /// </summary> /// <param name="keyword"></param> /// <param name="luceneQuery"></param> /// <returns></returns> private static string AnalyzerKeyword(string keyword) { StringBuilder queryStringBuilder = new StringBuilder(); ILuceneAnalyze analyzer = new LuceneAnalyze(); string[] words = analyzer.AnalyzerKey(keyword); if (words.Length == 1) { queryStringBuilder.AppendFormat("{0}:{1}* ", "title", words[0]); } else { StringBuilder fieldQueryStringBuilder = new StringBuilder(); foreach (string word in words) { queryStringBuilder.AppendFormat("{0}:{1} ", "title", word); } } string result = queryStringBuilder.ToString().TrimEnd(); logger.Info(string.Format("AnalyzerKeyword 将 keyword={0}转换为{1}", keyword, result)); return(result); }
private static string AnalyzerKeyword(string keyword) { StringBuilder queryStringBuilder = new StringBuilder(); ILuceneAnalyze analyzer = new LuceneAnalyze(); List <string> words = analyzer.AnalyzerKey(keyword, "title"); if (words.Count == 1) { queryStringBuilder.AppendFormat("{0}:{1}* ", "title", words[0]); } else { StringBuilder fieldQueryStringBuilder = new StringBuilder(); foreach (string word in words) { queryStringBuilder.AppendFormat("{0}:{1} ", "title", word); } } string result = queryStringBuilder.ToString().TrimEnd(); Console.WriteLine("AnalyzerKeyword 将 keyword={0}转换为{1}", keyword, result); return(result); }
/// <summary> /// 为keyword做盘古分词 /// </summary> /// <param name="keyword"></param> /// <param name="fieldName"></param> /// <returns></returns> private static string AnalyzerKeyword(string keyword, string fieldName) { StringBuilder queryStringBuilder = new StringBuilder(); ILuceneAnalyze analyzer = new LuceneAnalyze(); List <string> words = analyzer.AnalyzerKey(keyword, fieldName); if (words.Count == 1) { queryStringBuilder.AppendFormat("{0}:{1}* ", fieldName, words[0]); } else { StringBuilder fieldQueryStringBuilder = new StringBuilder(); foreach (string word in words) { queryStringBuilder.AppendFormat("{0}:{1} ", fieldName, word); } } string result = queryStringBuilder.ToString().TrimEnd(); m_logger.Info(string.Format("AnalyzerKeyword 将 keyword={0}转换为{1}", keyword, result)); return(result); }
public static List <string> PanGuSplitWord(string fieldName, string keyword) { ILuceneAnalyze luceneAnalyze = new LuceneAnalyze(); return(luceneAnalyze.AnalyzerKey(fieldName, keyword)); }
/// <summary> ///根据关键字进行查询,根据int的范围进行搜索 /// </summary> /// <param name="Title"></param> /// <returns></returns> public List <Bpo_JobEntity> SearchJobList4(string Title, string FullAddress, int MinId, int MaxId) { List <Bpo_JobEntity> jobList = new List <Bpo_JobEntity>(); string path = "F://LuceneIndexDir"; FSDirectory dir = FSDirectory.Open(path); //IndexSearcher searcher = new IndexSearcher(dir);//声明一个查询器,或者以下面一种方式声明 IndexReader reader = IndexReader.Open(dir, true);//查询器 IndexSearcher searcher = new IndexSearcher(reader); //搜索条件,BooleanQuery可以同时制定多个搜索条件 BooleanQuery booleanQuery = new BooleanQuery(); //Query query = new TermQuery(new Term("Title", $"*{Title}*"));//不支持通配符 //Query query = new WildcardQuery(new Term("Title", $"*{Title}*")); // 通配符 if (!string.IsNullOrEmpty(Title))//空格隔开,按照多个词进行搜索 { Title = new LuceneAnalyze().AnalyzerKeyword("Title", Title); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", new PanGuAnalyzer()); Query query = parser.Parse(Title); booleanQuery.Add(query, Occur.MUST); } if (MinId > 0)//空格隔开,按照多个词进行搜索 { //string idStr = new LuceneAnalyze().AnalyzerKeyword("Id", MinId.ToString()); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PanGuAnalyzer()); Query query = parser.Parse(MinId.ToString()); booleanQuery.Add(query, Occur.MUST); } if (!string.IsNullOrEmpty(FullAddress)) { FullAddress = new LuceneAnalyze().AnalyzerKeyword("FullAddress", FullAddress); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "FullAddress", new PanGuAnalyzer()); Query query = parser.Parse(FullAddress); booleanQuery.Add(query, Occur.MUST); //使用WildcardQuery,相当于sql的like //Query query2 = new WildcardQuery(new Term("FullAddress", $"*{FullAddress}*")); // 通配符 //booleanQuery.Add(query2, Occur.MUST); } //根据id区间区间搜索(此时id存储索引时要是NumericField类型) NumericRangeFilter <int> intFilter = null; //if (MinId > 0 && MaxId == 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", MinId, int.MaxValue, true, true); //} //else if (MaxId > 0 && MinId == 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", 0, MaxId, true, true); //} //else if (MaxId > 0 && MinId > 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", MinId, MaxId, true, true); //} //定义排序 SortField sortField = new SortField("Id", SortField.STRING, false); //降序 SortField sortField2 = new SortField("CompanyId", SortField.INT, false); //降序 Sort sort = new Sort(sortField, sortField2); //取搜索结果方法1: TopDocs docs = searcher.Search(booleanQuery, intFilter, 10000, sort); //找到的结果取100条数据 foreach (ScoreDoc sd in docs.ScoreDocs) //从docs.ScoreDocs取数据 { Document doc = searcher.Doc(sd.Doc); jobList.Add(new Bpo_JobEntity { Id = Convert.ToInt32(doc.Get("Id") ?? "-100"), Title = doc.Get("Title"), UserId = Convert.ToInt32(doc.Get("UserId") ?? "-100"), UserName = doc.Get("UserName"), CompanyId = Convert.ToInt32(doc.Get("CompanyId") ?? "-100"), CompanyName = doc.Get("CompanyName"), FullAddress = doc.Get("FullAddress"), CreateDate = Convert.ToDateTime(doc.Get("CreateDate")) }); } return(jobList); }