/// <summary> /// Return a list of ids for matching documents /// </summary> /// <param name="directory">Lucene Index</param> /// <param name="query">query used to return results</param> /// <param name="primaryKey">Name of a field (usually primary key) to be returned as the identifier of the matching document</param> /// <param name="targetFieldName">Name of the Field to search on</param> /// <returns></returns> public static List <string> SearchOnIndex(string query, string primaryKey, string targetFieldName) { var pks = new List <string>();// list of primary key matches using (var reader = IndexReader.Open(directory, true)) using (var searcher = new IndexSearcher(reader)) { // We are only searching on one Field (targetFieldName) // In this case the field contains all the content of the document // In a real world example you would be able to search on any field // possibly taking the field name form the query var parsedQuery = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, targetFieldName, analyzer).Parse(query); //NotifyInformation(parsedQuery.ToString()); int hitsPerPage = 10; var collector = TopScoreDocCollector.Create(hitsPerPage, true); searcher.Search(parsedQuery, collector); var matches = collector.TopDocs().ScoreDocs; for (int i = 0; i < matches.Length; ++i) { int docId = matches[i].Doc; // internal document id var document = searcher.Doc(docId); pks.Add(document.GetField(primaryKey).StringValue); } } return(pks); }
/* * Phương thức tìm kiếm văn bản */ private static void SearchQuery(string keyword) { // Biến toàn cục: Nơi lưu trữ các index using (var analyzer = new StandardAnalyzer(Version.LUCENE_29)) { LStore.Directory indexStore = LStore.FSDirectory.Open(indexDir); // Tạo truy vấn tìm kiếm Query query = new WildcardQuery(new Term("Content", $"*{keyword.ToLower()}*")); // Truyền query vào IndexSearcher Searcher search = new IndexSearcher(IndexReader.Open(indexStore, true)); /*Bắt đầu tìm kiếm. Có rất nhiều cách tìm kiếm @@ * Cách 1: Tìm dựa theo số lần xuất hiện*/ TopScoreDocCollector cllctr = TopScoreDocCollector.Create(10, true); // true: bật sắp xếp theo thứ tự // Lấy các kết quả đạt yêu cầu query var hits = search.Search(query, 100).ScoreDocs; // ScoreDoc[] hits = cllctr.TopDocs().ScoreDocs; // Vòng lặp lấy kết quả Console.WriteLine("Đã tìm thấy: {0} kết quả", hits.Length); foreach (var hit in hits) { var foundDoc = search.Doc(hit.Doc); Console.WriteLine("File found: {0}", foundDoc.Get("Filename")); Console.WriteLine("File found: {0}", foundDoc.Get("Path")); } } }
public AbstractSecondPassGroupingCollector(IEnumerable <ISearchGroup <TGroupValue> > groups, Sort groupSort, Sort withinGroupSort, int maxDocsPerGroup, bool getScores, bool getMaxScores, bool fillSortFields) { //System.out.println("SP init"); if (groups.Count() == 0) { throw new ArgumentException("no groups to collect (groups.size() is 0)"); } this.groupSort = groupSort; this.withinGroupSort = withinGroupSort; this.groups = groups; this.maxDocsPerGroup = maxDocsPerGroup; groupMap = new HashMap <TGroupValue, AbstractSecondPassGroupingCollector.SearchGroupDocs <TGroupValue> >(groups.Count()); foreach (SearchGroup <TGroupValue> group in groups) { //System.out.println(" prep group=" + (group.groupValue == null ? "null" : group.groupValue.utf8ToString())); ITopDocsCollector collector; if (withinGroupSort == null) { // Sort by score collector = TopScoreDocCollector.Create(maxDocsPerGroup, true); } else { // Sort by fields collector = TopFieldCollector.Create(withinGroupSort, maxDocsPerGroup, fillSortFields, getScores, getMaxScores, true); } groupMap[group.GroupValue] = new AbstractSecondPassGroupingCollector.SearchGroupDocs <TGroupValue>(group.GroupValue, collector); } }
/// <summary> /// 检查文档索引是否存在 /// </summary> /// <param name="docId"></param> /// <returns></returns> public bool Exists(string docId) { try { bool isExistIndex = IndexReader.IndexExists(this._fsDir); if (isExistIndex) { this._indexReader = IndexReader.Open(this._fsDir, false); IndexSearcher searcher = new IndexSearcher(this._indexReader); var q = new TermQuery(new Term(DocStruct.ID, docId)); TopScoreDocCollector collector = TopScoreDocCollector.create(10, true); searcher.Search(q, collector); return(collector.TopDocs().totalHits > 0); } else { return(false); } } catch (Exception ex) { LogHelper.WriteError(this.GetType(), ex); return(false); } }
protected AbstractSecondPassGroupingCollector(IEnumerable <ISearchGroup <TGroupValue> > groups, Sort groupSort, Sort withinGroupSort, int maxDocsPerGroup, bool getScores, bool getMaxScores, bool fillSortFields) // LUCENENET: CA1012: Abstract types should not have constructors (marked protected) { //System.out.println("SP init"); if (!groups.Any()) // LUCENENET TODO: Change back to .Count if/when IEnumerable<T> is changed to ICollection<T> or IReadOnlyCollection<T> { throw new ArgumentException("no groups to collect (groups.Count is 0)"); } this.groupSort = groupSort; this.withinGroupSort = withinGroupSort; this.groups = groups; this.maxDocsPerGroup = maxDocsPerGroup; m_groupMap = new JCG.Dictionary <TGroupValue, AbstractSecondPassGroupingCollector.SearchGroupDocs <TGroupValue> >(groups.Count()); foreach (SearchGroup <TGroupValue> group in groups) { //System.out.println(" prep group=" + (group.groupValue is null ? "null" : group.groupValue.utf8ToString())); ITopDocsCollector collector; if (withinGroupSort is null) { // Sort by score collector = TopScoreDocCollector.Create(maxDocsPerGroup, true); } else { // Sort by fields collector = TopFieldCollector.Create(withinGroupSort, maxDocsPerGroup, fillSortFields, getScores, getMaxScores, true); } m_groupMap[group.GroupValue] = new AbstractSecondPassGroupingCollector.SearchGroupDocs <TGroupValue>(group.GroupValue, collector); } }
public void Index_Protected_Content_Not_Indexed() { var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir)) using (indexer.ProcessNonAsync()) using (var searcher = ((LuceneSearcher)indexer.GetSearcher()).GetLuceneSearcher()) { //create the whole thing rebuilder.Populate(indexer); var protectedQuery = new BooleanQuery(); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndex.CategoryFieldName, IndexTypes.Content)), Occur.MUST)); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndex.ItemIdFieldName, ExamineDemoDataContentService.ProtectedNode.ToString())), Occur.MUST)); var collector = TopScoreDocCollector.Create(100, true); searcher.Search(protectedQuery, collector); Assert.AreEqual(0, collector.TotalHits, "Protected node should not be indexed"); } }
public List <Character> Search(string searchTerm) { //IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Name", analyzer); Query query = parser.Parse(searchTerm); Searcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(luceneIndexDirectory, true)); luceneIndexDirectory.Dispose(); TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true); searcher.Search(query, collector); var matches = collector.TopDocs().ScoreDocs; List <Character> results = new List <Character>(); Character sampleCharacter = null; foreach (var item in matches) { var id = item.Doc; var doc = searcher.Doc(id); sampleCharacter.Name = doc.GetField("Name").StringValue; sampleCharacter.Id = int.Parse(doc.GetField("Id").StringValue); results.Add(sampleCharacter); } return(results); }
public void Should_Fire_Attached_Delegate() { var luceneDelegateFired = false; var service = new DefaultIndexerService(Container.Resolve <ICmsConfiguration>(), Container.Resolve <IRepository>(), Container.Resolve <ISecurityService>(), Container.Resolve <IAccessControlService>()); var delegateBefore = LuceneSearchHelper.Search; LuceneSearchHelper.Search = (query, filter, arg3) => { luceneDelegateFired = true; return(TopScoreDocCollector.Create(0, true)); }; var results = service.Search(new SearchRequest("\"Test page HTML content\"")); Assert.IsTrue(luceneDelegateFired); Assert.IsNotNull(results); Assert.IsNotNull(results.Items); Assert.IsEmpty(results.Items); LuceneSearchHelper.Search = delegateBefore; }
IEnumerable <string> Search(string textToFind) { var reader = IndexReader.Open(_ramDirectory, true); var searcher = new IndexSearcher(reader); var analyzer = new StandardAnalyzer(Version.LUCENE_29); var parser = new MultiFieldQueryParser(Version.LUCENE_29, _searchFields, analyzer); var query = parser.Parse(textToFind); var collector = TopScoreDocCollector.create(100, true); searcher.Search(query, collector); var hits = collector.TopDocs().scoreDocs; var foundKeys = new List <string>(); foreach (ScoreDoc scoreDoc in hits) { var document = searcher.Doc(scoreDoc.doc); var key = document.Get(_keyField); if (key != null && !foundKeys.Contains(key)) { foundKeys.Add(key); } } reader.Close(); searcher.Close(); analyzer.Close(); return(foundKeys); }
public static IList <CorpusDocument> GetMoreLikeThis(string indexName, int indexDocumentId, int maxDocs) { // See: http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/search/similar/MoreLikeThis.html var mlt = new MoreLikeThis(Searcher.GetIndexReader()); mlt.SetAnalyzer(new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)); mlt.SetFieldNames(new[] { "Title", "Content" }); mlt.SetMinWordLen(4); // improve relevancy var query = mlt.Like(indexDocumentId); var tsdc = TopScoreDocCollector.create(maxDocs, true); Searcher.Search(query, tsdc); var hits = tsdc.TopDocs().ScoreDocs; var ret = new List <CorpusDocument>(maxDocs); foreach (var hit in hits) { var d = Searcher.Doc(hit.doc); ret.Add(new CorpusDocument { Id = d.Get("Id"), Title = d.Get("Title"), }); } return(ret); }
/// <summary> /// 搜索结果,并按得分顺序从高到底返回 /// </summary> public List <(string, float)> Search(string kw, int numHits = 10) { if (string.IsNullOrEmpty(kw)) { return(null); } kw = Format(kw); var ret = new List <(string, float)>(); try { var parser = new QueryParser(_matchVersion, "msg", new SmartChineseAnalyzer(_matchVersion)); var query = parser.Parse(kw); TopScoreDocCollector collector = TopScoreDocCollector.Create(numHits, true); using IndexReader reader = _indexWriter.GetReader(applyAllDeletes: true); IndexSearcher searcher = new IndexSearcher(reader); searcher.Search(query, null, collector); ScoreDoc[] docs = collector.GetTopDocs(0, collector.TotalHits).ScoreDocs; for (int i = 0; i < docs.Length; i++) { Document hitDoc = searcher.Doc(docs[i].Doc); ret.Add((hitDoc.Get("msg"), docs[i].Score)); } } catch (Lucene.Net.QueryParsers.Classic.ParseException) { //忽略含有特殊字符的字符串 } catch (Exception ex) { Log.Error(ex, "索引失败"); } return(ret); }
//public static void Search(string term, string dbName, string rootFolder) //{ // IList<ResultItem> resultList = SearchInDB(term, dbName, rootFolder); // GVResults.DataSource = resultList; //} public static IList <ResultItem> SearchInDB(string term, string dbName, string rootFolder) { Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "content", analyzer); Query query = parser.Parse(term); Directory dir = FSDirectory.Open(SearchForm.INDEXDB_FOLDER + "\\" + dbName); Searcher searcher = new IndexSearcher(IndexReader.Open(dir, true)); TopScoreDocCollector results = TopScoreDocCollector.Create(1000, true); searcher.Search(query, results); ScoreDoc[] hits = results.TopDocs().ScoreDocs; IList <ResultItem> resultList = new List <ResultItem>(); for (int i = 0; i < hits.Length; i++) { int docId = hits[i].Doc; float score = hits[i].Score; Document doc = searcher.Doc(docId); string path = doc.Get("path"); ResultItem item = new ResultItem(); item.Path = rootFolder + "\\" + path; item.Score = score.ToString(); resultList.Add(item); } return(resultList); }
public IList <DocumentData> Search(string searchString) { IList <DocumentData> documentDataList = new List <DocumentData>(); try { using (var indexDirectory = FSDirectory.Open(_searchContext.IndexPath)) using (var indexSearcher = new IndexSearcher(indexDirectory, true)) using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)) { QueryParser queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "name", analyser); var query = queryParser.Parse(searchString); var collector = TopScoreDocCollector.Create(500, true); indexSearcher.Search(query, collector); var topDocs = collector.TopDocs(); foreach (var item in topDocs.ScoreDocs) { var luDoc = indexSearcher.Doc(item.Doc); documentDataList.Add(new DocumentData { FileName = luDoc.Get("name"), FilePath = luDoc.Get("path") }); } } } catch (Exception ex) { Trace.WriteLine(ex.Message); } return(documentDataList); }
/// <summary> /// Returns file search results. /// </summary> /// <param name="query">Search query.</param> /// <param name="resultsCollector">Results collector containing search results.</param> /// <returns>Search results enumeration, or null if search type is not supported.</returns> private IEnumerable <ISearchHit> FileSearchResults(Query query, TopScoreDocCollector resultsCollector) { foreach (var doc in resultsCollector.TopDocs().ScoreDocs) { yield return(new LuceneSearchHit(query, mSearcher.Doc(doc.Doc), doc.Score, CaseSensitive, SearchType.Files)); } }
//Changes public IEnumerable <DocumentHit> Search(String term) { if (term == null) { return(null); } var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "text", analyzer); var query = parser.Parse(term); var collector = TopScoreDocCollector.Create(MaximumHits, true); //true: Sort By The Number Of Terme Occurence _searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; var listHits = new List <DocumentHit>(); for (int i = 0; i < hits.Length; i++) { Document doc = _searcher.Doc(hits[i].Doc); listHits.Add(new DocumentHit() { Title = doc.Get("title"), Path = doc.Get("path"), Link = doc.Get("link"), Score = hits[i].Score, PageRank = 0 //PageRank To Be Calculated }); } return(listHits); }
/// <summary> /// Queries the indexed data. /// </summary> /// <param name="start">The starting index.</param> /// <param name="count">The number of items to return.</param> /// <param name="text">The text to query for.</param> public static Dictionary <int, string> Query(int start, int count, string text) { text = text.Trim(); var result = new Dictionary <int, string> (); if (string.IsNullOrEmpty(text)) { return(result); } using (var searcher = new IndexSearcher(Directory, true)) { var reader = IndexReader.Open(Directory, true); var collector = TopScoreDocCollector.Create(count, true); var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "text", analyzer); var qText = text.IndexOf(' ') > 0 ? string.Format("(text:\"{0}\")", text) : string.Format("(text:{0}*) OR (text:{0}~0.5)", text); var query = SearchCommon.ParseQuery(qText, parser); searcher.Search(query, collector); var docs = collector.TopDocs(start, count).ScoreDocs; foreach (var scoreDoc in docs) { var doc = reader.Document(scoreDoc.Doc); var id = Convert.ToInt32(doc.Get("Id")); var t = doc.Get("text"); result.Add(id, t); } reader.Dispose(); analyzer.Close(); } return(result); }
private ScoreDoc[] GetDocsUnderTree(string path, bool recurse) { var field = recurse ? "InTree" : "Path"; var queryContext = SnQueryContext.CreateDefault(); var snQuery = SnQuery.Parse($"{field}:'{path.ToLower()}'", null); var lq = Compile(snQuery, queryContext); //LucQuery.Parse(String.Format("{0}:'{1}'", field, path.ToLower())); using (var readerFrame = LuceneSearchManager.GetIndexReaderFrame()) { var idxReader = readerFrame.IndexReader; var searcher = new IndexSearcher(idxReader); var numDocs = idxReader.NumDocs(); try { var collector = TopScoreDocCollector.Create(numDocs, false); searcher.Search(lq.Query, collector); var topDocs = collector.TopDocs(0, numDocs); return(topDocs.ScoreDocs); } finally { searcher.Close(); } } }
public List <Movie> Search(Lucene.Net.Store.Directory directory, string query) { var movies = new List <Movie>(); var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); using (var reader = IndexReader.Open(directory, true)) { using (var searcher = new IndexSearcher(reader)) { var parsedQuery = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Title", analyzer) .Parse(query); Console.WriteLine(parsedQuery.ToString()); int hitsPerPage = 20; var collector = TopScoreDocCollector.Create(hitsPerPage, true); searcher.Search(parsedQuery, collector); var matches = collector.TopDocs().ScoreDocs; for (var i = 0; i < matches.Length; ++i) { int docId = matches[i].Doc; var document = searcher.Doc(docId); var movie = new Movie(); movie.Title = document.GetField("Title").StringValue; movie.Rating = document.GetField("Rating").StringValue; movie.Description = document.GetField("Description").StringValue; movies.Add(movie); } } } return(movies); }
/// <summary> /// 获取 /// </summary> /// <param name="bQuery"></param> private List <CustomerEntity> GetSearchResult(BooleanQuery bQuery, Dictionary <string, string> dicKeywords, int PageSize, int PageIndex, out int totalCount) { List <CustomerEntity> list = new List <CustomerEntity>(); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(IndexDic), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); Sort sort = new Sort(new SortField("CreateDate", SortField.DOC, true)); searcher.Search(bQuery, null, collector); totalCount = collector.GetTotalHits();//返回总条数 TopDocs docs = searcher.Search(bQuery, (Filter)null, PageSize * PageIndex, sort); if (docs != null && docs.totalHits > 0) { for (int i = 0; i < docs.totalHits; i++) { if (i >= (PageIndex - 1) * PageSize && i < PageIndex * PageSize) { Document doc = searcher.Doc(docs.scoreDocs[i].doc); CustomerEntity model = new CustomerEntity() { CustomerId = doc.Get("number").ToString(), FullName = doc.Get("FullName").ToString(), CreateUserName = doc.Get("CreateUserName").ToString(), CreateDate = DateTime.Parse(doc.Get("CreateDate").ToString()), EnCode = doc.Get("EnCode").ToString() }; list.Add(SetHighlighter(dicKeywords, model)); } } } return(list); }
public IEnumerable <Ticket> SearchIndex(ITicketService ticketService, string searchText, out string queryTerm) { string[] fields = new[] { "title", "details", "tags", "comments" }; MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, fields, TdIndexAnalyzer); Query query = parser.Parse(searchText); queryTerm = query.ToString(); TopScoreDocCollector collector = TopScoreDocCollector.create(20, true); TdIndexSearcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().scoreDocs; SortedList <int, int> ticketIDs = new SortedList <int, int>(); var o = 0; foreach (ScoreDoc scoreDoc in hits) { //Get the document that represents the search result. Document document = TdIndexSearcher.Doc(scoreDoc.doc); int ticketID = int.Parse(document.Get("ticketid")); //The same document can be returned multiple times within the search results. if (!ticketIDs.Values.Contains(ticketID)) { ticketIDs.Add(o, ticketID); o++; } } return(ticketService.ListTickets(ticketIDs, false)); }
public List <(string Name, float Score)> GetRom(string romFileName) { var collector = TopScoreDocCollector.Create(10, true); //First, match on RomName. var query1 = new TermQuery(new Term("RomName", romFileName)); _searcher.Search(query1, collector); var hits = collector.TopDocs().ScoreDocs; var docs = hits.Select(h => (Doc: _searcher.Doc(h.Doc), h.Score)).Where(h => h.Score > 1); if (!docs.Any()) { //If not found, match on Name. var parser = new QueryParser(Version.LUCENE_30, "SimplifiedName", _analyzer); var simplifiedName = Regex.Replace(romFileName, NameSimplificationRegex, "", RegexOptions.IgnoreCase); simplifiedName = Regex.Replace(simplifiedName, CamelCaseSplitRegex, " ", RegexOptions.IgnorePatternWhitespace); var query2 = parser.Parse(simplifiedName); _searcher.Search(query2, collector); hits = collector.TopDocs().ScoreDocs; docs = hits.Select(h => (Doc: _searcher.Doc(h.Doc), h.Score)); } return(docs.Select(d => (Name: d.Doc.GetField("Name").StringValue, d.Score)) .OrderByDescending(d => d.Score).ThenBy(d => d.Name).ToList()); }
public void PerformSearch(MultiFacetRequest request) { using (var multiSearcher = new FacetSearcher(Searcher.IndexReader, request.Config)) { if (request.Sort.Any() == true) { var collector = TopFieldCollector.Create( new Sort(request.Sort.ToArray()), request.MaxResults, false, false, false, false); multiSearcher.Search(request.Query, collector); _scoreDocs = collector.TopDocs().ScoreDocs; } else { var collector = TopScoreDocCollector.Create(request.MaxResults, true); multiSearcher.Search(request.Query, collector); _scoreDocs = collector.TopDocs().ScoreDocs; } var results = multiSearcher.SearchWithFacets(request.Query, request.MaxResults, request.Facets); foreach (var facet in results.Facets.GroupBy(x => x.FacetFieldName)) { Facets.Add(facet.Key, CreateFacetResult(facet.ToList())); } TotalItemCount = results.Hits.TotalHits; } }
// ============================= ADDITIONAL METHODS ==================================== // This is a typical search on a Lucene's index file. private static void Search(string searchTerm, Lucene.Net.Search.IndexSearcher searcher, QueryParser parser, Directory indexDirectory, int totalDocuments) { // Supply conditions Query query = parser.Parse(searchTerm); // Will store the results (hits). TopScoreDocCollector collector = TopScoreDocCollector.Create(totalDocuments, true); searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; int counter = 0; // printing out the results foreach (ScoreDoc item in hits) { int docID = item.Doc; Document d = searcher.Doc(docID); // Call DisplayMessage(d); to display the message. DisplayMessage(d, searchTerm); counter++; } if (counter != 0) { Console.WriteLine("Found {0} messages that match your search term.", counter); } else { Console.WriteLine("There were no results matching your search request.\nSorry :("); } Console.WriteLine("=============================="); }
public ICollection <ISearchResult> QueryIndex(QueryData[] queries) { // Construct a new boolean query from the input query data BooleanQuery boolQuery = new BooleanQuery(); foreach (var query in queries) { var parsed = new QueryParser(Version.LUCENE_30, query.m_fieldName, new SnowballAnalyzer(Version.LUCENE_30, "English")).Parse(query.m_fieldValue); boolQuery.Add(parsed, Occur.SHOULD); } // Collect the top N results and format for output to the view TopScoreDocCollector topDocColl = TopScoreDocCollector.Create(50, true); m_searcher.Search(boolQuery, topDocColl); TopDocs topDocs = topDocColl.TopDocs(); List <ISearchResult> results = new List <ISearchResult>(); foreach (var searchHit in topDocs.ScoreDocs) { string topic = m_searcher.Doc(searchHit.Doc).GetField("Topic").StringValue; string id = m_searcher.Doc(searchHit.Doc).GetField("id").StringValue; string description = m_searcher.Doc(searchHit.Doc).GetField("description").StringValue; string title = m_searcher.Doc(searchHit.Doc).GetField("title").StringValue; results.Add(new SearchResult(topic + "/" + id + ".jpg", title, id + ".jpg", description)); } return(results); }
public object Search(string keyWord, int pageIndex, int pageSize) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); string indexPath = Directory.GetCurrentDirectory() + "/Index"; FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); //创建IndexSearcher准备进行搜索。 IndexSearcher searcher = new IndexSearcher(reader); // 查询条件 keyWord = GetKeyWordsSplitBySpace(keyWord, new PanGuTokenizer()); //创建QueryParser查询解析器。用来对查询语句进行语法分析。 //QueryParser调用parser进行语法分析,形成查询语法树,放到Query中。 QueryParser msgQueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Content", new PanGuAnalyzer(true)); Query msgQuery = msgQueryParser.Parse(keyWord); //TopScoreDocCollector:盛放查询结果的容器 //numHits 获取条数 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); //IndexSearcher调用search对查询语法树Query进行搜索,得到结果TopScoreDocCollector。 // 使用query这个查询条件进行搜索,搜索结果放入collector searcher.Search(msgQuery, null, collector); // 从查询结果中取出第n条到第m条的数据 ScoreDoc[] docs = collector.TopDocs(0, 1000).scoreDocs; stopwatch.Stop(); // 遍历查询结果 List <ReturnModel> resultList = new List <ReturnModel>(); var pm = new Page <ReturnModel> { PageIndex = pageIndex, PageSize = pageSize, TotalRows = docs.Length }; pm.TotalPages = pm.TotalRows / pageSize; if (pm.TotalRows % pageSize != 0) { pm.TotalPages++; } for (int i = (pageIndex - 1) * pageSize; i < pageIndex * pageSize && i < docs.Length; i++) { var doc = searcher.Doc(docs[i].doc); var content = HighlightHelper.HighLight(keyWord, doc.Get("Content")); var result = new ReturnModel { Title = doc.Get("Title"), Content = content, Count = Regex.Matches(content, "<font").Count }; resultList.Add(result); } pm.LsList = resultList; var elapsedTime = stopwatch.ElapsedMilliseconds + "ms"; var list = new { list = pm, ms = elapsedTime }; return(list); }
public static List <Book> SearchBooks(string propertyValue) { List <Book> books = new List <Book>(); IndexReader indexReader = IndexReader.Open(directory, true); using (var reader = IndexReader.Open(directory, true)) using (var searcher = new IndexSearcher(reader)) { using (Analyzer myAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)) { var queryParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Author|Title".Split('|'), myAnalyzer); queryParser.AllowLeadingWildcard = true; var query = queryParser.Parse("*" + propertyValue + "*"); var collector = TopScoreDocCollector.Create(2, true); searcher.Search(query, collector); var matches = collector.TopDocs().ScoreDocs; foreach (var hit in matches) { var id = hit.Doc; var documentFromSearch = searcher.Doc(id); books.Add(new Models.Book() { BookId = Convert.ToInt32(documentFromSearch.Get("BookId")), Author = documentFromSearch.Get("Author"), Title = documentFromSearch.Get("Title") }); } } } return(books); }
public async Task <IList <string> > ExecuteQueryAsync(Query query, string indexName, int start, int end) { var contentItemIds = new List <string>(); await _luceneIndexManager.SearchAsync(indexName, searcher => { if (end > 0) { var collector = TopScoreDocCollector.Create(end, true); searcher.Search(query, collector); var hits = collector.GetTopDocs(start, end); foreach (var hit in hits.ScoreDocs) { var d = searcher.Doc(hit.Doc, IdSet); contentItemIds.Add(d.GetField("ContentItemId").GetStringValue()); } } return(Task.CompletedTask); }); return(contentItemIds); }
public Task <IEnumerable <SearchResultItem> > SearchAsync(string searchText) { return(Task.Run(() => { var fields = new[] { "id", "title", "details", "tags", "events" }; var parser = new MultiFieldQueryParser(Version.LUCENE_30, fields, TdIndexAnalyzer); var query = parser.Parse(searchText); var collector = TopScoreDocCollector.Create(20, true); TdIndexSearcher.Search(query, collector); return collector.TopDocs().ScoreDocs.Select(d => { var document = TdIndexSearcher.Doc(d.Doc); return new SearchResultItem { Id = int.Parse(document.Get("id")), SearchScore = d.Score }; }); })); }
private ScoreDoc[] GetDocsUnderTree(string path, bool recurse) { var field = recurse ? "InTree" : "Path"; var lq = LucQuery.Parse(String.Format("{0}:'{1}'", path, path.ToLower())); var idxReader = GetIndexReader(); var searcher = new IndexSearcher(idxReader); var numDocs = idxReader.NumDocs(); try { var collector = TopScoreDocCollector.Create(numDocs, false); searcher.Search(lq.Query, collector); var topDocs = collector.TopDocs(0, numDocs); return(topDocs.ScoreDocs); } finally { if (searcher != null) { searcher.Close(); } searcher = null; } }
static void Query() { try { // Counterpart to the IndexWriter used above. IndexReader reader = IndexReader.Open(FSDirectory.Open(INDEX_DIR), true); Console.Out.WriteLine("Number of indexed docs: " + reader.NumDocs()); IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(INDEX_DIR)); // Construct a search query. String 1 in term is the field name, String 2 the content to match against. // The name of the field "content" here and how we filled a "content" field using the IndexWrite above. // Apparently BooleanQuery can be used to combine search queries. Term searchTerm = new Term("content", "defg"); TermQuery query = new TermQuery(searchTerm); // Used to collect the highest scoring hits when searching. TopScoreDocCollector topDocColl = TopScoreDocCollector.Create(10, true); searcher.Search(query, topDocColl); // Collection of documents matched using the search query. TopDocs topDocs = topDocColl.TopDocs(); Console.WriteLine("Number of hits: " + topDocs.TotalHits); // Traverse through the search hits, print their index in the query and the document's name. foreach (var searchHit in topDocs.ScoreDocs) { Console.WriteLine(searchHit.Doc + ". " + searcher.Doc(searchHit.Doc).GetField("name").StringValue); } } catch (IOException excep) { Console.Out.WriteLine(excep.Message); } }