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; }
private void RunQuery(string query, int expectedPosition) { var hqp = new HebrewQueryParser(global::Lucene.Net.Util.Version.LUCENE_29, "Text", analyzer); Query q = hqp.Parse(query); TopDocs td = searcher.Search(q, 10000); int num = td.ScoreDocs[0].Doc; var tf = searcher.IndexReader.GetTermFreqVectors(num)[0]; var tp = (TermPositionVector)tf; var trms_list = new SortedSet<Term>(); q.ExtractTerms(trms_list); foreach (var t in trms_list) { int[] pos = tp.GetTermPositions(tp.IndexOf(t.Text)); TermVectorOffsetInfo[] off = tp.GetOffsets(tp.IndexOf(t.Text)); AssertSinglePositionExists(pos, expectedPosition); /* string sPos = ""; string sOff = ""; foreach (int p in pos) { sPos += " " + p; } foreach (TermVectorOffsetInfo o in off) { sOff += " (" + o.GetStartOffset() + "," + o.GetEndOffset() + ")"; } Trace.WriteLine(string.Format("Term: {0} Pos:{1} Off:{2}", t.Text(), sPos, sOff)); */ } }
/// <summary> Parses a query, searching on the fields specified. Use this if you need /// to specify certain fields as required, and others as prohibited. /// <p/> /// /// <pre> /// Usage: /// <code> /// String[] fields = {"filename", "contents", "description"}; /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD, /// BooleanClause.Occur.MUST, /// BooleanClause.Occur.MUST_NOT}; /// MultiFieldQueryParser.parse("query", fields, flags, analyzer); /// </code> /// </pre> /// <p/> /// The code above would construct a query: /// /// <pre> /// <code> /// (filename:query) +(contents:query) -(description:query) /// </code> /// </pre> /// /// </summary> /// <param name="matchVersion">Lucene version to match; this is passed through to /// QueryParser. /// </param> /// <param name="query">Query string to parse /// </param> /// <param name="fields">Fields to search on /// </param> /// <param name="flags">Flags describing the fields /// </param> /// <param name="analyzer">Analyzer to use /// </param> /// <throws> ParseException </throws> /// <summary> if query parsing fails /// </summary> /// <throws> IllegalArgumentException </throws> /// <summary> if the length of the fields array differs from the length of /// the flags array /// </summary> public new static Query Parse(Lucene.Net.Util.Version matchVersion, string query, string[] fields, Occur[] flags, Analyzer analyzer) { 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); Query q = qp.Parse(query); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0)) { bQuery.Add(q, flags[i]); } } return bQuery; }
/// <summary> Parses a query, searching on the fields specified. Use this if you need /// to specify certain fields as required, and others as prohibited. /// <p/> /// /// <pre> /// Usage: /// <code> /// String[] fields = {"filename", "contents", "description"}; /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD, /// BooleanClause.Occur.MUST, /// BooleanClause.Occur.MUST_NOT}; /// MultiFieldQueryParser.parse("query", fields, flags, analyzer); /// </code> /// </pre> /// <p/> /// The code above would construct a query: /// /// <pre> /// <code> /// (filename:query) +(contents:query) -(description:query) /// </code> /// </pre> /// /// </summary> /// <param name="matchVersion">Lucene version to match; this is passed through to /// QueryParser. /// </param> /// <param name="query">Query string to parse /// </param> /// <param name="fields">Fields to search on /// </param> /// <param name="flags">Flags describing the fields /// </param> /// <param name="analyzer">Analyzer to use /// </param> /// <throws> ParseException </throws> /// <summary> if query parsing fails /// </summary> /// <throws> IllegalArgumentException </throws> /// <summary> if the length of the fields array differs from the length of /// the flags array /// </summary> public new static Query Parse(Lucene.Net.Util.Version matchVersion, string query, string[] fields, Occur[] flags, Analyzer analyzer) { 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); Query q = qp.Parse(query); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0)) { bQuery.Add(q, flags[i]); } } return(bQuery); }
/// <summary> Parses a query, searching on the fields specified. Use this if you need /// to specify certain fields as required, and others as prohibited. /// <p/> /// /// <pre> /// Usage: /// <code> /// String[] query = {"query1", "query2", "query3"}; /// String[] fields = {"filename", "contents", "description"}; /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD, /// BooleanClause.Occur.MUST, /// BooleanClause.Occur.MUST_NOT}; /// MultiFieldQueryParser.parse(query, fields, flags, analyzer); /// </code> /// </pre> /// <p/> /// The code above would construct a query: /// /// <pre> /// <code> /// (filename:query1) +(contents:query2) -(description:query3) /// </code> /// </pre> /// /// </summary> /// <param name="matchVersion">Lucene version to match; this is passed through to /// QueryParser. /// </param> /// <param name="queries">Queries string to parse /// </param> /// <param name="fields">Fields to search on /// </param> /// <param name="flags">Flags describing the fields /// </param> /// <param name="analyzer">Analyzer to use /// </param> /// <throws> ParseException </throws> /// <summary> if query parsing fails /// </summary> /// <throws> IllegalArgumentException </throws> /// <summary> if the length of the queries, fields, and flags array differ /// </summary> public static new Query Parse(Lucene.Net.Util.Version matchVersion, string[] queries, string[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) { if (!(queries.Length == fields.Length && queries.Length == flags.Length)) throw new System.ArgumentException("queries, fields, and flags array have have different length"); BooleanQuery bQuery = new BooleanQuery(); for (int i = 0; i < fields.Length; i++) { QueryParser qp = new HebrewQueryParser(matchVersion, fields[i], analyzer); Query q = qp.Parse(queries[i]); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0)) { bQuery.Add(q, flags[i]); } } return bQuery; }
public static Query ParseIntoDMQ(string query, string[] fields, Analyzer analyzer) { var dmQuery = new DisjunctionMaxQuery(0.0f); foreach (var t in fields) { QueryParser qp = new HebrewQueryParser(Lucene.Net.Util.Version.LUCENE_29, t, analyzer); var q = qp.Parse(query); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0)) dmQuery.Add(q); } return dmQuery; }