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; }
/// <summary> /// /// </summary> /// <param name="matchVersion"></param> /// <param name="query"></param> /// <param name="fields"></param> /// <param name="flags"></param> /// <param name="analyzer"></param> /// <param name="defaultOperator"></param> /// <returns></returns> public static Query Parse(Lucene.Net.Util.Version matchVersion, string query, string[] fields, BooleanClause.Occur[] flags, Analyzer analyzer, Operator defaultOperator) { if (fields.Length > flags.Length) throw new System.ArgumentException("fields.length != flags.length"); BooleanQuery bQuery = new BooleanQuery(); for (int i = 0; i < fields.Length; i++) { QueryParser qp = new HebrewQueryParser(matchVersion, fields[i], analyzer); qp.SetDefaultOperator(defaultOperator); Query q = qp.Parse(query); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery) q).GetClauses().Length > 0)) { bQuery.Add(q, flags[i]); } } return bQuery; }