public static void Main(string[] args) { string indexpath = args[0]; string query = args[1]; IndexSearcher searcher = new IndexSearcher(indexpath); Query parsedquery = QueryParser.Parse(query, "summary", new StandardAnalyzer()); Hits hits = searcher.Search(parsedquery); Console.WriteLine("Found " + hits.Length() + " document(s) that matched query '" + query + "':\n"); for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); Console.WriteLine(hits.Score(i) + ": " + doc.Get("excerpt") + "\n"); if (i == 50) { break; } } searcher.Close(); }
public override List <ISearchEntity> GetSearchResult(out int MatchCount) { Analyzer analyzer = new StandardAnalyzer(); IndexSearcher searcher = new IndexSearcher(searchInfo.ConfigElement.IndexDirectory); MultiFieldQueryParser parserName = new MultiFieldQueryParser(new string[] { "title", "content", "keywords" }, analyzer); Query queryName = parserName.Parse(searchInfo.QueryString); Hits hits = searcher.Search(queryName); List <ISearchEntity> ResultList = new List <ISearchEntity>(); for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); ResultList.Add((ISearchEntity) new NewsModel() { EntityIdentity = Convert.ToInt32(doc.Get("newsid")), Title = Convert.ToString(doc.Get("title")), Content = Convert.ToString(doc.Get("content")), Keywords = doc.Get("keywords") }); } searcher.Close(); MatchCount = hits.Length(); return(ResultList); }
public static void PrintHits(Hits hits) { if (hits == null) { Console.WriteLine("Hits: 0"); } else { Console.WriteLine("Hits: " + hits.Length()); // iterate over the first few results. for (int i = 0; i < 50 && i < hits.Length(); i++) { var doc = hits.Doc(i); string id = doc.Get("id"); int n; for (n = 0; n < Data.GetLength(0); ++n) { if (id == Data[n, 1]) { Console.WriteLine("{0}: {1} ==> {2} {3}", n, GetPath(n), GetContent(n), GetExternals(n)); break; } } } Console.WriteLine("-"); } }
/// <summary> /// Searches the index. /// </summary> /// <param name="queryText"></param> /// <param name="categoryNames"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="roleIds"></param> /// <returns></returns> public SearchResultCollection Find(string queryText, IList <string> categoryNames, int pageIndex, int pageSize, IEnumerable <int> roleIds) { long startTicks = DateTime.Now.Ticks; // the overall-query BooleanQuery query = new BooleanQuery(); // add our parsed query if (!String.IsNullOrEmpty(queryText)) { Query multiQuery = MultiFieldQueryParser.Parse(new[] { queryText, queryText, queryText }, new[] { "title", "summary", "contents" }, new StandardAnalyzer()); query.Add(multiQuery, BooleanClause.Occur.MUST); } // add the security constraint - must be satisfied query.Add(this.BuildSecurityQuery(roleIds), BooleanClause.Occur.MUST); // Add the category query (if available) if (categoryNames != null) { query.Add(this.BuildCategoryQuery(categoryNames), BooleanClause.Occur.MUST); } IndexSearcher searcher = new IndexSearcher(this._indexDirectory); Hits hits = searcher.Search(query); int start = pageIndex * pageSize; int end = (pageIndex + 1) * pageSize; if (hits.Length() <= end) { end = hits.Length(); } SearchResultCollection results = new SearchResultCollection(end); 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"); string[] categories = hits.Doc(i).GetValues("category"); result.Category = categories != null?String.Join(", ", categories) : String.Empty; 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); }
//public List<int> SearchIuses(string searchTerm) //{ // List<int> results = new List<int>(); // IndexSearcher searcher = new IndexSearcher(FSDirectory.GetDirectory(indexPath)); // QueryParser parser = new QueryParser("Rubro", analyzer); // parser.SetEnablePositionIncrements(false); // PhraseQuery q = new PhraseQuery(); // String[] words = searchTerm.Split(' '); // foreach (string word in words) // { // q.Add(new Term("Rubro", word)); // } // Console.WriteLine(q.ToString()); // //Query query = parser.Parse(searchTerm); // Hits hitsFound = searcher.Search(q); // TesisIndx sampleDataFileRow = null; // for (int i = 0; i < hitsFound.Length(); i++) // { // sampleDataFileRow = new TesisIndx(); // Document doc = hitsFound.Doc(i); // sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); // sampleDataFileRow.Rubro = doc.Get("Rubro"); // sampleDataFileRow.Texto = doc.Get("Texto"); // float score = hitsFound.Score(i); // sampleDataFileRow.Score = score; // results.Add(sampleDataFileRow.Ius); // } // parser = new QueryParser("Texto", analyzer); // parser.SetEnablePositionIncrements(false); // q = new PhraseQuery(); // words = searchTerm.Split(' '); // foreach (string word in words) // { // q.Add(new Term("Texto", word)); // } // // query = parser.Parse(searchTerm); // hitsFound = searcher.Search(q); // for (int i = 0; i < hitsFound.Length(); i++) // { // sampleDataFileRow = new TesisIndx(); // Document doc = hitsFound.Doc(i); // sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); // sampleDataFileRow.Rubro = doc.Get("Rubro"); // sampleDataFileRow.Texto = doc.Get("Texto"); // float score = hitsFound.Score(i); // sampleDataFileRow.Score = score; // results.Add(sampleDataFileRow.Ius); // } // results.Distinct(); // return results; //} /// <summary> /// Busca en el índice previamente construido las tesis que tengan coincidencia ya sea en el Rubro o Texto /// del término buscado /// </summary> /// <param name="searchTerm"></param> /// <returns></returns> public List <int> SearchIuses(string searchTerm) { List <int> results = new List <int>(); IndexSearcher searcher = new IndexSearcher(FSDirectory.GetDirectory(indexPath)); QueryParser parser = new QueryParser("RubroIndx", analyzer); parser.SetEnablePositionIncrements(false); Query query = parser.Parse(String.Format("\"{0}\"", searchTerm)); Console.WriteLine(query.ToString()); Hits hitsFound = searcher.Search(query); TesisIndx sampleDataFileRow = null; for (int i = 0; i < hitsFound.Length(); i++) { sampleDataFileRow = new TesisIndx(); Document doc = hitsFound.Doc(i); sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); sampleDataFileRow.RubroIndx = doc.Get("RubroIndx"); sampleDataFileRow.TextoIndx = doc.Get("TextoIndx"); float score = hitsFound.Score(i); sampleDataFileRow.Score = score; results.Add(sampleDataFileRow.Ius); } parser = new QueryParser("TextoIndx", analyzer); parser.SetEnablePositionIncrements(false); query = parser.Parse(String.Format("\"{0}\"", searchTerm)); Console.WriteLine(query.ToString()); hitsFound = searcher.Search(query); for (int i = 0; i < hitsFound.Length(); i++) { sampleDataFileRow = new TesisIndx(); Document doc = hitsFound.Doc(i); sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); sampleDataFileRow.Rubro = doc.Get("RubroIndx"); sampleDataFileRow.Texto = doc.Get("TextoIndx"); float score = hitsFound.Score(i); sampleDataFileRow.Score = score; results.Add(sampleDataFileRow.Ius); } results.Distinct(); return(results); }
protected void Page_Load(object sender, EventArgs e) { //if (Session["KeyWords"] == null ? false : true) //{ // Response.Redirect("Search.aspx"); //} String text = Session["KeyWords"].ToString(); ChineseAnalyzer analyzer = new ChineseAnalyzer(); TokenStream ts = analyzer.TokenStream("ItemName", new System.IO.StringReader(text)); Lucene.Net.Analysis.Token token; try { int n = 0; while ((token = ts.Next()) != null) { this.lbMsg.Text += (n++) + "->" + token.TermText() + " " + token.StartOffset() + " " + token.EndOffset() + " " + token.Type() + "<br>"; // Response.Write((n++) + "->" + token.TermText() + " " + token.StartOffset() + " " //+ token.EndOffset() + " " + token.Type() + "<br>"); } } catch { this.lbMsg.Text = "wrong"; } // Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.GetDirectory(Server.MapPath("/indexFile/"), false); IndexSearcher isearcher = new IndexSearcher(directory); Query query; query = QueryParser.Parse(Session["KeyWords"].ToString(), "ItemName", analyzer); //query = QueryParser.Parse("2", "nid", analyzer); Hits hits = isearcher.Search(query); this.lbMsg.Text += "<font color=red>共找到" + hits.Length() + "条记录</font><br>"; //Response.Write("<font color=red>共找到" + hits.Length() + "条记录</font><br>"); for (int i = 0; i < hits.Length(); i++) { Document hitDoc = hits.Doc(i); this.lbMsg.Text += "编号:" + hitDoc.Get("ItemID").ToString() + "<br>" + "分类:" + hitDoc.Get("CategoryName").ToString() + "<br>" + "专题:" + hitDoc.Get("ProductName").ToString() + "<br>" + "标题:<a href=" + hitDoc.Get("visiturl").ToString() + ">" + hitDoc.Get("ItemName").ToString() + "</a><br>"; //Response.Write("编号:" + hitDoc.Get("ItemID").ToString() + "<br>"); //Response.Write("分类:" + hitDoc.Get("CategoryName").ToString() + "<br>"); //Response.Write("标题:<a href=" + hitDoc.Get("visiturl").ToString() + ">" + hitDoc.Get("ItemName").ToString() + "</a><br>"); //Response.Write("专题:" + hitDoc.Get("ProductName").ToString() + "<br>"); } isearcher.Close(); directory.Close(); }
private void SearchFor(int n, Searcher searcher) { System.Console.Out.WriteLine("Searching for " + n); Hits hits = searcher.Search(QueryParsers.QueryParser.Parse(English.IntToEnglish(n), "contents", Lucene.Net.ThreadSafetyTest.ANALYZER)); System.Console.Out.WriteLine("Search for " + n + ": total=" + hits.Length()); for (int j = 0; j < System.Math.Min(3, hits.Length()); j++) { System.Console.Out.WriteLine("Hit for " + n + ": " + hits.Doc(j).Get("id")); } }
private void CheckHits(Hits hits, int expectedCount) { Assert.AreEqual(expectedCount, hits.Length(), "total results"); for (int i = 0; i < hits.Length(); i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = hits.Doc(i); Assert.AreEqual(System.Convert.ToString(i), d.Get(ID_FIELD), "check " + i); } } }
private static void PrintHits(Hits hits) { System.Console.Out.WriteLine(hits.Length() + " total results\n"); for (int i = 0; i < hits.Length(); i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = hits.Doc(i); System.Console.Out.WriteLine(i + " " + d.Get(ID_FIELD)); } } }
private void PrintHits(System.IO.StringWriter out_Renamed, Hits hits) { out_Renamed.WriteLine(hits.Length() + " total results\n"); for (int i = 0; i < hits.Length(); i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = hits.Doc(i); out_Renamed.WriteLine(i + " " + d.Get(ID_FIELD)); } } }
public static void Main(System.String[] args) { try { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" }; for (int j = 0; j < docs.Length; j++) { Document d = new Document(); d.Add(Field.Text("contents", docs[j])); writer.AddDocument(d); } writer.Close(); Searcher searcher = new IndexSearcher(directory); System.String[] queries = new System.String[] { "\"a c e\"" }; Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer); parser.SetPhraseSlop(4); for (int j = 0; j < queries.Length; j++) { Query query = parser.Parse(queries[j]); System.Console.Out.WriteLine("Query: " + query.ToString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); hits = searcher.Search(query); System.Console.Out.WriteLine(hits.Length() + " total results"); for (int i = 0; i < hits.Length() && i < 10; i++) { Document d = hits.Doc(i); System.Console.Out.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents")); } } searcher.Close(); } catch (System.Exception e) { System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message); } }
/// <summary> /// Searches the index. /// </summary> /// <param name="indexPath">The index path.</param> /// <param name="searchText">The search text.</param> /// <returns></returns> public List <IndexInformation> SearchIndex(string indexPath, string searchText) { List <IndexInformation> searchResults = new List <IndexInformation>(); try { using (Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(indexPath))) { Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, RegularExpression, analyzer); Query query = parser.Parse(searchText); using (var searcher = new IndexSearcher(directory, true)) { Hits topDocs = searcher.Search(query); if (topDocs != null) { for (int i = 0; i < topDocs.Length(); i++) { searchResults.Add(new IndexInformation { FilePath = topDocs.Doc(i).Get(FilePath), Date = topDocs.Doc(i).Get(Date) }); } } } } } catch (Exception ex) { throw; } return(searchResults); }
public List <string> Search(string text) { List <string> lstFilteredValue = new List <string>(); try { IndexSearcher MyIndexSearcher = new IndexSearcher(LuceneDirectory); Query mainQuery = this.GetParsedQuerywc(text); //Do the search Hits hits = MyIndexSearcher.Search(mainQuery); int results = hits.Length(); for (int i = 0; i < results; i++) { Document doc = hits.Doc(i); float score = hits.Score(i); lstFilteredValue.Add(doc.Get("Name") + "," + doc.Get("Id")); } } catch (Exception GeneralException) { } return(lstFilteredValue); }
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); }
protected void BindLucene() { StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(); IndexSearcher searcher = new IndexSearcher("C:\\Clients\\NWTD5.0.200\\SearchIndex\\eCommerceFramework\\CatalogEntryIndexer"); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.Add(new TermQuery(new Term("_catalog", "NWTD")), BooleanClause.Occur.MUST); Hits hits = searcher.Search(boolQuery, new Sort("TypeSort")); DataTable dt = new DataTable(); dt.Columns.Add("Code"); dt.Columns.Add("Name"); dt.Columns.Add("TypeSort"); for (int hitIndex = 0; hitIndex < hits.Length() && hitIndex < 10; hitIndex++) { Document hitDocument = hits.Doc(hitIndex); object[] param = new[] { hitDocument.GetField("Code").StringValue(), hitDocument.GetField("Name").StringValue(), hitDocument.GetField("TypeSort").StringValue() }; dt.Rows.Add(param); } this.gvLuceneSearchResults.DataSource = dt; this.gvLuceneSearchResults.DataBind(); }
public TokenResult FindExact(TokenParameter parameter) { IndexSearcher indexSearcher = Searcher; string projectIdentifier = string.Format("{0}_{1}", parameter.Username, parameter.Project).ToLower(); var projectQuery = new TermQuery(new Term(ItemProjectIdentifier, projectIdentifier)); var identfierQuery = new TermQuery(new Term(ItemIdentifier, string.Format("{0}", parameter.FullyQualifiedName))); var booleanQuery = new BooleanQuery(); booleanQuery.Add(projectQuery, BooleanClause.Occur.MUST); booleanQuery.Add(identfierQuery, BooleanClause.Occur.MUST); Hits hits = indexSearcher.Search(booleanQuery); if (hits.Length() > 0) { var document = hits.Doc(0); // Take only the first one string filePath = document.Get(ItemPath); int location = Int32.Parse(document.Get(ItemLocation)); var projectCodeDirectory = this.applicationConfigurationProvider.GetProjectSourceCodePath(parameter.Username, parameter.Project); //string relativePath = filePath //projectCodeDirectory.MakeRelativePath(filePath); return(new Models.TokenResult { FileName = Path.GetFileName(filePath), Position = location, Path = filePath }); } return(null); }
/// <summary> /// 根据产品编号搜索产品 /// (从索引) /// </summary> /// <param name="productSysNo">产品编号</param> /// <returns>产品列表</returns> /// <remarks>2013-08-12 黄波 创建</remarks> public PdProductIndex Search(int productSysNo , ProductStatus.产品价格来源 priceSource = ProductStatus.产品价格来源.会员等级价 , int priceSourceSysNo = CustomerLevel.初级) { var returnValue = new PdProductIndex(); BooleanQuery query = new BooleanQuery(); query.Add(new TermQuery(new Term("SysNo", productSysNo.ToString())), BooleanClause.Occur.SHOULD); //搜索 var searchIndex = Hyt.Infrastructure.Lucene.ProductIndex.Searcher; Hits hits = searchIndex.Search(query); for (var i = 0; i < hits.Length(); i++) { try { var pdProductIndex = Hyt.Infrastructure.Lucene.ProductIndex.Instance.DocumentToModel(hits.Doc(i)); pdProductIndex.RankPrice = GetProductRankPrice(pdProductIndex.Prices, pdProductIndex.BasicPrice, priceSource, priceSourceSysNo); returnValue = pdProductIndex; } catch { } } // searchIndex.Close(); return(returnValue); }
void DoTitleMatches() { string term = searchterm; try { string[] terms = Regex.Split(term, " +"); term = ""; foreach (string t in terms) { term += t + "~ "; } searchterm = "title:(" + term + ")"; DateTime now = DateTime.UtcNow; Query query = state.Parse(searchterm); Hits hits = state.Searcher.Search(query); int numhits = hits.Length(); LogRequest(searchterm, query, numhits, now); SendHeaders(200, "OK"); for (int i = 0; i < numhits && i < 10; i++) { Document doc = hits.Doc(i); float score = hits.Score(i); string pageNamespace = doc.Get("namespace"); string title = doc.Get("title"); SendResultLine(score, pageNamespace, title); } } catch (Exception e) { log.Error(e.Message + e.StackTrace); } }
public override List <SearchResult> Search(string searchStr) { List <SearchResult> results = new List <SearchResult>(); string cleanSearchStr = cleaner.Replace(searchStr, "").ToLower().Trim(); IndexSearcher searcher = new IndexSearcher(directory); //QueryParser parser = new QueryParser("title", analyzer); //Query query = parser.Parse(cleanSearchStr + "~0.7"); Query query = parser.Parse(cleanSearchStr + "~0.7"); Hits hits = searcher.Search(query); int resultCount = hits.Length(); for (int i = 0; i < resultCount; i++) { SearchResult result = new SearchResult(); result.Item = DatabaseManager.Get <T>(int.Parse(hits.Doc(i).Get("id"))); result.Score = hits.Score(i); results.Add(result); } return(results); }
public static IDictionary <string, string> Query(string searchTerm) { BuildIndexTask.Wait(); System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]"); searchTerm = rgx.Replace(searchTerm, " "); IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); QueryParser parser = new QueryParser("Guideline", analyzer); Query query = parser.Parse(searchTerm.ToLower()); Hits hitsFound = searcher.Search(query); IDictionary <string, string> results = new Dictionary <string, string>(); for (int i = 0; i < hitsFound.Length(); i++) { Document doc = hitsFound.Doc(i); float score = hitsFound.Score(i); string CodeSnippetName = doc.Get("CodeSnippetName"); string CodeSnippet = doc.Get("CodeSnippet"); if (score > 0.6) { results.Add(CodeSnippetName, CodeSnippet); } } searcher.Close(); return(results); }
public Hit GetHitById(string id) { IndexSearcher s = GetSearcher(); Hits hits = s.Search(new TermQuery(new Term(FieldName.Id, id))); return(hits.Length() == 1 ? new Hit(hits.Doc(0), null) : null); }
public IEnumerable <DataFileRow> Search(string searchTerm) { IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); QueryParser parser = new QueryParser("LineText", analyzer); Query query = parser.Parse(searchTerm); Hits hitsFound = searcher.Search(query); List <DataFileRow> results = new List <DataFileRow>(); DataFileRow sampleDataFileRow = null; for (int i = 0; i < hitsFound.Length(); i++) { sampleDataFileRow = new DataFileRow(); Document doc = hitsFound.Doc(i); sampleDataFileRow.LineNumber = int.Parse(doc.Get("LineNumber")); sampleDataFileRow.LineText = doc.Get("LineText"); float score = hitsFound.Score(i); sampleDataFileRow.Score = score; results.Add(sampleDataFileRow); } luceneIndexDirectory.Close(); searcher.Close(); return(results.OrderByDescending(x => x.Score).ToList()); }
public static IList <string> Search(string searchTerm) { System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]"); searchTerm = rgx.Replace(searchTerm, " "); IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); QueryParser parser = new QueryParser("Data", analyzer); Query query = parser.Parse(searchTerm.ToLower()); Hits hitsFound = searcher.Search(query); IList <string> results = new List <string>(); for (int i = 0; i < hitsFound.Length(); i++) { Document doc = hitsFound.Doc(i); float score = hitsFound.Score(i); string fileName = doc.Get("FileName"); if (score > 0.6) { results.Add(doc.Get("FileName")); } } searcher.Close(); return(results); }
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(); } }
public string DidYouMean(string pattern) { try { IndexSearcher searcher = new IndexSearcher(m_HistoryPath); Term t = new Term(Constants.SearchedText, pattern); FuzzyQuery query = new FuzzyQuery(t); Hits hits = searcher.Search(query); if (hits.Length() != 0) { return(hits.Doc(0).Get(Constants.SearchedText)); } else { return(""); } } catch (Exception) { return(""); } }
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 override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { System.Type type = GetCriteriaClass(criteria); ISearchFactoryImplementor searchFactory = ContextHelper.GetSearchFactory(GetSession(criteria)); Iesi.Collections.Generic.ISet <System.Type> types; IndexSearcher searcher = FullTextSearchHelper.BuildSearcher(searchFactory, out types, type); if (searcher == null) { throw new SearchException("Could not find a searcher for class: " + type.FullName); } Lucene.Net.Search.Query query = FullTextSearchHelper.FilterQueryByClasses(types, luceneQuery); Hits hits = searcher.Search(query); List <object> ids = new List <object>(); for (int i = 0; i < hits.Length(); i++) { object id = DocumentBuilder.GetDocumentId(searchFactory, type, hits.Doc(i)); ids.Add(id); } base.Values = ids.ToArray(); return(base.ToSqlString(criteria, criteriaQuery, enabledFilters)); }
public void TrailingGap() { Hits a = TestIndex.SearchHeadRevision(PathQuery("/general/**")); Hits b = TestIndex.SearchHeadRevision(PathQuery("/general/")); Assert.That(a.Length() == b.Length()); HashSet <string> aa = new HashSet <string>(); HashSet <string> bb = new HashSet <string>(); for (int i = 0; i < a.Length(); ++i) { aa.Add(a.Doc(i).Get(FieldName.Id)); bb.Add(b.Doc(i).Get(FieldName.Id)); } Assert.That(aa.SetEquals(bb), Is.True); }
public static int GetCount(string type, string channelid, string classid, string year, string keywords, string groupname, out Dictionary <string, int> groupAggregate) { if (keywords.Length == 0) { keywords = "jUmBoT"; } DateTime arg_15_0 = DateTime.Now; string[] array = type.Split(new char[] { ',' }); int num = array.Length; IndexSearcher[] array2 = new IndexSearcher[num]; for (int i = 0; i < num; i++) { array2[i] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/data/index/" + array[i] + "/")); } MultiSearcher multiSearcher = new MultiSearcher(array2); BooleanQuery booleanQuery = new BooleanQuery(); if (channelid != "0") { Term t = new Term("channelid", channelid); TermQuery query = new TermQuery(t); booleanQuery.Add(query, BooleanClause.Occur.MUST); } if (Validator.StrToInt(year, 0) > 1900) { Term t2 = new Term("year", year); TermQuery query2 = new TermQuery(t2); booleanQuery.Add(query2, BooleanClause.Occur.MUST); } string[] fields = new string[] { "title", "tags", "summary", "content", "fornull" }; MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, new StandardAnalyzer()); Query query3 = multiFieldQueryParser.Parse(keywords); booleanQuery.Add(query3, BooleanClause.Occur.MUST); Hits hits = multiSearcher.Search(booleanQuery); if (num == 1) { groupAggregate = SimpleFacets.Facet(booleanQuery, array2[0], groupname); } else { groupAggregate = null; } return(hits.Length()); }
private void DoTestSearch(System.IO.StringWriter out_Renamed, bool useCompoundFile) { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); writer.SetUseCompoundFile(useCompoundFile); System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" }; for (int j = 0; j < docs.Length; j++) { Document d = new Document(); d.Add(Field.Text("contents", docs[j])); writer.AddDocument(d); } writer.Close(); Searcher searcher = new IndexSearcher(directory); System.String[] queries = new System.String[] { "a b", "\"a b\"", "\"a b c\"", "a c", "\"a c\"", "\"a c e\"" }; Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer); parser.SetPhraseSlop(4); for (int j = 0; j < queries.Length; j++) { Query query = parser.Parse(queries[j]); out_Renamed.WriteLine("Query: " + query.ToString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); hits = searcher.Search(query); out_Renamed.WriteLine(hits.Length() + " total results"); for (int i = 0; i < hits.Length() && i < 10; i++) { Document d = hits.Doc(i); out_Renamed.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents")); } } searcher.Close(); }