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)); }
public virtual void TestBasic() { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); Document doc = new Document(); doc.Add(new Field("field", "value", Field.Store.NO, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Close(); TermQuery termQuery = new TermQuery(new Term("field", "value")); // should not throw exception with primitive query QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery); IndexSearcher searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10); Assert.AreEqual(1, hits.totalHits); // should not throw exception with complex primitive query BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.Add(termQuery, Occur.MUST); booleanQuery.Add(new TermQuery(new Term("field", "missing")), Occur.MUST_NOT); qwf = new QueryWrapperFilter(termQuery); hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10); Assert.AreEqual(1, hits.totalHits); // should not throw exception with non primitive Query (doesn't implement // Query#createWeight) qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu"))); hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10); Assert.AreEqual(1, hits.totalHits); }
public void TestTermSearch() { using (var dir = FSDirectory.Open(TestEnvironment.TestIndexDirectory)) using (var indexSearcher = new IndexSearcher(dir)) { var termSubjectAnt = new Term("subject", "ant"); var termQuerySubjectAnt = new TermQuery(termSubjectAnt); var topDocsSubjectAnt = indexSearcher.Search(termQuerySubjectAnt, 10); // title=Ant in Action // subject=apache ant build tool junit java development Assert.Equal(1, topDocsSubjectAnt.TotalHits); var termSubjectJUnit = new Term("subject", "junit"); var termQuerySubjectJUnit = new TermQuery(termSubjectJUnit); var topDocsSubjectJUnit = indexSearcher.Search(termQuerySubjectJUnit, 10); // ExplainResults(indexSearcher, termQuerySubjectJUnit, topDocsSubjectJUnit); // title=JUnit in Action, Second Edition // subject=junit unit testing mock objects // title=Ant in Action // subject=apache ant build tool junit java development Assert.Equal(2, topDocsSubjectJUnit.TotalHits); // Ants in Action, "JUnit in Action, Second Edition" } }
// main search method private static IEnumerable<SampleData> _search(string searchQuery, string searchField = "") { // validation if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<SampleData>(); // set up lucene searcher using (var searcher = new IndexSearcher(_directory, false)) { var hits_limit = 1000; var analyzer = new StandardAnalyzer(Version.LUCENE_30); // search by single field if (!string.IsNullOrEmpty(searchField)) { var parser = new QueryParser(Version.LUCENE_30, searchField, analyzer); var query = parseQuery(searchQuery, parser); var hits = searcher.Search(query, hits_limit).ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return results; } // search by multiple fields (ordered by RELEVANCE) else { var parser = new MultiFieldQueryParser (Version.LUCENE_30, new[] {"Id", "Name", "Description"}, analyzer); var query = parseQuery(searchQuery, parser); var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return results; } } }
public virtual void TestQuery() { RAMDirectory dir = new RAMDirectory(); IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true); AddDoc("one", iw); AddDoc("two", iw); AddDoc("three four", iw); iw.Close(); IndexSearcher is_Renamed = new IndexSearcher(dir); Hits hits = is_Renamed.Search(new MatchAllDocsQuery()); Assert.AreEqual(3, hits.Length()); // some artificial queries to trigger the use of skipTo(): BooleanQuery bq = new BooleanQuery(); bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); hits = is_Renamed.Search(bq); Assert.AreEqual(3, hits.Length()); bq = new BooleanQuery(); bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); bq.Add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST); hits = is_Renamed.Search(bq); Assert.AreEqual(1, hits.Length()); // delete a document: is_Renamed.GetIndexReader().DeleteDocument(0); hits = is_Renamed.Search(new MatchAllDocsQuery()); Assert.AreEqual(2, hits.Length()); is_Renamed.Close(); }
/// <summary> /// Searches the index with the specified query text /// </summary> /// <param name="querytext">Text to search the index</param> /// <returns></returns> static public TopDocs SearchIndex(string querytext, out double time) { start = DateTime.Now; querytext = querytext.ToLower(); char[] splitters = new char[] { '\t', '\'', '-', '(', ')', ',', '’', '\n', ':', ';', '?', '.', '!', '*' }; foreach (var s in splitters) { querytext = querytext.Replace(s.ToString(), string.Empty); } try { Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 100); end = DateTime.Now; time = (end - start).TotalMilliseconds; return(results); } catch { string onlyLetters = new string(querytext.Where(char.IsLetter).ToArray()); if (onlyLetters.Length == 0) { onlyLetters = "asdfghjklzxcvbnmqwertyuiop"; } Query query = parser.Parse(onlyLetters); TopDocs results = searcher.Search(query, 100); end = DateTime.Now; time = (end - start).TotalMilliseconds; return(results); } //time = (end - start).TotalMilliseconds; //return; }
public TopDocs SearchIndex(string term) { Query query = parser.Parse(term); PhraseQuery phraseQuery = parser.PhraseSlop(term); Lucene.Net.Search.TopDocs topDocs = searcher.Search(query, 10); Console.WriteLine(topDocs.TotalHits + " documents has been returned."); return(topDocs); }
// Test that FieldScoreQuery returns docs with expected score. private void DoTestCustomScore(System.String field, FieldScoreQuery.Type tp, double dboost) { float boost = (float)dboost; IndexSearcher s = new IndexSearcher(dir, true); FieldScoreQuery qValSrc = new FieldScoreQuery(field, tp); // a query that would score by the field QueryParser qp = new QueryParser(Util.Version.LUCENE_CURRENT, TEXT_FIELD, anlzr); System.String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup. // regular (boolean) query. Query q1 = qp.Parse(qtxt); Log(q1); // custom query, that should score the same as q1. CustomScoreQuery q2CustomNeutral = new CustomScoreQuery(q1); q2CustomNeutral.Boost = boost; Log(q2CustomNeutral); // custom query, that should (by default) multiply the scores of q1 by that of the field CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, qValSrc); q3CustomMul.SetStrict(true); q3CustomMul.Boost = boost; Log(q3CustomMul); // custom query, that should add the scores of q1 to that of the field CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, qValSrc); q4CustomAdd.SetStrict(true); q4CustomAdd.Boost = boost; Log(q4CustomAdd); // custom query, that multiplies and adds the field score to that of q1 CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, qValSrc, qValSrc); q5CustomMulAdd.SetStrict(true); q5CustomMulAdd.Boost = boost; Log(q5CustomMulAdd); // do al the searches TopDocs td1 = s.Search(q1, null, 1000); TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000); TopDocs td3CustomMul = s.Search(q3CustomMul, null, 1000); TopDocs td4CustomAdd = s.Search(q4CustomAdd, null, 1000); TopDocs td5CustomMulAdd = s.Search(q5CustomMulAdd, null, 1000); // put results in map so we can verify the scores although they have changed System.Collections.Hashtable h1 = TopDocsToMap(td1); System.Collections.Hashtable h2CustomNeutral = TopDocsToMap(td2CustomNeutral); System.Collections.Hashtable h3CustomMul = TopDocsToMap(td3CustomMul); System.Collections.Hashtable h4CustomAdd = TopDocsToMap(td4CustomAdd); System.Collections.Hashtable h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd); VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd); }
/// <summary> /// Searches the index. /// </summary> /// <param name="queryText"></param> /// <param name="keywordFilter">A Hashtable where the key is the fieldname of the keyword and /// the value the keyword itself.</param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public SearchResultCollection Find(string queryText, Hashtable keywordFilter, int pageIndex, int pageSize) { long startTicks = DateTime.Now.Ticks; Hits hits; try { Query query = MultiFieldQueryParser.Parse(queryText, new string[] {"title", "contents"}, new StandardAnalyzer()); IndexSearcher searcher = new IndexSearcher(this._indexDirectory); if (keywordFilter != null && keywordFilter.Count > 0) { QueryFilter qf = BuildQueryFilterFromKeywordFilter(keywordFilter); hits = searcher.Search(query, qf); } else { hits = searcher.Search(query); } int start = pageIndex*pageSize; int end = (pageIndex + 1)*pageSize; if (hits.Length() <= end) { end = hits.Length(); } SearchResultCollection results = new SearchResultCollection(); results.TotalCount = hits.Length(); results.PageIndex = pageIndex; for (int i = start; i < end; i++) { SearchResult result = new SearchResult(); result.Title = hits.Doc(i).Get("title"); result.Summary = hits.Doc(i).Get("summary"); result.Author = hits.Doc(i).Get("author"); result.ModuleType = hits.Doc(i).Get("moduletype"); result.Path = hits.Doc(i).Get("path"); result.Category = hits.Doc(i).Get("category"); result.DateCreated = DateTime.Parse((hits.Doc(i).Get("datecreated"))); result.Score = hits.Score(i); result.Boost = hits.Doc(i).GetBoost(); result.SectionId = Int32.Parse(hits.Doc(i).Get("sectionid")); results.Add(result); } searcher.Close(); results.ExecutionTime = DateTime.Now.Ticks - startTicks; return results; } catch (Exception ex) { throw new SearchException("Error while performing full-text search.", ex); } }
public virtual void TestBefore() { // create an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); long now = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); Document doc = new Document(); // add time that is in the past doc.Add(new Field("datefield", DateTools.TimeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Optimize(); writer.Close(); IndexSearcher searcher = new IndexSearcher(indexStore, true); // filter that should preserve matches //DateFilter df1 = DateFilter.Before("datefield", now); TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now, DateTools.Resolution.MILLISECOND), false, true); // filter that should discard matches //DateFilter df2 = DateFilter.Before("datefield", now - 999999); TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools.TimeToString(0, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, false); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); ScoreDoc[] result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1, null, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, null, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); // run queries with DateFilter result = searcher.Search(query1, df1, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query1, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, df1, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); result = searcher.Search(query2, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); }
public LuceneBitArray And(LNS.Query query) { UseScratch(); searcher.Search(query, null, collector); if (Debug) { Explain(query); } this.And(scratch); return(this); }
public virtual void TestBefore() { // create an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); long now = (long) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); // add time that is in the past doc.Add(new Field("datefield", Lucene.Net.Documents.DateTools.TimeToString(now - 1000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED)); doc.Add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED)); writer.AddDocument(doc); writer.Optimize(); writer.Close(); IndexSearcher searcher = new IndexSearcher(indexStore); // filter that should preserve matches //DateFilter df1 = DateFilter.Before("datefield", now); RangeFilter df1 = new RangeFilter("datefield", Lucene.Net.Documents.DateTools.TimeToString(now - 2000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Lucene.Net.Documents.DateTools.TimeToString(now, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), false, true); // filter that should discard matches //DateFilter df2 = DateFilter.Before("datefield", now - 999999); RangeFilter df2 = new RangeFilter("datefield", Lucene.Net.Documents.DateTools.TimeToString(0, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Lucene.Net.Documents.DateTools.TimeToString(now - 2000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), true, false); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); Hits result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1); Assert.AreEqual(0, result.Length()); result = searcher.Search(query2); Assert.AreEqual(1, result.Length()); // run queries with DateFilter result = searcher.Search(query1, df1); Assert.AreEqual(0, result.Length()); result = searcher.Search(query1, df2); Assert.AreEqual(0, result.Length()); result = searcher.Search(query2, df1); Assert.AreEqual(1, result.Length()); result = searcher.Search(query2, df2); Assert.AreEqual(0, result.Length()); }
public virtual void TestBefore() { // create an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); long now = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; Document doc = new Document(); // add time that is in the past doc.Add(Field.Keyword("datefield", DateField.TimeToString(now - 1000))); doc.Add(Field.Text("body", "Today is a very sunny day in New York City")); writer.AddDocument(doc); writer.Optimize(); writer.Close(); IndexSearcher searcher = new IndexSearcher(indexStore); // filter that should preserve matches DateFilter df1 = DateFilter.Before("datefield", now); // filter that should discard matches DateFilter df2 = DateFilter.Before("datefield", now - 999999); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); Hits result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1); Assert.AreEqual(0, result.Length()); result = searcher.Search(query2); Assert.AreEqual(1, result.Length()); // run queries with DateFilter result = searcher.Search(query1, df1); Assert.AreEqual(0, result.Length()); result = searcher.Search(query1, df2); Assert.AreEqual(0, result.Length()); result = searcher.Search(query2, df1); Assert.AreEqual(1, result.Length()); result = searcher.Search(query2, df2); Assert.AreEqual(0, result.Length()); }
public void TestBooleanQuerySerialization() { Lucene.Net.Search.BooleanQuery lucQuery = new Lucene.Net.Search.BooleanQuery(); lucQuery.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field", "x")), Occur.MUST); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bf.Serialize(ms, lucQuery); ms.Seek(0, System.IO.SeekOrigin.Begin); Lucene.Net.Search.BooleanQuery lucQuery2 = (Lucene.Net.Search.BooleanQuery)bf.Deserialize(ms); ms.Close(); Assert.AreEqual(lucQuery, lucQuery2, "Error in serialization"); Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir, true); int hitCount = searcher.Search(lucQuery, 20).TotalHits; searcher.Close(); searcher = new Lucene.Net.Search.IndexSearcher(dir, true); int hitCount2 = searcher.Search(lucQuery2, 20).TotalHits; Assert.AreEqual(hitCount, hitCount2, "Error in serialization - different hit counts"); }
public void TestBooleanQuerySerialization() { Lucene.Net.Search.BooleanQuery lucQuery = new Lucene.Net.Search.BooleanQuery(); lucQuery.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field", "x")), Occur.MUST); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); #pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) bf.Serialize(ms, lucQuery); ms.Seek(0, System.IO.SeekOrigin.Begin); Lucene.Net.Search.BooleanQuery lucQuery2 = (Lucene.Net.Search.BooleanQuery)bf.Deserialize(ms); #pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) ms.Close(); Assert.AreEqual(lucQuery, lucQuery2, "Error in serialization"); using var reader = DirectoryReader.Open(dir); //Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir, true); Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(reader); int hitCount = searcher.Search(lucQuery, 20).TotalHits; //searcher.Close(); searcher = new Lucene.Net.Search.IndexSearcher(reader); int hitCount2 = searcher.Search(lucQuery2, 20).TotalHits; Assert.AreEqual(hitCount, hitCount2, "Error in serialization - different hit counts"); }
private bool isInIndex(IndexableFileInfo fileInfo) { IndexSearcher searcher = new IndexSearcher(this.luceneIndexDir); try { BooleanQuery bq = new BooleanQuery(); bq.Add(new TermQuery(new Term("filename", fileInfo.Filename)), BooleanClause.Occur.MUST); bq.Add(new TermQuery(new Term("LastModified", DateTools.DateToString(fileInfo.LastModified, DateTools.Resolution.SECOND))), BooleanClause.Occur.MUST); Hits hits = searcher.Search(bq); int count = hits.Length(); if (count > 0) { return(true); } } catch (Exception ex) { Console.Write(ex.Message); } finally { searcher.Close(); } return(false); }
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); }
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); }
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 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; }
private void HandleRangeFacet(string index, Facet facet, IndexQuery indexQuery, IndexSearcher currentIndexSearcher, Dictionary<string, IEnumerable<FacetValue>> results) { var rangeResults = new List<FacetValue>(); foreach (var range in facet.Ranges) { var baseQuery = database.IndexStorage.GetLuceneQuery(index, indexQuery, database.IndexQueryTriggers); //TODO the built-in parser can't handle [NULL TO 100.0}, i.e. a mix of [ and } //so we need to handle this ourselves (greater and less-than-or-equal) var rangeQuery = database.IndexStorage.GetLuceneQuery(index, new IndexQuery { Query = facet.Name + ":" + range }, database.IndexQueryTriggers); var joinedQuery = new BooleanQuery(); joinedQuery.Add(baseQuery, BooleanClause.Occur.MUST); joinedQuery.Add(rangeQuery, BooleanClause.Occur.MUST); var topDocs = currentIndexSearcher.Search(joinedQuery, null, 1); if (topDocs.TotalHits > 0) { rangeResults.Add(new FacetValue { Count = topDocs.TotalHits, Range = range }); } } results[facet.Name] = rangeResults; }
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); }
private static IEnumerable<CustomerId> _search(string searchQuery) { // validation if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<CustomerId>(); // set up lucene searcher using (var searcher = new IndexSearcher(_directory, false)) { var hits_limit = 1000; var analyzer = new StandardAnalyzer(Version.LUCENE_29); { //var query = new TermQuery(new Term("CustomerName", searchQuery)); var query = new BooleanQuery(); query.Add(new TermQuery(new Term("CustomerName", searchQuery)), Occur.MUST); var hits = searcher.Search(query, hits_limit).ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); 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; }
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(); } }
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 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 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 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; }
// ============================= 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 void MrsJones() { using (var dir = new RAMDirectory()) using (var analyzer = new LowerCaseKeywordAnalyzer()) { using (var writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { var document = new Lucene.Net.Documents.Document(); document.Add(new Field("Name", "MRS. SHABA", Field.Store.NO, Field.Index.ANALYZED_NO_NORMS)); writer.AddDocument(document); } var searcher = new IndexSearcher(dir, true); var termEnum = searcher.IndexReader.Terms(); while (termEnum.Next()) { var buffer = termEnum.Term.Text; Console.WriteLine(buffer); } var queryParser = new RangeQueryParser(Version.LUCENE_29, "", analyzer); var query = queryParser.Parse("Name:\"MRS. S*\""); Console.WriteLine(query); var result = searcher.Search(query, 10); Assert.NotEqual(0, result.TotalHits); } }
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 override void Warm(IndexReader r) { IndexSearcher s = new IndexSearcher(r); Lucene.Net.Search.TopDocs hits = s.Search(new TermQuery(new Term("foo", "bar")), 10, null); Assert.AreEqual(20, hits.TotalHits); }
public void Initialize_Indexes_All_Nodes() { string elementIdForTestingSearch = _deepNodeFinder.GetNodesForIndexing()[0].Id; int expectedNumNodes = _deepNodeFinder.GetNodesForIndexing().Length; Assert.AreEqual("usfr-pte_NetCashFlowsProvidedUsedOperatingActivitiesDirectAbstract", elementIdForTestingSearch, "TEST SANITY: element id for test search"); Assert.AreEqual(1595, expectedNumNodes, "TEST SANITY: Number of nodes in found in the test taxonomy"); IndexReader indexReader = IndexReader.Open(_indexMgr.LuceneDirectory_ForTesting); Assert.AreEqual(expectedNumNodes, indexReader.NumDocs(), "An incorrect number of documents were found in the Lucene directory after initialization"); IndexSearcher searcher = new IndexSearcher(_indexMgr.LuceneDirectory_ForTesting); try { Hits results = searcher.Search(new TermQuery(new Term(LuceneNodeIndexer.ELEMENTID_FOR_DELETING_FIELD, elementIdForTestingSearch))); Assert.AreEqual(1, results.Length(), "Search results should only have 1 hit"); Assert.AreEqual(elementIdForTestingSearch, results.Doc(0).Get(LuceneNodeIndexer.ELEMENTID_FIELD), "Search results yielded the wrong element!"); } finally { searcher.Close(); } }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected override void Warm(IndexSearcher searcher) { searcher.Search(new MatchAllDocsQuery(), 1); // Create the tenant filters _filters = new Dictionary<string, Filter>(); IEnumerable<string> tenantIds = PackageTenantId.GetDistintTenantId(searcher.IndexReader); foreach (string tenantId in tenantIds) { _filters.Add(tenantId, new CachingWrapperFilter(new TenantFilter(tenantId))); } _publicFilter = new CachingWrapperFilter(new PublicFilter()); _latestVersion = new CachingWrapperFilter(LatestVersionFilter.Create(searcher.IndexReader, false, false)); _latestVersionIncludeUnlisted = new CachingWrapperFilter(LatestVersionFilter.Create(searcher.IndexReader, false, true)); _latestVersionIncludePrerelease = new CachingWrapperFilter(LatestVersionFilter.Create(searcher.IndexReader, true, false)); _latestVersionIncludePrereleaseIncludeUnlisted = new CachingWrapperFilter(LatestVersionFilter.Create(searcher.IndexReader, true, true)); // Recalculate precalculated Versions arrays PackageVersions packageVersions = new PackageVersions(searcher.IndexReader); _versionsByDoc = packageVersions.CreateVersionsLookUp(null); _versionListsByDoc = packageVersions.CreateVersionListsLookUp(); // Set metadata LastReopen = DateTime.UtcNow; NumDocs = searcher.IndexReader.NumDocs(); CommitUserData = searcher.IndexReader.CommitUserData; }
public TopDocs SearchForQuery(string querytext, out Lucene.Net.Search.Query query, bool toProcess, bool toExpand) // Searches index with query text { Stopwatch stopwatch2 = Stopwatch.StartNew(); if (!toProcess) { querytext = "\"" + querytext + "\""; } else { if (toExpand) { var standardParser = new QueryParser(VERSION, URL_FN, new StandardAnalyzer(VERSION)); querytext = standardParser.Parse(querytext).ToString().Replace("Url:", "").Replace("Query:", ""); querytext = GetSynonyms(querytext); } querytext = querytext.ToLower(); } if (querytext != string.Empty) { query = parser.Parse(querytext); currentQuery = query; stopwatch2.Stop(); queryTime = stopwatch2.Elapsed.TotalSeconds.ToString(); finalQuery = query.ToString(); TopDocs results = searcher.Search(query, 100); return(results); } else { query = null; return(null); } }
private void button2_Click(object sender, EventArgs e) { String field = "content"; IndexReader reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(INDEX_DIR.FullName)), true); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, field, analyzer); Query query = parser.Parse(textBox1.Text.Trim()); TopScoreDocCollector collector = TopScoreDocCollector.Create(searcher.MaxDoc, false); searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; MessageBox.Show(this, "共 " + collector.TotalHits.ToString() + " 条记录"); //ltrResult.Text = "共 " + collector.GetTotalHits().ToString() + " 条记录<br>"; //for (Int32 i = 0; i < collector.GetTotalHits(); i++) //{ // ltrResult.Text += "doc=" + hits[i].doc + " score=" + hits[i].score + "<br>"; // Document doc = searcher.Doc(hits[i].doc); // ltrResult.Text += "Path:" + doc.Get("path") + "<br>"; //} reader.Dispose(); }
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; }); }
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 virtual void TestReverseDateSort() { IndexSearcher searcher = new IndexSearcher(directory); // Create a Sort object. reverse is set to true. // problem occurs only with SortField.AUTO: Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.AUTO, true)); QueryParser queryParser = new QueryParser(TEXT_FIELD, new WhitespaceAnalyzer()); Query query = queryParser.Parse("Document"); // Execute the search and process the search results. System.String[] actualOrder = new System.String[5]; ScoreDoc[] hits = searcher.Search(query, null, 1000, sort).scoreDocs; for (int i = 0; i < hits.Length; i++) { Document document = searcher.Doc(hits[i].doc); System.String text = document.Get(TEXT_FIELD); actualOrder[i] = text; } searcher.Close(); // Set up the expected order (i.e. Document 5, 4, 3, 2, 1). System.String[] expectedOrder = new System.String[5]; expectedOrder[0] = "Document 5"; expectedOrder[1] = "Document 4"; expectedOrder[2] = "Document 3"; expectedOrder[3] = "Document 2"; expectedOrder[4] = "Document 1"; Assert.AreEqual(new System.Collections.ArrayList(expectedOrder), new System.Collections.ArrayList(actualOrder)); }
public SearchResults Find(string terms) { Directory directory = FSDirectory.GetDirectory("./index",false); // Now search the index: var isearcher = new IndexSearcher(directory); // Parse a simple query that searches for "text": //Query query = QueryParser.Parse("text", "fieldname", analyzer); var qp = new QueryParser("description", _analyzer); Query query = qp.Parse(terms); Hits hits = isearcher.Search(query); var sr = new SearchResults(); // Iterate through the results: for (int i = 0; i < hits.Length(); i++) { Document hitDoc = hits.Doc(i); sr.Add(new Result() { Name = hitDoc.Get("name"), Description = hitDoc.Get("description") }); } isearcher.Close(); directory.Close(); return sr; }
public void RemoveFromIndex(string[] keys, string value) { var analyzer = new AccentedAnalyzer(); var query = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, keys, analyzer); var directory = FSDirectory.Open(System.IO.Directory.GetParent(IndexPath)); IndexReader idxReader = IndexReader.Open(indexPath); var searcher = new IndexSearcher(directory, true); query.SetDefaultOperator(QueryParser.Operator.AND); var q = query.Parse(value); int top = idxReader.MaxDoc(); var results = TopScoreDocCollector.create(top, true); searcher.Search(q, results); ScoreDoc[] hits = results.TopDocs().scoreDocs; Document[] documents = new Document[hits.Length]; IndexReader indexReader = null; for (int i = 0; i < hits.Length; i++) { int docId = hits[i].doc; indexReader = IndexReader.Open(directory, false); indexReader.DeleteDocument(docId); indexReader.Commit(); indexReader.Flush(); indexReader.Close(); } searcher.Close(); directory.Close(); }
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 ArrayList getNotesMatchingTitle(string search) { ArrayList snotes = new ArrayList (); try { QueryParser parser = new QueryParser ("title", analyzer); string lucsearch = search + "*^4" + " content:" + search + "*"; Query query = parser.Parse (lucsearch); IndexSearcher searcher = new IndexSearcher (lucIdx); Hits hits = searcher.Search (query); int results = hits.Length (); Console.WriteLine ("Found {0} results", results); for (int i = 0; i < results; i++) { Document doc = hits.Doc (i); //float score = hits.Score (i); snotes.Add (new Note (doc.Get ("title"), doc.Get ("lastmod"))); } } catch (Exception e) { Console.WriteLine ("ERROR Search: " + e.Message); } return snotes; }
public Lucene.Net.Search.TopDocs SearchIndex(string text) { text.ToLower(); query = parser.Parse(text); Lucene.Net.Search.TopDocs doc = searcher.Search(query, 1000); searcher.Dispose(); return(doc); }
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 TopDocs SearchIndex(string queryStr) { Console.WriteLine($"Searching for: {queryStr}"); Query query = parser.Parse(queryStr); TopDocs results = searcher.Search(query, 100); Console.WriteLine($"Number of results is {results.TotalHits}"); return(results); }
public TopDocs SearchIndex(string querytext) { querytext = querytext.ToLower(); Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 2800); NoOfDocs = results.TotalHits; return(results); }
/// <summary> /// Searches the index with the specified query text /// </summary> /// <param name="querytext">Text to search the index</param> /// <returns></returns> public TopDocs SearchIndex(string querytext) { //form.ResultsUpdate("Searching for " + querytext); querytext = querytext.ToLower(); Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 10); //form.ResultsUpdate("Found " + results.TotalHits + " documents."); return(results); }
/// <summary> /// Searches the index with the specified query text /// </summary> /// <param name="querytext">Text to search the index</param> /// <returns></returns> public TopDocs SearchIndex(string querytext) { System.Console.WriteLine("Searching for " + querytext); querytext = querytext.ToLower(); Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 10); System.Console.WriteLine("Found " + results.TotalHits + " documents."); return(results); }
public Lucene.Net.Search.TopDocs SearchIndex(string text) { Console.WriteLine("Searching for " + text); text.ToLower(); Query query = parser.Parse(text); Lucene.Net.Search.TopDocs doc = searcher.Search(query, 3); Console.WriteLine("Total no of Results " + doc.TotalHits.ToString()); return(doc); }
public TopDocs SearchIndex(string query) { Console.WriteLine($"Searching for {query}..."); // construct a lucene query Query qo = parser.Parse(query); // retrieve the results using the query TopDocs td = searcher.Search(qo, 100); // return the results return(td); }
/// Executes the query. // Preprocesses the query text entered by the user // and queries the index. // Calculates the total time to run the query // and sets some text variables for later use. public int RunQuery(string text, bool preproc, out string qText) { // start timer... DateTime start = DateTime.Now; // get the query settings from the collection IRQueryParams queryParams = myCollection.GetQueryParams(); string[] queryFields = queryParams.Fields; float[] queryFieldBoosts = queryParams.FieldBoosts; // build field boost dictionary IDictionary <string, float> boosts = new Dictionary <string, float>(); for (int i = 0; i < queryFields.Length; i++) { boosts.Add(queryFields[i], queryFieldBoosts[i]); } // setup searcher, query and parser CreateSearcher(); Query query; parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, queryFields, analyzer, boosts); // preprocess query (if required) if (preproc == true) { query = PreprocessQuery(text, parser); } else { // no preprocessing query = parser.Parse(text); } // print query text to form qText = query.ToString(); // execute the search searchResults = searcher.Search(query, maxResults); // end timer and calculate total time DateTime end = DateTime.Now; TimeSpan duration = end - start; queryTime = duration.Seconds + (float)duration.Milliseconds / 1000; CleanUpSearcher(); return(searchResults.TotalHits); }
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); }
//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 Test_Store_RAMDirectory() { Lucene.Net.Store.RAMDirectory ramDIR = new Lucene.Net.Store.RAMDirectory(); //Index 1 Doc Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); var conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(ramDIR, conf /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED*/); Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); wr.AddDocument(doc); wr.Dispose(); //now serialize it System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); serializer.Serialize(memoryStream, ramDIR); //Close DIR ramDIR.Dispose(); ramDIR = null; //now deserialize memoryStream.Seek(0, System.IO.SeekOrigin.Begin); Lucene.Net.Store.RAMDirectory ramDIR2 = (Lucene.Net.Store.RAMDirectory)serializer.Deserialize(memoryStream); //Add 1 more doc Lucene.Net.Analysis.Analyzer analyzer2 = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); var conf2 = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); wr = new Lucene.Net.Index.IndexWriter(ramDIR2, conf2 /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED*/); doc = new Lucene.Net.Documents.Document(); doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); wr.AddDocument(doc); wr.Dispose(); Lucene.Net.Search.TopDocs topDocs; //Search using (var reader = DirectoryReader.Open(ramDIR2)) { Lucene.Net.Search.IndexSearcher s = new Lucene.Net.Search.IndexSearcher(reader); Lucene.Net.QueryParsers.Classic.QueryParser qp = new Lucene.Net.QueryParsers.Classic.QueryParser(Version.LUCENE_CURRENT, "field1", new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_CURRENT)); Lucene.Net.Search.Query q = qp.Parse("value1"); topDocs = s.Search(q, 100); } Assert.AreEqual(topDocs.TotalHits, 2, "See the issue: LUCENENET-174"); }
private TopDocs SearchIndex(string querytext) { Log += "\r\nSearching for ' " + querytext + " ' ~ ' "; querytext = querytext.ToLower(); querytext = Regex.Replace(querytext, @"[^0-9a-z.]+", " "); //querytext = querytext.Replace(",",String.Empty); Log += querytext + " ' "; Query query = parser.Parse(querytext); TopDocs results = searcher.Search(query, 1400); Log += "\r\nNumber of results is " + results.TotalHits; return(results); }
public IEnumerable <Document> Search(string propertyNameToSearch, string searchTerm, int topCount = int.MaxValue) { LuceneSearch.IndexSearcher searcher = new LuceneSearch.IndexSearcher(IndexDirectory); QueryParser parser = new QueryParser(LuceneVersion, propertyNameToSearch, AnalyzerProvider()); LuceneSearch.Query query = parser.Parse(searchTerm); LuceneSearch.TopDocs hits = searcher.Search(query, topCount); IndexReader reader = IndexReader.Open(IndexDirectory, true); for (int i = 0; i < hits.ScoreDocs.Length; i++) { LuceneSearch.ScoreDoc scoreDoc = hits.ScoreDocs[i]; Document doc = reader.Document(scoreDoc.Doc); yield return(doc); } }
/*** * Understands the lucene query syntax */ public List <Utilities.Language.TextIndexing.IndexResult> GetDocumentsWithQuery(string query) { List <Utilities.Language.TextIndexing.IndexResult> fingerprints = new List <Utilities.Language.TextIndexing.IndexResult>(); HashSet <string> fingerprints_already_seen = new HashSet <string>(); try { using (Lucene.Net.Index.IndexReader index_reader = Lucene.Net.Index.IndexReader.Open(LIBRARY_INDEX_BASE_PATH, true)) { using (Lucene.Net.Search.IndexSearcher index_searcher = new Lucene.Net.Search.IndexSearcher(index_reader)) { Lucene.Net.QueryParsers.QueryParser query_parser = new Lucene.Net.QueryParsers.QueryParser(Version.LUCENE_29, "content", analyzer); Lucene.Net.Search.Query query_object = query_parser.Parse(query); Lucene.Net.Search.Hits hits = index_searcher.Search(query_object); var i = hits.Iterator(); while (i.MoveNext()) { Lucene.Net.Search.Hit hit = (Lucene.Net.Search.Hit)i.Current; string fingerprint = hit.Get("fingerprint"); string page = hit.Get("page"); if (!fingerprints_already_seen.Contains(fingerprint)) { fingerprints_already_seen.Add(fingerprint); IndexResult index_result = new IndexResult { fingerprint = fingerprint, score = hit.GetScore() }; fingerprints.Add(index_result); } } // Close the index index_searcher.Close(); } index_reader.Close(); } } catch (Exception ex) { Logging.Warn(ex, "GetDocumentsWithQuery: There was a problem opening the index file for searching."); } return(fingerprints); }
public TopDocs SearchForQuery(string querytext, out Lucene.Net.Search.Query query, bool toProcess) // Searches index with query text { Stopwatch stopwatch2 = Stopwatch.StartNew(); querytext = querytext.ToLower(); if (!toProcess) { querytext = "\"" + querytext + "\""; } query = parser.Parse(querytext); stopwatch2.Stop(); queryTime = stopwatch2.Elapsed.TotalSeconds.ToString(); finalQuery = query.ToString(); TopDocs results = searcher.Search(query, 100); return(results); }