public ActionResult Search(string query) { ViewData["Message"] = "query : " + query; var searcher = new IndexSearcher( new Lucene.Net.Store.SimpleFSDirectory(new DirectoryInfo(Configuration.IndexDirectory)), readOnly: true); var fieldsToSearchIn = new[] {Configuration.Fields.Name, Configuration.Fields.Description}; var queryanalizer = new MultiFieldQueryParser(Version.LUCENE_CURRENT, fieldsToSearchIn, new BrazilianAnalyzer()); var numberOfResults = 10; var top10Results = searcher.Search(queryanalizer.Parse(query), numberOfResults); var docs = new List<DocumentViewModel>(); foreach (var scoreDoc in top10Results.scoreDocs) { var document = searcher.Doc(scoreDoc.doc); var name = document.GetField(Configuration.Fields.Name).StringValue(); var description = document.GetField(Configuration.Fields.Description).StringValue(); var link = document.GetField(Configuration.Fields.Link).StringValue(); docs.Add(new DocumentViewModel(name, description, link)); } return View(new SearchViewModel(docs)); }
//function to get list of 10 documents at each result page public List <string[]> GetResultCollection(int index) { List <string[]> result = new List <string[]>(); int start = 10 * (index - 1); if (ScoreDocs != null) { for (int i = start; i < (start + 10); i++) { if (i >= ScoreDocs.Length) { break; } Document doc = searcher.Doc(ScoreDocs[i].Doc); string[] current = { "" + (i + 1), doc.Get(TITLE_FN).ToString(), doc.Get(AUTHOR_FN).ToString(), doc.Get(BILI_FN).ToString(), doc.Get(WORD_FN).ToString(), doc.Get(ID_FN).ToString() }; result.Add(current); } } return(result); }
public TopDocs SearchIndex(string querytext) { querytext = querytext.ToLower(); // var multiFieldQuery = Parse Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 500); totalresultLabel.Text = "Total number of result is: " + (results.TotalHits).ToString(); int i = 0; int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; Array.Resize <string>(ref searchResultList, i + 1); Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); //string myFieldValue = doc.Get(TEXT_FN).ToString(); string titleValue = doc.Get(TITLE_FN).ToString(); string abstractValue = doc.Get(ABSTRACT_FN).ToString(); searchResultList[i] = "Q0 " + (scoreDoc.Doc + 1) + " " + rank + " " + scoreDoc.Score + " "; i++; } return(results); }
//search public string SearchIndex(string queryText) { string output = "Nothing"; if (queryText != "") { queryText.ToLower(); Query query = parser.Parse(queryText); docs = searcher.Search(query, 1400);//here, 1400 means requiring to find up to 1400 documents //because it is impossible to be more than 1400. numofrelevant = docs.TotalHits; //it represent how many documents found already(no more than 100) output = "There are " + numofrelevant.ToString() + " relavant documents.\r\n"; //display numofdoc = 10; int totaldoc = 10; if (docs.ScoreDocs.Length < 10) { totaldoc = docs.ScoreDocs.Length; } if (numofrelevant > 0) { output = output + "The relevant documents from 1 to " + totaldoc.ToString() + " are as follow:\r\n"; } option.Clear();//option is a list used to store the documents found in the screen.notice, if the screen is changed, the option list will be changed. for (int i = 0; i < totaldoc; i++) { ScoreDoc scoredoc = docs.ScoreDocs[i]; Document doc1 = searcher.Doc(scoredoc.Doc); option.Add(doc1.Get(DocID)); //output = output + "Document " + scoredoc.Doc.ToString() + ":\r\n"; output = output + "Rank " + (i + 1).ToString() + ": " + DocID + ":" + doc1.Get(DocID) + "\r\n"; output = output + TITLE + ":" + doc1.Get(TITLE) + "\r\n"; output = output + AUTHOR + ":" + doc1.Get(AUTHOR) + "\r\n"; output = output + BIBLiINFO + ":" + doc1.Get(BIBLiINFO) + "\r\n"; //because requirement is to show the first sentence. char[] symbols = { '.', '?', '!' }; string[] sentences = doc1.Get(ABSTRACT).ToString().Split(symbols, StringSplitOptions.RemoveEmptyEntries); foreach (string sentence in sentences) { if (sentence.Length > 0) { output = output + "The first sentence of teh abstract:" + sentence + "\r\n"; break;//once I find the first sentence, I will jump out of the loop. } } //output = output + "The score is" + scoredoc.Score.ToString()+"\r\n"; } } return(output); }//end index search
public void FuzzyQueryTest() { string titulo = "titulo"; string texto = "texto"; using (var diretorio = new RAMDirectory()) { IndexarArquivosEmDocumento(diretorio, new Field[] { new Field(titulo, "fuzzy", Field.Store.YES, Field.Index.ANALYZED), new Field(titulo, "wuzzy", Field.Store.YES, Field.Index.ANALYZED) }); using (var searcher = new IndexSearcher(diretorio, true)) { var query = new FuzzyQuery(new Term(titulo, "wuzza")); var matches = searcher.Search(query, 10); Assert.AreEqual(2, matches.TotalHits, "both close enough"); Assert.IsTrue(matches.ScoreDocs[0].Score != matches.ScoreDocs[1].Score, "wuzzy closer then fuzzy"); var doc = searcher.Doc(matches.ScoreDocs[0].Doc); Assert.AreEqual("wuzzy", doc.Get(titulo), "wazza bear"); } } }
private static IList<int> SearchCore(SearchFilter searchFilter, out int totalHits) { if (!Directory.Exists(LuceneCommon.IndexDirectory)) { totalHits = 0; return new int[0]; } SortField sortField = GetSortField(searchFilter); int numRecords = searchFilter.Skip + searchFilter.Take; using (var directory = new LuceneFileSystem(LuceneCommon.IndexDirectory)) { var searcher = new IndexSearcher(directory, readOnly: true); var query = ParseQuery(searchFilter); var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); Filter filter = new QueryWrapperFilter(termQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(sortField)); var keys = results.scoreDocs.Skip(searchFilter.Skip) .Select(c => ParseKey(searcher.Doc(c.doc).Get("Key"))) .ToList(); totalHits = results.totalHits; searcher.Close(); return keys; } }
public SearchResults Search(DocumentRoot root, string term) { var results = new SearchResults(); var indexPath = _settings.GetSearchIndexPath(); var version = Lucene.Net.Util.Version.LUCENE_30; using (var directory = FSDirectory.Open(new DirectoryInfo(indexPath))) using (var indexReader = IndexReader.Open(directory, true)) using (var indexSearch = new IndexSearcher(indexReader)) { var analyzer = new StandardAnalyzer(version); var queryParser = new MultiFieldQueryParser(version, new[] { "Title", "Body" }, analyzer); var query = queryParser.Parse(term); var resultDocs = indexSearch.Search(query, indexReader.MaxDoc); var hits = resultDocs.ScoreDocs; foreach (var hit in hits) { var doc = indexSearch.Doc(hit.Doc); results.Documents.Add(new SearchResult { Score = hit.Score, Snippet = doc.Get("Snippet"), Title = doc.Get("Title") }); } } return results; }
/// <summary> /// Outputs results to the screen /// </summary> /// <param name="results">Search results</param> public void DisplayResults(TopDocs results) { List <String> evalrecords = new List <String>(); int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string field_URL = doc.Get(TEXT).ToString(); //string field_Text = doc.Get(TEXT_PASSAGE).ToString(); //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue); Console.WriteLine(); Console.WriteLine("Rank #" + rank); Console.WriteLine("Text: " + field_URL); Console.WriteLine(); string topicId = "001"; string studgroup = "10124021"; String record = topicId + " Q0 " + TEXT + "" + rank + " " + scoreDoc.Score + " " + studgroup; evalrecords.Add(record); } System.IO.File.WriteAllLines(@"C:\Users\Suprith Kangokar\Desktop\SEM 3\IFN647- Advance Info & Retreival\baseline.txt", evalrecords); }
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; }
private static DataTable ConvertToDataTable(IndexSearcher indexSearcher, ScoreDoc[] result) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(LuceneConfig.Field_Path)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_FileName)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_PageNumber)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_ContentByPage)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_Score)); foreach (ScoreDoc scoreDoc in result) { Document doc = indexSearcher.Doc(scoreDoc.Doc); DataRow dr = dt.NewRow(); dr[LuceneConfig.Field_Path] = doc.Get(LuceneConfig.Field_Path); dr[LuceneConfig.Field_FileName] = doc.Get(LuceneConfig.Field_FileName); dr[LuceneConfig.Field_PageNumber] = doc.Get(LuceneConfig.Field_PageNumber); dr[LuceneConfig.Field_ContentByPage] = doc.Get(LuceneConfig.Field_ContentByPage); dr[LuceneConfig.Field_Score] = scoreDoc.Score; dt.Rows.Add(dr); } return dt; }
public Task<List<TestDocument>> Query(string q) { QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "text", Analyzer); Query query = parser.Parse(q); IndexSearcher searcher = new IndexSearcher(Index, true); //Do the search TopDocs docs = searcher.Search(query, 10); int results = docs.TotalHits; List<TestDocument> ret = new List<TestDocument>(); for (int i = 0; i < results; i++) { ScoreDoc d = docs.ScoreDocs[i]; float score = d.Score; Document idoc = searcher.Doc(d.Doc); ret.Add(new TestDocument() { Id = Convert.ToInt32(idoc.GetField("id").StringValue), Text = idoc.GetField("text").StringValue }); } searcher.Dispose(); return Task.FromResult(ret); }
// Activity 7 /// <summary> /// Outputs results to the screen /// </summary> /// <param name="results">Search results</param> public void DisplayResults(TopDocs results) { int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); if (doc.GetField("passage_text") != null) { string Result_passage_text = doc.Get("passage_text").ToString(); string Result_url = doc.Get("url").ToString(); Console.WriteLine("\n\nRank: " + rank + "\nPassage text: " + Result_passage_text); Console.WriteLine("url: " + Result_url); } else { string Result_query = doc.Get("query").ToString(); string Result_answers = doc.Get("answers").ToString(); Console.WriteLine("\n\nRank: " + rank + "\nQuery: " + Result_query); Console.WriteLine("answers: " + Result_answers); } //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue); } }
public static ICollection<SearchResult> Execute(string query) { ICollection<SearchResult> searchResults = new List<SearchResult>(); string directoryPath = AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\LuceneIndexes"; var directory = FSDirectory.Open(directoryPath); var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "SearchBody", analyzer); Query searchQuery = parser.Parse(query + "*"); IndexSearcher searcher = new IndexSearcher(directory); TopDocs hits = searcher.Search(searchQuery, 200); int results = hits.ScoreDocs.Length; for (int i = 0; i < results; i++) { Document doc = searcher.Doc(hits.ScoreDocs[i].Doc); var searchResult = new SearchResult(); searchResult.EntityId = int.Parse(doc.Get("EntityId")); searchResult.EntityTypeName = doc.Get("EntityTypeName"); searchResult.SearchTitle = doc.Get("SearchTitle"); searchResult.SearchBody = doc.Get("SearchBody"); searchResults.Add(searchResult); } searcher.Dispose(); directory.Dispose(); return searchResults; }
public Data searchLucene(Data data) { Account_lg account = new Account_lg(); List<string> item = new List<string>(); Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo("C:\\Visual Studio 2010\\Transaction" + "\\LuceneIndex")); var analyzer = new StandardAnalyzer(Version.LUCENE_29); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "name", "username" }, analyzer); //search for multifield Query query = parser.Parse((data.getString("search")) + "*"); //cant search blank text with wildcard as first character TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true); searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; int count = hits.Length; for (int i = 0; i < count; i++) { int docId = hits[i].Doc; float score = hits[i].Score; Document doc = searcher.Doc(docId); string id = doc.Get("id"); item.Add(id); } Data list = account.selectUser(data, item.ToArray()); reader.Dispose(); searcher.Dispose(); return list; }
private IList<SearchResult> RunQuery(Query query) { // If two threads ran this method simultaneously, there would be issues with this.IndexReader. // Alternatively, there could be one RAMDirectory per filesystem directory. lock (this) { IndexReader newReader = this.indexReader.Reopen(); if (newReader != this.indexReader) { this.indexReader.Dispose(); this.indexReader = newReader; } IndexSearcher searcher; searcher = new IndexSearcher(newReader); if (query == null) { return new List<SearchResult>(); } TopDocs hits = searcher.Search(query, 1000); return hits.ScoreDocs .Select(scoreDoc => searcher.Doc(scoreDoc.Doc).Get(FullTextFieldType.Path.ToString())) .Select(path => new SearchResult(path)) .ToList(); } }
private static void SearchByFld2(string fld, string txt) { string strIndexDir = @"D:\Index"; Analyzer std = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)); Lucene.Net.Search.Searcher srchr = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(directory, true)); var parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, fld, std); Lucene.Net.Search.Query qry = parser.Parse(txt); var cllctr = srchr.Search(qry, 1000); Console.WriteLine(cllctr.TotalHits); ScoreDoc[] hits = cllctr.ScoreDocs; for (int i = 0; i < hits.Length; i++) { int docId = hits[i].Doc; float score = hits[i].Score; Lucene.Net.Documents.Document doc = srchr.Doc(docId); Console.WriteLine("索引时间:" + doc.Get("addtime")); Console.WriteLine("Searched from Text: " + doc.Get(fld)); } Console.WriteLine("over"); }
public Task<SearchResultCollection> Search(string search) { return System.Threading.Tasks.Task.Run(() => { var src = new SearchResultCollection(); if (string.IsNullOrWhiteSpace(search)) return src; try { var parser = new QueryParser(Version.LUCENE_30,"All", analyzer); Query q = new TermQuery(new Term("All", search)); using (var indexSearcher = new IndexSearcher(directory, true)) { Query query = parser.Parse(search); TopDocs result = indexSearcher.Search(query, 50); foreach (ScoreDoc h in result.ScoreDocs) { Document doc = indexSearcher.Doc(h.Doc); string id = doc.Get("id"); BaseContent value; if (LookupTable.TryGetValue(id, out value)) src.Add(new SearchResult {Relevance = h.Score, Content = value}); } } } catch (Exception e) { Logger.Log("DataServer","Error lucene search",e.Message,Logger.Level.Error); } return src; }); }
public static LuceneResult SearchBIMXchange(string field, string key, int pageSize, int pageNumber) { const string luceneIndexPath = "C:\\LuceneIndex"; var directory = FSDirectory.Open(new DirectoryInfo(luceneIndexPath)); var analyzer = new StandardAnalyzer(Version.LUCENE_29); var parser = new QueryParser(Version.LUCENE_29, field, analyzer); var query = parser.Parse(String.Format("{0}*", key)); var searcher = new IndexSearcher(directory, true); var topDocs = searcher.Search(query, 1000000); var docs = new List<Document>(); var start = (pageNumber-1)*pageSize; for (var i = start; i < start + pageSize && i < topDocs.TotalHits; i++) { var scoreDoc = topDocs.ScoreDocs[i]; var docId = scoreDoc.doc; var doc = searcher.Doc(docId); docs.Add(doc); } searcher.Close(); directory.Close(); var result = new LuceneResult {Results = docs, TotalCount = topDocs.TotalHits}; return result; }
public List<int> AiComplete(string args) { List<int> list = new List<int>(); try { IndexReader citac = IndexReader.Open(this.folder, true); var seracher = new IndexSearcher(citac); var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Content", new KeywordAnalyzer()); queryParser.AllowLeadingWildcard = true; var query = queryParser.Parse(args+"*"); TopDocs result = seracher.Search(query, 5); var lista = result.ScoreDocs; foreach (var hint in lista) { var h = seracher.Doc(hint.Doc); list.Add(h.Get("ArticlesID").ToInt()); } } catch (Exception) { } return list; }
public IEnumerable<MedStandardInfo> Find(string keywords, IProgress<string> progress) { using (var directory = GetDirectory()) using (var searcher = new IndexSearcher(directory)) { var query = GetQuery(keywords); var sort = GetSort(); var docs = searcher.Search(query, null, 1000, sort); var result = new List<MedStandardInfo>(); foreach (var scoreDoc in docs.ScoreDocs) { var doc = searcher.Doc(scoreDoc.Doc); var product = new MedStandardInfo() { StandardName = doc.Get("StandardName"), FileName = doc.Get("FileName"), Mkb = doc.Get("Mkb"), OrderNum = doc.Get("OrderNum"), }; result.Add(product); } return result; } }
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); }
static List<String[]> readQuestionInput(List<String> input) { List<String[]> results = new List<String[]>(); IndexReader indexReader = IndexReader.Open(directory, true); Searcher indexSearch = new IndexSearcher(indexReader); foreach (var keyword in input) { var query = createQuery("Content" , keyword , analyzer); //var query = createBooleanQuery("Content", input); //Console.WriteLine("Searching for : " + query); TopDocs resultDocs = indexSearch.Search(query, indexReader.MaxDoc); var hits = resultDocs.ScoreDocs; foreach (var hit in hits) { var documentFromSearch = indexSearch.Doc(hit.Doc); //results.Add(new String[]{ "Path: " , documentFromSearch.Get("Path")}); foreach (String[] result in ContentSpilt.indexof_withPro(input, documentFromSearch.Get("Content"))) { String[] temp = new String[3]; temp[0] = documentFromSearch.Get("Subject") ; temp[1] = result[0]; temp[2] = result[1]; results.Add(temp); } } } return results; }
private IList <Document> GetSearchHits(Query q) { if (q == null) { return(null); } Lucene.Net.Search.IndexSearcher searcher = Index.CreateSearcher(); var hits = searcher.Search(q, Int32.MaxValue); if (this.Explain > -1) { this.Explanation = searcher.Explain(q, this.Explain); } IList <Document> hitsToReturn = new List <Document>(); foreach (var searchDoc in hits.ScoreDocs) { var doc = searcher.Doc(searchDoc.Doc); hitsToReturn.Add(doc); } return(hitsToReturn); }
public IEnumerable<Card> Query(string searchStr = null) { if (string.IsNullOrEmpty(searchStr)) { foreach (var card in _cards) { yield return card.Value; } yield break; } var query = CreateQuery(searchStr); var searcher = new IndexSearcher(_directory); var hits = searcher.Search(query, _cards.Count).ScoreDocs; foreach (var hit in hits) { var document = searcher.Doc(hit.Doc); var cardName = document.Get("id"); yield return _cards[cardName]; } }
public SearchResult[] Search(string searchString) { Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_29); QueryParser parser = new QueryParser(Version.LUCENE_29, "Content", analyzer); var query = parser.Parse(searchString); Searcher searcher = new IndexSearcher(Lucene.Net.Index.IndexReader.Open(directory, true)); TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true); searcher.Search(query, collector); var hits = collector.TopDocs().ScoreDocs; List<SearchResult> results = new List<SearchResult>(); for (int i = 0; i < hits.Length; i++) { int docId = hits[i].Doc; float score = hits[i].Score; Lucene.Net.Documents.Document doc = searcher.Doc(docId); results.Add(new SearchResult { BookId = Guid.Parse(doc.Get("BookId")), Score = score }); } return results.ToArray(); }
private void button1_Click(object sender, EventArgs e) { Directory index = new RAMDirectory(); StandardAnalyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); IndexWriter w = new IndexWriter(index, analyzer); addDoc(w, "Lucene in Action"); addDoc(w, "Lucene for Dummies"); addDoc(w, "Managing Gigabytes"); addDoc(w, "The Art of Computer Science"); w.Close(); String querystr = "Lucene in Action"; Query q = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", analyzer).Parse(querystr); //q.Parse(); int hitsPerPage = 10; IndexReader reader = IndexReader.Open(index,true); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.Create(hitsPerPage, true); searcher.Search(q, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; System.Console.WriteLine("Found {0} Hits", hits.Length); foreach (var item in hits) { int docId = item.Doc; Document d = searcher.Doc(docId); System.Console.WriteLine(d.Get("title") + " " + item.Score); } }
private IQueryable<Package> SearchCore(SearchFilter searchFilter, out int totalHits) { int numRecords = searchFilter.Skip + searchFilter.Take; var searcher = new IndexSearcher(_directory, readOnly: true); var query = ParseQuery(searchFilter); // IF searching by relevance, boost scores by download count. if (searchFilter.SortProperty == SortProperty.Relevance) { var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); query = new CustomScoreQuery(query, downloadCountBooster); } var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); var filter = new QueryWrapperFilter(termQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter))); totalHits = results.totalHits; if (results.totalHits == 0 || searchFilter.CountOnly) { return Enumerable.Empty<Package>().AsQueryable(); } var packages = results.scoreDocs .Skip(searchFilter.Skip) .Select(sd => PackageFromDoc(searcher.Doc(sd.doc))) .ToList(); return packages.AsQueryable(); }
// ============================= 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("=============================="); }
private void SetResults(TopDocs results) { ResultSet = new System.Collections.ArrayList(); ResultSavingInfo = new System.Collections.ArrayList(); int rank = 0; string CPath = init.getCollectionPath(); foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); int firstNewLineIndx = doc.Get(".W").ToString().IndexOf("\n"); string DocId = doc.Get(".I").ToString().Replace("\n", String.Empty); ResultSet.Add("[" + DocId + "]. " + doc.Get(".T").ToString().Replace("\n", String.Empty) + " ( R" + rank + " )\r\n" + " " + doc.Get(".A").ToString().Replace("\n", String.Empty) + "\r\n" + " " + doc.Get(".B").ToString().Replace("\n", String.Empty) + "\r\n" + " " + doc.Get(".W").ToString().Substring(0, firstNewLineIndx) + " ...\r\n" + " " + "file://" + CPath + "/" + DocId + ".txt"); ResultSavingInfo.Add(new string[] { QueryID, "Q0", DocId, rank.ToString(), scoreDoc.Score.ToString(), "9583131_9837809_9539361_ACH" }); } }
public void CanQueryLuceneIndexCreatedOnDisk() { CanCreateLuceneIndexOnDisk(); System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.GetTempPath()); using (Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(di)) { Lucene.Net.Index.IndexReader ir = Lucene.Net.Index.IndexReader.Open(directory, true); Lucene.Net.Search.Searcher searcher = new Lucene.Net.Search.IndexSearcher(ir); using (Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)) { Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Version.LUCENE_30, "content", analyzer); Lucene.Net.Search.Query query = parser.Parse("lorem"); Lucene.Net.Search.TopScoreDocCollector collector = Lucene.Net.Search.TopScoreDocCollector.Create(100, true); searcher.Search(query, collector); Lucene.Net.Search.ScoreDoc[] docs = collector.TopDocs().ScoreDocs; foreach (Lucene.Net.Search.ScoreDoc scoreDoc in docs) { //Get the document that represents the search result. Document document = searcher.Doc(scoreDoc.Doc); var id = document.Get("Id"); var content = document.Get("content"); } } } }
private void btnExecuteSearch_Click(object sender, EventArgs e) { Directory indexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(tempPath)); IndexSearcher searcher = new IndexSearcher(indexDirectory, true); // read-only=true QueryParser qp = new HebrewQueryParser(Lucene.Net.Util.Version.LUCENE_29, "content", analyzer); qp.SetDefaultOperator(QueryParser.Operator.AND); Query query = qp.Parse(txbSearchQuery.Text); ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs; // Iterate through the results: BindingList<SearchResult> l = new BindingList<SearchResult>(); for (int i = 0; i < hits.Length; i++) { Document hitDoc = searcher.Doc(hits[i].doc); SearchResult sr = new SearchResult(hitDoc.GetField("title").StringValue(), hitDoc.GetField("path").StringValue(), hits[i].score); l.Add(sr); } searcher.Close(); indexDirectory.Close(); dgvResults.DataSource = l; }
public ProjectData[] Search(string searchTerm) { IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); IntegralCollector searcherCollector = new IntegralCollector(); // Setup the fields to search through string[] searchfields = new string[] { "name", "vessel" }; // Build our booleanquery that will be a combination of all the queries for each individual search term var finalQuery = new BooleanQuery(); var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, searchfields, analyzer); // Split the search string into separate search terms by word string[] terms = searchTerm.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); foreach (string term in terms) finalQuery.Add(parser.Parse(term.Replace("~", "") + "~"),Occur.SHOULD); searcher.Search(finalQuery, searcherCollector); var results = new ProjectData[searcherCollector.Docs.Count]; for (int i = 0; i < searcherCollector.Docs.Count; i++) { var doc = searcher.Doc(searcherCollector.Docs[i]); results[i] = new ProjectData(doc.Get("name"), doc.Get("vessel")); } return results; }
public static LuceneResult MultiSearchBIMXchange(Dictionary<string,string> terms, int pageSize, int pageNumber) { var directory = FSDirectory.Open(new DirectoryInfo("LuceneIndex")); var booleanQuery = new BooleanQuery(); foreach(var term in terms) { var query = new TermQuery(new Term(term.Key, term.Value)); booleanQuery.Add(query,BooleanClause.Occur.MUST); } var searcher = new IndexSearcher(directory, true); var topDocs = searcher.Search(booleanQuery, 10); var docs = new List<Document>(); var start = (pageNumber - 1) * pageSize; for (var i = start; i < start + pageSize && i < topDocs.TotalHits; i++) { var scoreDoc = topDocs.ScoreDocs[i]; var docId = scoreDoc.doc; var doc = searcher.Doc(docId); docs.Add(doc); } searcher.Close(); directory.Close(); var result = new LuceneResult {Results = docs, TotalCount = topDocs.TotalHits}; return result; }
public Data searchLucene(Data data) { Search_gl search = new Search_gl(); List<string> item = new List<string>(); Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex")); var analyzer = new StandardAnalyzer(Version.LUCENE_29); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //QueryParser queryParser = new QueryParser(Version.LUCENE_29, "summary", analyzer); //search for single field MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] {"name", "summary"}, analyzer); //search for multifield Query query = parser.Parse((data.getString("search")) + "*"); //cant search blank text with wildcard as first character TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; int count = hits.Length; for (int i = 0; i < count; i++) { int docId = hits[i].doc; float score = hits[i].score; Document doc = searcher.Doc(docId); string id = doc.Get("id"); item.Add(id); } Data list = search.search(data, item.ToArray()); reader.Close(); searcher.Close(); return list; }
public IList<SearchDocument> Search(IList<Filter> searchFilters, IList<SearchSortField> sortFields) { if (searchFilters == null || searchFilters.Count == 0) return new List<SearchDocument>(); var sort = CreateSort(searchFilters, sortFields); var reader = IndexReader.Open(_directory, true); var searcher = new IndexSearcher(reader); var query = CreateQuery(searchFilters); var hits = searcher.Search(query, new QueryWrapperFilter(query), 100, sort); var searchResult = new List<SearchDocument>(); foreach (var facet in hits.ScoreDocs) { var doc = searcher.Doc(facet.Doc); var searchDoc = new SearchDocument { Fields = new List<SearchDocumentField>() }; var docFields = doc.GetFields(); foreach (var field in docFields) { var value = doc.Get(field.Name); searchDoc.Fields.Add(new SearchDocumentField {FieldName = field.Name, Value = value}); } searchResult.Add(searchDoc); } return searchResult; }
public ISearchResult Search(string query) { var timer = new Stopwatch(); timer.Start(); var directory = FSDirectory.Open(new DirectoryInfo(path)); var analyzer = new StandardAnalyzer(Version.LUCENE_29); var searcher = new IndexSearcher(directory, true); var queryParser = new QueryParser(Version.LUCENE_29, "text", analyzer); var result = searcher.Search(queryParser.Parse(query), 20); var docs = (from scoreDoc in result.scoreDocs let doc = searcher.Doc(scoreDoc.doc) let fields = new Dictionary<string, string> { { "title", doc.Get("title") }, { "text", doc.Get("text") } } select new LuceneDocument { Id = scoreDoc.doc.ToString(), Fields = fields }).ToList(); var ret = new SearchResult { Query = query, Total = result.totalHits, Documents = docs, Source = Name }; searcher.Close(); directory.Close(); timer.Stop(); ret.Duration = (decimal) timer.Elapsed.TotalSeconds; return ret; }
public static void Arquive(string text, string url, bool isEnglish) { //INICIALIZAR VARIÁVEIS CONSOANTE SER EM INGLÊS OU EM PORTUGUÊS string directory; Lucene.Net.Analysis.Analyzer analyzer; Object _mon= isEnglish? _monEn : _monPt; InitPathAndAnalyzer(out directory, out analyzer, isEnglish); //CRIAR UM NOVO DOCUMENTO COM A INFORMAÇÃO NECESSÁRIA: UM CAMPO COM O URL E UM OUTRO COM O CONTEUDO A ANALIZAR string hash= text.GetHashCode().ToString(); Document document = new Document(); document.Add(new Field("url", url, Field.Store.YES,Field.Index.NOT_ANALYZED)); document.Add(new Field("text", text, Field.Store.NO, Field.Index.ANALYZED)); document.Add(new Field("hash",hash, Field.Store.YES, Field.Index.NOT_ANALYZED)); Monitor.Enter(_mon); Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo(directory)); if (System.IO.Directory.GetFiles(directory).Length == 0) { IndexWriter init = new IndexWriter(dir, analyzer, true, new IndexWriter.MaxFieldLength(25000)); init.Dispose(); } //VERIFICAR SE O DOCUMENTO JÁ EXISTE E SE CONTEM A MESMA INFORMAÇÃO QUE JÁ LÁ SE ENCONTRA IndexSearcher isearcher = new IndexSearcher(dir, false); //O CAMPO DE PROCURA SERA O URL QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "url", analyzer); Query query = parser.Parse(url); int s = isearcher.IndexReader.NumDocs(); //Console.WriteLine(isearcher.IndexReader.NumDocs()); ScoreDoc[] hits = isearcher.Search(query, null, 1000).ScoreDocs; if(hits.Length>0){ Document doc = isearcher.Doc(hits[0].Doc); //Console.WriteLine(doc.Get("url")); //É IGUAL if (doc.Get("hash").Equals(hash)) { Monitor.Exit(_mon); return; } //É DIFERENTE else { isearcher.IndexReader.DeleteDocument(hits[0].Doc); } } isearcher.Dispose(); //ACEDER AO INDEX COM A INFORMAÇÃO INDEXADA DA LINGUAGEM CORRECTA E ADICIONAR O NOVO DOCUMENTO IndexWriter iwriter = new IndexWriter(dir, analyzer, false, new IndexWriter.MaxFieldLength(25000)); iwriter.AddDocument(document); iwriter.Optimize(); iwriter.Dispose(); Monitor.Exit(_mon); }
private void StartSearch() { string searchText = txtSearch.Text; string whitelist = "^[a-zA-Z0-9-,. ]+$"; Regex pattern = new Regex(whitelist); if (!pattern.IsMatch(searchText)) { throw new Exception("Invalid Search Criteria"); } //Supply conditions Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Contents", analyzer); Query query = parser.Parse(searchText); string indexPath = HttpContext.Current.Request.PhysicalPath.Replace("Default.aspx", "Indexes\\"); Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath)); Lucene.Net.Search.Searcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(dir, true)); TopDocs topDocs = searcher.Search(query, 100); int countResults = topDocs.ScoreDocs.Length; if (lblSearchResults.Text.Length > 0) { lblSearchResults.Text = ""; } if (countResults > 0) { string results; results = string.Format("<br />Search Results <br />"); for (int i = 0; i < countResults; i++) { ScoreDoc scoreDoc = topDocs.ScoreDocs[i]; int docId = scoreDoc.Doc; float score = scoreDoc.Score; Lucene.Net.Documents.Document doc = searcher.Doc(docId); string docPath = doc.Get("FileName"); string urlLink = "~/" + docPath.Substring(docPath.LastIndexOf("Help"), docPath.Length - docPath.LastIndexOf("Help")).Replace("\\", "/"); results += "Text found in: <a href=" + urlLink.Replace("~/Help/", "") + "?txtSearch=" + searchText + ">" + urlLink + "</a><br />"; } lblSearchResults.Text += results; } else { lblSearchResults.Text = "No records found for \"" + searchText + "\""; } searcher.Dispose(); }
public static Document[] PerformQuery(string indexPath, string queryText, string defaultFieldName, Analyzer analyzer, int resultAmount = 100) { Directory searchDirectory = FSDirectory.Open(indexPath); IndexSearcher searcher = new IndexSearcher(searchDirectory); QueryParser parser = new QueryParser(Version.LUCENE_30, defaultFieldName, analyzer); Query query = parser.Parse(queryText); TopDocs topDocs = searcher.Search(query, resultAmount); return topDocs.ScoreDocs.Select(sDoc => searcher.Doc(sDoc.Doc)).ToArray(); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string searchKey = context.Request["wd"]; string indexPath = context.Server.MapPath("../IndexData"); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //搜索条件 PhraseQuery query = new PhraseQuery(); //把用户输入的关键字进行分词 foreach (string word in Picture.Utility.SplitContent.SplitWords(searchKey)) { query.Add(new Term("tag", word)); } //query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系 query.SetSlop(100); //指定关键词相隔最大距离 //TopScoreDocCollector盛放查询结果的容器 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器 //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果 ScoreDoc[] docs = collector.TopDocs(0,10).scoreDocs; //展示数据实体对象集合 var tagModels = new List<Picture.Model.TagModel>(); for (int i = 0; i < docs.Length; i++) { int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id) Document doc = searcher.Doc(docId);//根据文档id来获得文档对象Document Picture.Model.TagModel tag = new Picture.Model.TagModel(); //picture.ImgSummary = doc.Get("summary"); tag.TagName= Picture.Utility.SplitContent.HightLight(searchKey, doc.Get("tag")); //book.ContentDescription = doc.Get("content");//未使用高亮 //搜索关键字高亮显示 使用盘古提供高亮插件 //book.ContentDescription = Picture.Utility.SplitContent.HightLight(Request.QueryString["SearchKey"], doc.Get("content")); tag.TId = Convert.ToInt32(doc.Get("id")); tagModels.Add(tag); } SearchPreviewResult result = new SearchPreviewResult() { q=searchKey, p=false }; foreach (var item in tagModels) { result.s.Add(item.TagName); } System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.Write(jss.Serialize(result)); }
public void DisplayResult(TopDocs topDocs) { for (int i = 0; i < topDocs.ScoreDocs.Length; i++) { Lucene.Net.Documents.Document document = searcher.Doc(topDocs.ScoreDocs[i].Doc); string fieldValue = document.Get(TEXT_FN).ToString(); Console.WriteLine("Rank " + (i + 1) + " text : " + fieldValue); } }
// Processes query and ranks results public void SearchIndex(string querytext, ref TopDocs results, ref int numberOfDocuments) { parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, TEXT_FN, analyzer); querytext = querytext.ToLower(); Query query = parser.Parse(querytext); results = searcher.Search(query, 1400); numberOfDocuments = results.TotalHits; int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string myFieldValue = doc.Get(TEXT_FN).ToString(); //Console.WriteLine("Rank " + rank + " text " + myFieldValue); } }
private HelpData GetHelpDataByDocID(int docID, IndexSearcher indexSearcher) { var document = indexSearcher.Doc(docID); return new HelpData { BasicInfo = document.Get("BasicInfo"), Description = document.Get("Description"), ScreenName = document.Get("ScreenName"), ViewName = document.Get("ViewName") }; }
public void DisplayResults(TopDocs topDocs) { int i = 1; foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs) { Document doc = searcher.Doc(scoreDoc.Doc); string text = doc.Get(TEXT_FN).ToString(); Console.WriteLine($"Rank {i++} Text: {text}"); } }
public TopDocs SearchIndex(string querytext) { System.Console.WriteLine("Searching for " + querytext); querytext = querytext.ToLower(); Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 500); totalresultLabel.Text = "Total number of result is " + (results.TotalHits).ToString(); int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string myFieldValue = doc.Get(TEXT_FN).ToString(); } return(results); }
public List <Dictionary <string, string> > SearchIndex(Query query, Dictionary <string, float> weights) { //parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, parserFields, shingleAnalyzer, weights); TopDocs results = null; //System.Console.WriteLine("Searching for " + query.ToString()); try { results = searcher.Search(query, 200); } catch (Exception e) { // invalid string } List <Dictionary <string, string> > fullResults = new List <Dictionary <string, string> >(); int docIndex = 0; foreach (ScoreDoc doc in results.ScoreDocs) { Document fullDoc = searcher.Doc(doc.Doc); Dictionary <string, string> fields = new Dictionary <string, string>(); foreach (Field f in fullDoc.GetFields()) { fields[f.Name] = f.StringValue; } fields[DOC_RANK] = doc.Score.ToString().Replace("\n", ""); if (fields.Keys.Count > 0) { fullResults.Add(fields); } docIndex++; } Console.WriteLine("================================================================================================"); System.Console.WriteLine("Number of results is " + results.TotalHits); DisplayResults(results); return(fullResults); }
private void Explain(LNS.Query query) { int j = 0; while (j < collector.Array.Count) { int i; i = collector.Array.GetNextTrueIndex(j); if (i >= collector.Array.Count) { break; } j = i + 1; Document doc = searcher.Doc(i); LNS.Explanation exp = searcher.Explain(query, i); Log.Debug("Query: [{0}]", query); Log.Debug("Matching URI: {0}", doc.Get("Uri")); Log.Debug("Explanation: {0}", exp); } }
public List <Item> search(string keyWord) { //string strAnalyzerType = comboBox2.SelectedItem.ToString(); string strAnalyzerType = "StandardAnalyzer"; if (comboBox2.SelectedItem != null) { strAnalyzerType = comboBox2.SelectedItem.ToString(); } string strType = "A"; if (textBox3.Text != "") { strType = textBox3.Text.ToString(); } FSDirectory dir = FSDirectory.Open(indexLocation); List <Item> results = new List <Item>(); Lucene.Net.Util.Version AppLuceneVersion = Lucene.Net.Util.Version.LUCENE_30; Lucene.Net.Analysis.Analyzer analyzer = AnalyzerHelper.GetAnalyzerByName(strAnalyzerType); //Lucene.Net.QueryParsers.MultiFieldQueryParser parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(AppLuceneVersion,new string[] { "content","title" }, analyzer); IDictionary <String, Single> dictionary = new Dictionary <String, Single>(); dictionary.Add("title", 5); dictionary.Add("content", 10); Lucene.Net.QueryParsers.MultiFieldQueryParser parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(AppLuceneVersion, new string[] { "title", "content" }, analyzer); //parser.DefaultOperator = Lucene.Net.QueryParsers.QueryParser.Operator.AND; Lucene.Net.Search.Query query = parser.Parse(keyWord); //parser. //BooleanClause //Lucene.Net.Search.BooleanClause booleanClauses = new BooleanClause(); //BooleanClause // BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST }; Occur[] occurs = new Occur[] { Occur.MUST, Occur.MUST }; Lucene.Net.Search.Query query2 = Lucene.Net.QueryParsers.MultiFieldQueryParser.Parse(AppLuceneVersion, new string[] { keyWord, strType }, new string[] { "content", "title" }, occurs, analyzer); Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir); TopDocs topdocs = searcher.Search(query2, 100); Lucene.Net.Search.ScoreDoc[] hits = topdocs.ScoreDocs; foreach (var hit in hits) { var foundDoc = searcher.Doc(hit.Doc); results.Add(new Item { title = foundDoc.Get("title"), content = foundDoc.Get("content") }); } searcher.Dispose(); return(results); }
public EntityInfo Extract(TopDocs topDocs, Lucene.Net.Search.IndexSearcher searcher, int index) { ScoreDoc scoreDoc = topDocs.ScoreDocs[index]; Document doc = searcher.Doc(scoreDoc.Doc); //TODO if we are lonly looking for score (unlikely), avoid accessing doc (lazy load) EntityInfo entityInfo = Extract(doc); object[] eip = entityInfo.Projection; if (eip != null && eip.Length > 0) { for (int x = 0; x < projection.Length; x++) { switch (projection[x]) { case ProjectionConstants.SCORE: eip[x] = scoreDoc.Score; break; case ProjectionConstants.ID: eip[x] = entityInfo.Id; break; case ProjectionConstants.DOCUMENT: eip[x] = doc; break; case ProjectionConstants.DOCUMENT_ID: eip[x] = scoreDoc.Doc; break; //case ProjectionConstants.BOOST: // eip[x] = doc.Boost; // break; case ProjectionConstants.THIS: //THIS could be projected more than once //THIS loading delayed to the Loader phase if (entityInfo.IndexesOfThis == null) { entityInfo.IndexesOfThis = new List <int>(1); } entityInfo.IndexesOfThis.Add(x); break; } } } return(entityInfo); }
public void DisplayResults(TopDocs td) { int rank = 0; foreach (ScoreDoc sd in td.ScoreDocs) { rank++; // retrieve the document from the "ScoreDoc" object Document doc = searcher.Doc(sd.Doc); string myfieldValue = doc.Get(TEXT_FN).ToString(); Console.WriteLine($"Rank {rank} text {myfieldValue}"); } }
// Activity 7 /// <summary> /// Outputs results to the screen /// </summary> /// <param name="results">Search results</param> public void DisplayResults(TopDocs results) { var form1 = new Form1(); int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string myFieldValue = doc.Get(TEXT_FN).ToString(); ListViewItem item = new ListViewItem(rank.ToString()); item.SubItems.Add(myFieldValue); form1.listView.Items.Add(item); } }
public List <string[]> DisplayResults(TopDocs results, string label) { List <string[]> retrievedDocs = new List <string[]>(); foreach (ScoreDoc scoreDoc in results.ScoreDocs) { // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string[] temp = new string[2]; temp[0] = doc.Get("ID"); temp[1] = doc.Get(label); retrievedDocs.Add(temp); } return(retrievedDocs); }
public List <Item> Search2(string keyWord) { //string strAnalyzerType = comboBox2.SelectedItem.ToString(); string strAnalyzerType = "StandardAnalyzer"; if (comboBox2.SelectedItem != null) { strAnalyzerType = comboBox2.SelectedItem.ToString(); } string strType = "A"; if (textBox3.Text != "") { strType = textBox3.Text.ToString(); } FSDirectory dir = FSDirectory.Open(indexLocation); List <Item> results = new List <Item>(); Lucene.Net.Util.Version AppLuceneVersion = Lucene.Net.Util.Version.LUCENE_30; Lucene.Net.Analysis.Analyzer analyzer = AnalyzerHelper.GetAnalyzerByName(strAnalyzerType); Lucene.Net.QueryParsers.MultiFieldQueryParser parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(AppLuceneVersion, new string[] { "title", "content" }, analyzer); ///////////////////////////////////////////////// BooleanQuery query = new BooleanQuery(); Query queryType = new TermQuery(new Term("title", strType)); Query queryKey = new TermQuery(new Term("content", keyWord)); query.Add(queryType, Occur.MUST); query.Add(queryKey, Occur.MUST); Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir); TopDocs topdocs = searcher.Search(query, 100); Lucene.Net.Search.ScoreDoc[] hits = topdocs.ScoreDocs; foreach (var hit in hits) { var foundDoc = searcher.Doc(hit.Doc); results.Add(new Item { title = foundDoc.Get("title"), content = foundDoc.Get("content") }); } searcher.Dispose(); return(results); }
public List <Result> DisplayResults(TopDocs results, Lucene.Net.Search.Query query) { int rank = 0; List <Result> retrievedResults = new List <Result>(); foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string url = doc.Get(URL_FN).ToString(); string title = GetTitle(url); int passageID = Int32.Parse(doc.Get(PASSAGEID_FN)); retrievedResults.Add(new Result(rank, scoreDoc.Score, title, url, null, null, passageID)); } return(retrievedResults); }
static public int GetResults(TopDocs results) { int rank = 0; items.Clear(); foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Document doc = searcher.Doc(scoreDoc.Doc); string content = doc.Get(CONTENT_FN).ToString(); string passage_id = doc.Get(PASSAGE_ID_FN).ToString(); string query_id = doc.Get(QUERY_ID_FN).ToString(); string is_selected = doc.Get(IS_SELECTED_FN).ToString(); string url = doc.Get(URL_FN).ToString(); items.Add(new[] { rank.ToString(), query_id, content, url, passage_id }); } return(results.TotalHits); }
/// <summary> /// Outputs results to the screen /// </summary> /// <param name="results">Search results</param> public void DisplayResults(TopDocs results) { int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string field_URL = doc.Get(TEXT_URL).ToString(); //string field_Text = doc.Get(TEXT_PASSAGE).ToString(); //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue); Console.WriteLine(); Console.WriteLine("Rank #" + rank); Console.WriteLine("Result: " + field_URL); //Console.WriteLine("Passage Text: "); //Console.WriteLine(field_Text); Console.WriteLine(); } }
public StoredInfo GetStoredInfo(Uri uri) { StoredInfo info = new StoredInfo(); LNS.Query query = UriQuery("Uri", uri); SingletonCollector collector = new SingletonCollector(); LNS.IndexSearcher searcher = LuceneCommon.GetSearcher(PrimaryStore); searcher.Search(query, null, collector); if (collector.MatchId != -1) { Document doc = searcher.Doc(collector.MatchId); info = DocumentToStoredInfo(doc); } LuceneCommon.ReleaseSearcher(searcher); return(info); }
public List <string> DisplayResults(TopDocs results, Lucene.Net.Search.Query query) { int rank = 0; List <string> retrievedResults = new List <string>(); foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; // retrieve the document from the 'ScoreDoc' object Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string myFieldValue = doc.Get(TEXT_FN).ToString(); //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue); //console.writeline("rank " + rank + "\n"+ myfieldvalue); string[] sArray = myFieldValue.Split(new char[] { '‰' }, StringSplitOptions.RemoveEmptyEntries); string title = GetTitle(sArray[0]); string previewText = GeneratePreviewText(query, sArray[1]); retrievedResults.Add(rank + "‰" + scoreDoc.Score + "‰" + scoreDoc.Doc + "‰" + previewText + "‰" + title + myFieldValue); } return(retrievedResults); }
private static void SearchByFld(string fld, string txt) { string strIndexDir = @"D:\Index"; Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)); //Provide the directory where index is stored using (var srchr = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(directory, true))) //true opens the index in read only mode { Query query = new TermQuery(new Term(fld, txt)); TopDocs result = srchr.Search(query, 10); Console.WriteLine(result.TotalHits); foreach (var scoreDoc in result.ScoreDocs) { int docId = scoreDoc.Doc; Lucene.Net.Documents.Document doc = srchr.Doc(docId); var contents = doc.Get(fld); Console.WriteLine(contents); } } }
/// <summary> /// test for both constant score and boolean query, the other tests only use the constant score mode </summary> private void TestRange(int precisionStep) { string field = "field" + precisionStep; int count = 3000; int lower = (Distance * 3 / 2) + StartOffset, upper = lower + count * Distance + (Distance / 3); NumericRangeQuery <int> q = NumericRangeQuery.NewInt32Range(field, precisionStep, lower, upper, true, true); NumericRangeFilter <int> f = NumericRangeFilter.NewInt32Range(field, precisionStep, lower, upper, true, true); for (sbyte i = 0; i < 3; i++) { TopDocs topDocs; string type; switch (i) { case 0: type = " (constant score filter rewrite)"; q.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; topDocs = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER); break; case 1: type = " (constant score boolean rewrite)"; q.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE; topDocs = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER); break; case 2: type = " (filter)"; topDocs = Searcher.Search(new MatchAllDocsQuery(), f, NoDocs, Sort.INDEXORDER); break; default: return; } ScoreDoc[] sd = topDocs.ScoreDocs; Assert.IsNotNull(sd); Assert.AreEqual(count, sd.Length, "Score doc count" + type); Document doc = Searcher.Doc(sd[0].Doc); Assert.AreEqual(2 * Distance + StartOffset, doc.GetField(field).GetInt32Value().Value, "First doc" + type); doc = Searcher.Doc(sd[sd.Length - 1].Doc); Assert.AreEqual((1 + count) * Distance + StartOffset, doc.GetField(field).GetInt32Value().Value, "Last doc" + type); } }