/// <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 Document this [int i] { get { return(hits.Doc(i)); } }
//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); }
/// <summary> /// search /// </summary> /// <param name="indexDir"></param> /// <param name="q">keyword</param> /// <param name="pageLen">every page's length</param> /// <param name="pageNo">page number</param> /// <param name="recCount">result number</param> /// <returns></returns> public List <TDocs> Search(String q, int pageLen, int pageNo, out int recCount) { string keywords = q; IndexSearcher search = new IndexSearcher(INDEX_DIR); q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer()); QueryParser queryParser = new QueryParser("content", new PanGuAnalyzer(true)); Query query = queryParser.Parse(q); Hits hits = search.Search(query, Sort.RELEVANCE); List <TDocs> result = new List <TDocs>(); recCount = hits.Length(); int i = (pageNo - 1) * pageLen; while (i < recCount && result.Count < pageLen) { TDocs docs = null; try { docs = new TDocs(); docs.Path = hits.Doc(i).Get("path"); docs.Name = hits.Doc(i).Get("name"); docs.Title = hits.Doc(i).Get("title"); docs.Extension = hits.Doc(i).Get("ext"); //rem this item in case the search result will be too large & consume too much memory, // takes loading time, abstract is enough //docs.Content = hits.Doc(i).Get("content"); PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<color=red>", "</color>"); PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 100; docs.Abstract = highlighter.GetBestFragment(keywords, hits.Doc(i).Get("content")); } catch (Exception e) { Debug.WriteLine(e.Message); } finally { result.Add(docs); i++; } } search.Close(); return(result); }
/// <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 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); }
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 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 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 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("-"); } }
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 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 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 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 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 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 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 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 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 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); }
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 StoryCollection Find(int hostId, int storyId) { int?docId = ConvertStoryIdtoDocId(hostId, storyId); if (docId.HasValue) { IndexSearcher indexSearch = SearchQuery.GetSearcher(hostId); IndexReader indexReader = indexSearch.GetIndexReader(); MoreLikeThis mlt = new MoreLikeThis(indexReader); mlt.SetAnalyzer(new DnkAnalyzer()); //mlt.SetFieldNames(new string[] { "title", "description" }); //these values control the query used to find related/similar stories // //-we are only using the title and tags fields, //-the term must appear 1 or more times, //-the query will only have 3 terms //-a word less than 3 char in len with be ignored //-the term must appear at in at least 4 doc mlt.SetFieldNames(new string[] { "title", "tags" }); mlt.SetMinTermFreq(1); mlt.SetMaxQueryTerms(5); mlt.SetMinWordLen(3); mlt.SetMinDocFreq(4); mlt.SetStopWords(StopWords()); mlt.SetBoost(true); Query mltQuery = mlt.Like(docId.Value); Hits hits = indexSearch.Search(mltQuery); List <int> results = new List <int>(); for (int i = 0; i < hits.Length(); i++) { Document d = hits.Doc(i); int hitStoryId = int.Parse(d.GetField("id").StringValue()); if (hitStoryId != storyId) { results.Add(hitStoryId); if (results.Count == NUMBER_OF_RELATED_STORIES_TO_RETURN) { break; } } } return(SearchQuery.LoadStorySearchResults(results)); } else { return(null); } }
public override CardDescription[] Read(string dirname, ProgressBar process, ToolStripStatusLabel lbl) { if (dirname == null || dirname.Length <= 0) { return(null); } if (!Directory.Exists(dirname)) { return(null); } if (dirname[dirname.Length - 1] != '\\') { dirname += "\\"; } if (File.Exists(dirname + "list.txt")) { string[] files = File.ReadAllLines(dirname + "list.txt", Encoding.UTF8); foreach (string s in Directory.GetFiles(dirname)) { string ss = s.Substring(s.LastIndexOf('\\') + 1); bool inlist = false; foreach (string s2 in files) { if (string.Equals(ss, s2, StringComparison.OrdinalIgnoreCase)) { inlist = true; break; } } if (!(inlist || string.Equals(ss, "list.txt", StringComparison.OrdinalIgnoreCase))) { File.Delete(dirname + ss); } } } ArrayList cards = new ArrayList(MinCapacity); Query query = new MatchAllDocsQuery(); Searcher searcher = new IndexSearcher(dirname); Hits hits = searcher.Search(query); int length = hits.Length(); for (int i = 0; i < length; i++) { Document doc = hits.Doc(i); cards.Add(ParseCard(doc)); } return((CardDescription[])cards.ToArray(typeof(CardDescription))); }
/// <summary> Perform synonym expansion on a query. /// /// </summary> /// <param name="">query /// </param> /// <param name="">syns /// </param> /// <param name="">a /// </param> /// <param name="">field /// </param> /// <param name="">boost /// </param> public static Query Expand(System.String query, Searcher syns, Analyzer a, System.String field, float boost) { System.Collections.Hashtable already = new System.Collections.Hashtable(); // avoid dups System.Collections.IList top = new System.Collections.ArrayList(); // needs to be separately listed.. // [1] Parse query into separate words so that when we expand we can avoid dups TokenStream ts = a.TokenStream(field, new System.IO.StringReader(query)); Lucene.Net.Analysis.Token t; while ((t = ts.Next()) != null) { System.String word = t.TermText(); if (already.Contains(word) == false) { already.Add(word, word); top.Add(word); } } BooleanQuery tmp = new BooleanQuery(); // [2] form query System.Collections.IEnumerator it = top.GetEnumerator(); while (it.MoveNext()) { // [2a] add to level words in System.String word = (System.String)it.Current; TermQuery tq = new TermQuery(new Term(field, word)); tmp.Add(tq, BooleanClause.Occur.SHOULD); // [2b] add in unique synonums Hits hits = syns.Search(new TermQuery(new Term(Syns2Index.F_WORD, word))); for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); System.String[] values = doc.GetValues(Syns2Index.F_SYN); for (int j = 0; j < values.Length; j++) { System.String syn = values[j]; if (already.Contains(syn) == false) { already.Add(syn, syn); tq = new TermQuery(new Term(field, syn)); if (boost > 0) { // else keep normal 1.0 tq.SetBoost(boost); } tmp.Add(tq, BooleanClause.Occur.SHOULD); } } } } return(tmp); }
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")); } }
public string[] GetFragments(Query query, Hits hits) { var highlighter = CreateHighlighter(query); var result = new string[hits.Length()]; for (var i = 0; i < result.Length; i++) { var size = PackedSizeConverter.FromSortableString(hits.Doc(i).Get(FieldName.Size)); var loc = hits.Doc(i).Get(FieldName.Id).Split('@'); var info = Svn.GetPathInfo(loc[0], Convert.ToInt32(loc[1])); if (info.IsDirectory) { continue; } var text = Svn.GetPathContent(loc[0], Convert.ToInt32(loc[1]), size); var tokens = new SimpleTokenStream(text); result[i] = GetFragments(highlighter, tokens, text); } return(result); }
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)); } } }
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); } } }
///////////////////////////////////////////////////////// static void ExecuteRemove(string arg) { LuceneDriver driver = new LuceneDriver(index_dir); if (arg.IndexOf("://") != -1) { Uri uri = new Uri(arg); ICollection hits = driver.DoQueryByUri(uri); if (hits == null || hits.Count == 0) { Console.WriteLine("Uri not found in the index: {0}", uri); Environment.Exit(1); } driver.Remove(uri); driver.Flush(); Console.WriteLine("Successfully removed Uri: {0}", uri); } else { IndexSearcher searcher = new IndexSearcher(driver.Store); BooleanQuery query = new BooleanQuery(); Term term = new Term("prop:k:Tag", arg); // Argh TermQuery term_query = new TermQuery(term); query.Add(term_query, false, false); Hits hits = searcher.Search(query); int n_hits = hits.Length(); string uri; for (int i = 0; i < n_hits; ++i) { Document doc = hits.Doc(i); uri = doc.Get("Uri"); if (uri == null) { continue; } driver.Remove(UriFu.UriStringToUri(uri)); } driver.Flush(); Console.WriteLine("Successfully removed {0} items with tag: {1}", n_hits, arg); } }