public void Wildcrad_query_must_transform_correclty_to_ES() { var query = new WildcardQuery("type", "te*t"); Assert.IsTrue(query.Field == "type"); Assert.IsTrue(query.Value.ToString() == "te*t"); string json = query.ToString(); var jo = JsonObject.Parse(json); var q = jo.ToQuery(); Assert.IsTrue(q.Type == QueryType.WildcardQuery); query = q as WildcardQuery; Assert.IsTrue(query.Field == "type"); Assert.IsTrue(query.Value.ToString() == "te*t"); }
/// <summary> /// 资讯查询 /// </summary> /// <param name="keyword">关键字</param> /// <returns>符合查询条件的资讯列表</returns> public static List <Music> Search(string keyword, Sort sort, int currentPage, int pageSize, out int recordCount) { if (!IndexExists()) { IndexReset(); } if (sort == null) { sort = new Sort(new SortField("Number", 0)); } Analyzer analyzer = new PanGuAnalyzer(); BooleanQuery totalQuery = new BooleanQuery(); MultiFieldQueryParser parser = new MultiFieldQueryParser(VERSION, new string[] { "Author", "Name" }, analyzer); if (!String.IsNullOrEmpty(keyword)) { keyword = ClearKeyword(keyword); //Lucene.Net.Search.Query query = parser.Parse(SearchBase.AnalysisKeyword(keyword)); LogHelper.ErrorLoger.Error("keyword:" + keyword); Query query = new WildcardQuery(new Term("Name", "*" + keyword + "*")); //Query query = parser.Parse(keyword); LogHelper.ErrorLoger.Error("keyword:" + query.ToString()); totalQuery.Add(query, Occur.MUST); } if (currentPage < 0) { currentPage = 0; } int beginIndex = currentPage * pageSize; int lastIndex = (currentPage + 1) * pageSize; IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(indexPath), true); TopDocs result = searcher.Search(totalQuery, null, lastIndex, sort); ScoreDoc[] hits = result.ScoreDocs; recordCount = result.TotalHits; if (recordCount < lastIndex) { lastIndex = recordCount; } List <Music> pList = new List <Music>(); Music p = null; for (int i = beginIndex; i < lastIndex; i++) { var doc = searcher.Doc(hits[i].Doc); p = new Music(); p.Author = doc.Get("Author"); p.Name = doc.Get("Name"); p.FileUrl = doc.Get("FileUrl"); p.Cover = doc.Get("Cover"); pList.Add(p); } searcher.Dispose(); return(pList); }