public virtual void TestAsterisk() { Directory indexStore = GetIndexStore("body", new string[] { "metal", "metals" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); Query query1 = new TermQuery(new Term("body", "metal")); Query query2 = new WildcardQuery(new Term("body", "metal*")); Query query3 = new WildcardQuery(new Term("body", "m*tal")); Query query4 = new WildcardQuery(new Term("body", "m*tal*")); Query query5 = new WildcardQuery(new Term("body", "m*tals")); BooleanQuery query6 = new BooleanQuery(); query6.Add(query5, BooleanClause.Occur.SHOULD); BooleanQuery query7 = new BooleanQuery(); query7.Add(query3, BooleanClause.Occur.SHOULD); query7.Add(query5, BooleanClause.Occur.SHOULD); // Queries do not automatically lower-case search terms: Query query8 = new WildcardQuery(new Term("body", "M*tal*")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 2); AssertMatches(searcher, query5, 1); AssertMatches(searcher, query6, 1); AssertMatches(searcher, query7, 2); AssertMatches(searcher, query8, 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2); reader.Dispose(); indexStore.Dispose(); }
public virtual void TestTermWithoutWildcard() { Directory indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); wq.Boost = 0.1F; Query q = searcher.Rewrite(wq); Assert.IsTrue(q is TermQuery); Assert.AreEqual(q.Boost, wq.Boost, 0); wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); wq.Boost = 0.2F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT); wq.Boost = 0.3F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); wq.Boost = 0.4F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); reader.Dispose(); indexStore.Dispose(); }
public virtual void TestEscapes() { Directory indexStore = GetIndexStore("field", new string[] { "foo*bar", "foo??bar", "fooCDbar", "fooSOMETHINGbar", "foo\\" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); // without escape: matches foo??bar, fooCDbar, foo*bar, and fooSOMETHINGbar WildcardQuery unescaped = new WildcardQuery(new Term("field", "foo*bar")); AssertMatches(searcher, unescaped, 4); // with escape: only matches foo*bar WildcardQuery escaped = new WildcardQuery(new Term("field", "foo\\*bar")); AssertMatches(searcher, escaped, 1); // without escape: matches foo??bar and fooCDbar unescaped = new WildcardQuery(new Term("field", "foo??bar")); AssertMatches(searcher, unescaped, 2); // with escape: matches foo??bar only escaped = new WildcardQuery(new Term("field", "foo\\?\\?bar")); AssertMatches(searcher, escaped, 1); // check escaping at end: lenient parse yields "foo\" WildcardQuery atEnd = new WildcardQuery(new Term("field", "foo\\")); AssertMatches(searcher, atEnd, 1); reader.Dispose(); indexStore.Dispose(); }
/// <summary> /// macro for readability </summary> public static Query Cswcq(Term wild) { WildcardQuery query = new WildcardQuery(wild); query.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); return(query); }
/// <summary> /// Returns a collection of all suggestions that match the given searchTerm. /// A match means search term appears anywhere in the product code or category name. /// /// If the searchTerm is null or empty, or doesn't match any categories, than an empty list is returned. /// The suggestions will be returned in the correct order. /// </summary> /// <param name="searchTerm"></param> /// <returns></returns> public IEnumerable<ISuggestion> FindSuggestionsBySubstring(string searchTerm) { if (string.IsNullOrEmpty(searchTerm)) { return new List<CategorySuggestion>(); } // The product codes and category names have been stored in upper case. So need to convert search term to upper case as well. string searchTermUc = LuceneEscape(searchTerm.ToUpper()); Query query1 = new WildcardQuery(new Term("UcName", "*" + searchTermUc + "*")); // Get the searcher. Access _searcher only once while doing a search. Another request running // LoadProductStore could change this property. By accessing once, you are sure your searcher stays the same. var searcher = _searcher; // Actual search TopDocs hits = searcher.Search(query1, searcher.MaxDoc); // Return results. ScoreDocs is sorted by relevancy, but we want alphabetic sorting, and category suggestions first. IEnumerable<ISuggestion> sortedResults = hits.ScoreDocs .Select(d => { var doc = searcher.Doc(d.Doc); return DocToSuggestion(doc); }) .OrderBy(s=>(s is CategorySuggestion ? 0 : 1)) .ThenBy(s=>s.SortedName); return sortedResults; }
/// <summary> /// Adds the query. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="query">The query.</param> /// <param name="filter">The filter.</param> protected void AddQuery(string fieldName, BooleanQuery query, StringCollection filter) { fieldName = fieldName.ToLower(); if (filter.Count > 0) { if (filter.Count != 1) { var booleanQuery = new BooleanQuery(); var containsFilter = false; foreach (var index in filter) { if (String.IsNullOrEmpty(index)) { continue; } var nodeQuery = new WildcardQuery(new Term(fieldName, index)); booleanQuery.Add(nodeQuery, Occur.SHOULD); containsFilter = true; } if (containsFilter) { query.Add(booleanQuery, Occur.MUST); } } else { if (!String.IsNullOrEmpty(filter[0])) { this.AddQuery(fieldName, query, filter[0].ToLower()); } } } }
public void TestPrefixTerm() { RAMDirectory indexStore = GetIndexStore("field", new String[] { "prefix", "prefixx" }); IndexSearcher searcher = new IndexSearcher(indexStore, true); MultiTermQuery wq = new WildcardQuery(new Term("field", "prefix*")); AssertMatches(searcher, wq, 2); MultiTermQuery expected = new PrefixQuery(new Term("field", "prefix")); wq.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.1F; expected.RewriteMethod = wq.RewriteMethod; expected.Boost = wq.Boost; Assert.AreEqual(searcher.Rewrite(expected), searcher.Rewrite(wq)); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; wq.Boost = 0.2F; expected.RewriteMethod = wq.RewriteMethod; expected.Boost = wq.Boost; Assert.AreEqual(searcher.Rewrite(expected), searcher.Rewrite(wq)); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; wq.Boost = 0.3F; expected.RewriteMethod = wq.RewriteMethod; expected.Boost = wq.Boost; Assert.AreEqual(searcher.Rewrite(expected), searcher.Rewrite(wq)); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.4F; expected.RewriteMethod = wq.RewriteMethod; expected.Boost = wq.Boost; Assert.AreEqual(searcher.Rewrite(expected), searcher.Rewrite(wq)); }
public virtual void TestTermWithoutWildcard() { RAMDirectory indexStore = GetIndexStore("field", new System.String[] { "nowildcard", "nowildcardx" }); IndexSearcher searcher = new IndexSearcher(indexStore, true); MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.1f; Query q = searcher.Rewrite(wq); Assert.IsTrue(q is TermQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; wq.Boost = 0.2f; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; wq.Boost = 0.3F; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.4F; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); }
public virtual void TestEquals() { AutomatonQuery a1 = new AutomatonQuery(NewTerm("foobar"), BasicAutomata.MakeString("foobar")); // reference to a1 AutomatonQuery a2 = a1; // same as a1 (accepts the same language, same term) AutomatonQuery a3 = new AutomatonQuery(NewTerm("foobar"), BasicOperations.Concatenate(BasicAutomata.MakeString("foo"), BasicAutomata.MakeString("bar"))); // different than a1 (same term, but different language) AutomatonQuery a4 = new AutomatonQuery(NewTerm("foobar"), BasicAutomata.MakeString("different")); // different than a1 (different term, same language) AutomatonQuery a5 = new AutomatonQuery(NewTerm("blah"), BasicAutomata.MakeString("foobar")); Assert.AreEqual(a1.GetHashCode(), a2.GetHashCode()); Assert.AreEqual(a1, a2); Assert.AreEqual(a1.GetHashCode(), a3.GetHashCode()); Assert.AreEqual(a1, a3); // different class AutomatonQuery w1 = new WildcardQuery(NewTerm("foobar")); // different class AutomatonQuery w2 = new RegexpQuery(NewTerm("foobar")); Assert.IsFalse(a1.Equals(w1)); Assert.IsFalse(a1.Equals(w2)); Assert.IsFalse(w1.Equals(w2)); Assert.IsFalse(a1.Equals(a4)); Assert.IsFalse(a1.Equals(a5)); Assert.IsFalse(a1.Equals(null)); }
public virtual void TestTermWithoutWildcard() { RAMDirectory indexStore = GetIndexStore("field", new System.String[] {"nowildcard", "nowildcardx"}); IndexSearcher searcher = new IndexSearcher(indexStore, true); MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.1f; Query q = searcher.Rewrite(wq); Assert.IsTrue(q is TermQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; wq.Boost = 0.2f; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; wq.Boost = 0.3F; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); wq.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.4F; q = searcher.Rewrite(wq); Assert.True(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost); }
//@Override public override bool Equals(System.Object obj) { if (this == obj) { return(true); } if (!base.Equals(obj)) { return(false); } if (GetType() != obj.GetType()) { return(false); } WildcardQuery other = (WildcardQuery)obj; if (term == null) { if (other.term != null) { return(false); } } else if (!term.Equals(other.term)) { return(false); } return(true); }
public virtual void TestAsterisk() { RAMDirectory indexStore = GetIndexStore("body", new System.String[]{"metal", "metals"}); IndexSearcher searcher = new IndexSearcher(indexStore); Query query1 = new TermQuery(new Term("body", "metal")); Query query2 = new WildcardQuery(new Term("body", "metal*")); Query query3 = new WildcardQuery(new Term("body", "m*tal")); Query query4 = new WildcardQuery(new Term("body", "m*tal*")); Query query5 = new WildcardQuery(new Term("body", "m*tals")); BooleanQuery query6 = new BooleanQuery(); query6.Add(query5, false, false); BooleanQuery query7 = new BooleanQuery(); query7.Add(query3, false, false); query7.Add(query5, false, false); // Queries do not automatically lower-case search terms: Query query8 = new WildcardQuery(new Term("body", "M*tal*")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 2); AssertMatches(searcher, query5, 1); AssertMatches(searcher, query6, 1); AssertMatches(searcher, query7, 2); AssertMatches(searcher, query8, 0); }
public virtual void TestAsterisk() { RAMDirectory indexStore = GetIndexStore("body", new System.String[] { "metal", "metals" }); IndexSearcher searcher = new IndexSearcher(indexStore); Query query1 = new TermQuery(new Term("body", "metal")); Query query2 = new WildcardQuery(new Term("body", "metal*")); Query query3 = new WildcardQuery(new Term("body", "m*tal")); Query query4 = new WildcardQuery(new Term("body", "m*tal*")); Query query5 = new WildcardQuery(new Term("body", "m*tals")); BooleanQuery query6 = new BooleanQuery(); query6.Add(query5, BooleanClause.Occur.SHOULD); BooleanQuery query7 = new BooleanQuery(); query7.Add(query3, BooleanClause.Occur.SHOULD); query7.Add(query5, BooleanClause.Occur.SHOULD); // Queries do not automatically lower-case search terms: Query query8 = new WildcardQuery(new Term("body", "M*tal*")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 2); AssertMatches(searcher, query5, 1); AssertMatches(searcher, query6, 1); AssertMatches(searcher, query7, 2); AssertMatches(searcher, query8, 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2); }
public override Query VisitWildcardQuery(WildcardQuery wildcardq) { _dump.Append("WildcardQ("); var q = base.VisitWildcardQuery(wildcardq); _dump.Append(BoostToString(q)); _dump.Append(")"); return q; }
protected override ParameterizedSql BuildQuery(WildcardQuery wildcardQuery) { var termText = EscapeForSql(wildcardQuery.Term.Text); termText = termText.Replace("*", "%"); termText = termText.Replace("?", "_"); var userVariables = new Dictionary<string, string> {{"field1", termText}}; var sql = FieldPlaceholder + " LIKE '%' + @field1 + '%'"; return new ParameterizedSql(sql, userVariables); }
protected override ParameterizedSql BuildQuery(WildcardQuery wildcardQuery) { var termText = EscapeForSql(wildcardQuery.Term.Text); // termText = termText.Replace("*", "%"); termText = termText.Replace("?", "_"); var userVariables = new Dictionary<string, string> {{"field1", "\"" + termText + "\""}}; var sql = "CONTAINS(" + FieldPlaceholder + ", @field1)"; return new ParameterizedSql(sql, userVariables); }
public virtual void TestTermWithoutWildcard() { RAMDirectory indexStore = GetIndexStore("field", new System.String[]{"nowildcard", "nowildcardx"}); IndexSearcher searcher = new IndexSearcher(indexStore); Query wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq = searcher.Rewrite(wq); Assert.IsTrue(wq is TermQuery); }
public virtual void TestTermWithoutWildcard() { RAMDirectory indexStore = GetIndexStore("field", new System.String[] { "nowildcard", "nowildcardx" }); IndexSearcher searcher = new IndexSearcher(indexStore); Query wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq = searcher.Rewrite(wq); Assert.IsTrue(wq is TermQuery); }
public virtual void TestDeMorgan() { Directory dir1 = NewDirectory(); RandomIndexWriter iw1 = new RandomIndexWriter(Random(), dir1, Similarity, TimeZone); Document doc1 = new Document(); doc1.Add(NewTextField("field", "foo bar", Field.Store.NO)); iw1.AddDocument(doc1); IndexReader reader1 = iw1.Reader; iw1.Dispose(); Directory dir2 = NewDirectory(); RandomIndexWriter iw2 = new RandomIndexWriter(Random(), dir2, Similarity, TimeZone); Document doc2 = new Document(); doc2.Add(NewTextField("field", "foo baz", Field.Store.NO)); iw2.AddDocument(doc2); IndexReader reader2 = iw2.Reader; iw2.Dispose(); BooleanQuery query = new BooleanQuery(); // Query: +foo -ba* query.Add(new TermQuery(new Term("field", "foo")), Occur.MUST); WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*")); wildcardQuery.MultiTermRewriteMethod = (MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); query.Add(wildcardQuery, Occur.MUST_NOT); MultiReader multireader = new MultiReader(reader1, reader2); IndexSearcher searcher = NewSearcher(multireader); Assert.AreEqual(0, searcher.Search(query, 10).TotalHits); Task foo = new Task(TestDeMorgan); TaskScheduler es = TaskScheduler.Default; searcher = new IndexSearcher(multireader, es); if (VERBOSE) { Console.WriteLine("rewritten form: " + searcher.Rewrite(query)); } Assert.AreEqual(0, searcher.Search(query, 10).TotalHits); multireader.Dispose(); reader1.Dispose(); reader2.Dispose(); dir1.Dispose(); dir2.Dispose(); }
protected static BooleanQuery GetTextQuery(SearchUserInput searchUserInput, params string[] fieldNames) { var masterQuery = new BooleanQuery(); foreach (string queryTerm in searchUserInput.Terms) { foreach (string fieldName in fieldNames) { var query = new WildcardQuery(new Term(fieldName, queryTerm)); masterQuery.Add(query, searchUserInput.BooleanCombinationLogic); } } return masterQuery; }
public void TestEmptyTerm() { RAMDirectory indexStore = GetIndexStore("field", new String[] { "nowildcard", "nowildcardx" }); IndexSearcher searcher = new IndexSearcher(indexStore, true); MultiTermQuery wq = new WildcardQuery(new Term("field", "")); wq.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; AssertMatches(searcher, wq, 0); BooleanQuery expected = new BooleanQuery(true); Assert.AreEqual(searcher.Rewrite(expected), searcher.Rewrite(wq)); }
private void AssertPatternHits(string pattern, int numHits) { // TODO: run with different rewrites string filledPattern = FillPattern(pattern); if (VERBOSE) { Console.WriteLine("TEST: run wildcard pattern=" + pattern + " filled=" + filledPattern); } Query wq = new WildcardQuery(new Term("field", filledPattern)); TopDocs docs = Searcher.Search(wq, 25); Assert.AreEqual(numHits, docs.TotalHits, "Incorrect hits for pattern: " + pattern); }
protected static BooleanQuery GetPlaceNameQuery(SearchUserInput searchUserInput, int parentAreaTypeId) { var masterQuery = new BooleanQuery(); foreach (string queryTerm in searchUserInput.Terms) { var query = new WildcardQuery(new Term(FieldNames.PlaceName, queryTerm)); masterQuery.Add(query, searchUserInput.BooleanCombinationLogic); } var parentAreaTypeIdQuery = new WildcardQuery(new Term("Parent_Area_Code_" + parentAreaTypeId, "x")); masterQuery.Add(parentAreaTypeIdQuery, BooleanClause.Occur.MUST_NOT); return masterQuery; }
public virtual Query Build(IQueryNode queryNode) { WildcardQueryNode wildcardNode = (WildcardQueryNode)queryNode; WildcardQuery q = new WildcardQuery(new Term(wildcardNode.GetFieldAsString(), wildcardNode.GetTextAsString())); MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod)queryNode.GetTag(MultiTermRewriteMethodProcessor.TAG_ID); if (method != null) { q.SetRewriteMethod(method); } return q; }
// Random rnd is passed in so that the exact same random query may be created // more than once. public static BooleanQuery RandBoolQuery(System.Random rnd, bool allowMust, int level, System.String field, System.String[] vals, TestBoolean2.Callback cb) { BooleanQuery current = new BooleanQuery(rnd.Next() < 0); for (int i = 0; i < rnd.Next(vals.Length) + 1; i++) { int qType = 0; // term query if (level > 0) { qType = rnd.Next(10); } Query q; if (qType < 3) { q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)])); } else if (qType < 7) { q = new WildcardQuery(new Term(field, "w*")); } else { q = RandBoolQuery(rnd, allowMust, level - 1, field, vals, cb); } int r = rnd.Next(10); Occur occur; if (r < 2) { occur = Occur.MUST_NOT; } else if (r < 5) { occur = allowMust ? Occur.MUST : Occur.SHOULD; } else { occur = Occur.SHOULD; } current.Add(q, occur); } if (cb != null) { cb.PostCreate(current); } return(current); }
public virtual void TestQuestionmark() { RAMDirectory indexStore = GetIndexStore("body", new System.String[]{"metal", "metals", "mXtals", "mXtXls"}); IndexSearcher searcher = new IndexSearcher(indexStore); Query query1 = new WildcardQuery(new Term("body", "m?tal")); Query query2 = new WildcardQuery(new Term("body", "metal?")); Query query3 = new WildcardQuery(new Term("body", "metals?")); Query query4 = new WildcardQuery(new Term("body", "m?t?ls")); Query query5 = new WildcardQuery(new Term("body", "M?t?ls")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 3); AssertMatches(searcher, query5, 0); }
public virtual void TestQuestionmark() { RAMDirectory indexStore = GetIndexStore("body", new System.String[] { "metal", "metals", "mXtals", "mXtXls" }); IndexSearcher searcher = new IndexSearcher(indexStore); Query query1 = new WildcardQuery(new Term("body", "m?tal")); Query query2 = new WildcardQuery(new Term("body", "metal?")); Query query3 = new WildcardQuery(new Term("body", "metals?")); Query query4 = new WildcardQuery(new Term("body", "m?t?ls")); Query query5 = new WildcardQuery(new Term("body", "M?t?ls")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 3); AssertMatches(searcher, query5, 0); }
public virtual void TestEmptyTerm() { Directory indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "")); wq.MultiTermRewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; AssertMatches(searcher, wq, 0); Query q = searcher.Rewrite(wq); Assert.IsTrue(q is BooleanQuery); Assert.AreEqual(0, ((BooleanQuery)q).Clauses.Count); reader.Dispose(); indexStore.Dispose(); }
public void TestLotsOfAsterisks() { RAMDirectory indexStore = GetIndexStore("body", new String[] { "metal", "metals" }); IndexSearcher searcher = new IndexSearcher(indexStore, true); System.Text.StringBuilder term = new System.Text.StringBuilder(); term.Append("m"); for (int i = 0; i < 512; i++) { term.Append("*"); } term.Append("tal"); Query query3 = new WildcardQuery(new Term("body", term.ToString())); AssertMatches(searcher, query3, 1); searcher.Close(); indexStore.Close(); }
public ActionResult GetHint(string term) { if (!string.IsNullOrEmpty(term)) { var seacher = new Seacher(dataFolder); WildcardQuery nameQuery = new WildcardQuery(new Term("Name", "*" + term + "*")); var Result = seacher.Search(nameQuery, 5); Result.SearchResultItems.OrderBy(x => x.Type); if (Result.SearchResultItems.Count > 0) { Result.SearchResultItems.OrderBy(x => x.Type); return Json(Result.SearchResultItems); } } return new JsonResult { Data = new { OK = false } }; }
public virtual void TestEquals() { WildcardQuery wq1 = new WildcardQuery(new Term("field", "b*a")); WildcardQuery wq2 = new WildcardQuery(new Term("field", "b*a")); WildcardQuery wq3 = new WildcardQuery(new Term("field", "b*a")); // reflexive? Assert.AreEqual(wq1, wq2); Assert.AreEqual(wq2, wq1); // transitive? Assert.AreEqual(wq2, wq3); Assert.AreEqual(wq1, wq3); Assert.IsFalse(wq1.Equals(null)); FuzzyQuery fq = new FuzzyQuery(new Term("field", "b*a")); Assert.IsFalse(wq1.Equals(fq)); Assert.IsFalse(fq.Equals(wq1)); }
public virtual void TestPrefixTerm() { Directory indexStore = GetIndexStore("field", new string[] { "prefix", "prefixx" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "prefix*")); AssertMatches(searcher, wq, 2); Terms terms = MultiFields.GetTerms(searcher.IndexReader, "field"); Assert.IsTrue(wq.GetTermsEnum(terms) is PrefixTermsEnum); wq = new WildcardQuery(new Term("field", "*")); AssertMatches(searcher, wq, 2); Assert.IsFalse(wq.GetTermsEnum(terms) is PrefixTermsEnum); Assert.IsFalse(wq.GetTermsEnum(terms).GetType().Name.Contains("AutomatonTermsEnum")); reader.Dispose(); indexStore.Dispose(); }
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[] { "productname", "keywords", "description" }, analyzer); Query queryName = parserName.Parse(searchInfo.QueryString); Query queryCategory = new WildcardQuery(new Term("catepath", "*" + searchInfo.Category + "*")); BooleanQuery bQuery = new BooleanQuery(); bQuery.Add(queryName, BooleanClause.Occur.MUST); if (searchInfo.Category != 0) bQuery.Add(queryCategory, BooleanClause.Occur.MUST); Hits hits = searcher.Search(bQuery, GetSort()); List<ISearchEntity> ResultList = new List<ISearchEntity>(); for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); ResultList.Add((ISearchEntity)new ProductModel() { EntityIdentity = Convert.ToInt32(doc.Get("productid")), ProductName = doc.Get("productname"), CategoryID = Convert.ToInt32(doc.Get("cateid")), CategoryPath = doc.Get("catepath"), Keywords = doc.Get("keywords"), Description = doc.Get("description"), Price = Convert.ToDecimal(doc.Get("price")), CreateTime = Convert.ToDateTime(doc.Get("createtime")), UpdateTime = Convert.ToDateTime(doc.Get("updatetime")), ProductImage = Convert.ToString(doc.Get("mainimage")) }); } searcher.Close(); MatchCount = hits.Length(); return ResultList; }
public virtual void TestQuestionmark() { Directory indexStore = GetIndexStore("body", new string[] { "metal", "metals", "mXtals", "mXtXls" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); Query query1 = new WildcardQuery(new Term("body", "m?tal")); Query query2 = new WildcardQuery(new Term("body", "metal?")); Query query3 = new WildcardQuery(new Term("body", "metals?")); Query query4 = new WildcardQuery(new Term("body", "m?t?ls")); Query query5 = new WildcardQuery(new Term("body", "M?t?ls")); Query query6 = new WildcardQuery(new Term("body", "meta??")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 1); AssertMatches(searcher, query3, 0); AssertMatches(searcher, query4, 3); AssertMatches(searcher, query5, 0); AssertMatches(searcher, query6, 1); // Query: 'meta??' matches 'metals' not 'metal' reader.Dispose(); indexStore.Dispose(); }
public virtual void DeleteAll() { var directory = FSDirectory.Open(new DirectoryInfo(LucenePath), new NativeFSLockFactory()); //IndexReader:对索引库进行读取的类 var isExist = IndexReader.IndexExists(directory);//是否存在索引库文件夹以及索引库特征文件 if (isExist) { //如果索引目录被锁定(比如索引过程中程序异常退出或另一进程在操作索引库),则解锁 if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } //创建向索引库写操作对象 IndexWriter(索引目录,指定使用盘古分词进行切词,最大写入长度限制) //补充:使用IndexWriter打开directory时会自动对索引库文件上锁 var writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED); Query query = new WildcardQuery(new Term("title", "*")); writer.DeleteDocuments(query); writer.Close();//会自动解锁 directory.Close(); //不要忘了Close,否则索引结果搜不到 }
public void TestWildcards() { using (var directory = new RAMDirectory()) { IndexSingleFieldDocs(directory, new[] { new Field("contents", "wild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "child", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mildew", Field.Store.YES, Field.Index.ANALYZED) }); using (var indexSearcher = new IndexSearcher(directory)) { var query = new WildcardQuery(new Term("contents", "?ild*")); var topDocs = indexSearcher.Search(query, 10); Assert.Equal(3, topDocs.TotalHits); // "child no match" Assert.Equal(topDocs.ScoreDocs[0].Score, topDocs.ScoreDocs[1].Score, 1); // "score the same" Assert.Equal(topDocs.ScoreDocs[1].Score, topDocs.ScoreDocs[2].Score, 1); // "score the same" } } }
public virtual void TestTermWithoutWildcard() { Directory indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard")); AssertMatches(searcher, wq, 1); wq.MultiTermRewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.1F; Query q = searcher.Rewrite(wq); Assert.IsTrue(q is TermQuery); Assert.AreEqual(q.Boost, wq.Boost, 0); wq.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; wq.Boost = 0.2F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); wq.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; wq.Boost = 0.3F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); wq.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE; wq.Boost = 0.4F; q = searcher.Rewrite(wq); Assert.IsTrue(q is ConstantScoreQuery); Assert.AreEqual(q.Boost, wq.Boost, 0.1); reader.Dispose(); indexStore.Dispose(); }
public virtual void TestAsterisk() { Directory indexStore = GetIndexStore("body", new string[] { "metal", "metals" }); IndexReader reader = DirectoryReader.Open(indexStore); IndexSearcher searcher = NewSearcher(reader); Query query1 = new TermQuery(new Term("body", "metal")); Query query2 = new WildcardQuery(new Term("body", "metal*")); Query query3 = new WildcardQuery(new Term("body", "m*tal")); Query query4 = new WildcardQuery(new Term("body", "m*tal*")); Query query5 = new WildcardQuery(new Term("body", "m*tals")); BooleanQuery query6 = new BooleanQuery(); query6.Add(query5, Occur.SHOULD); BooleanQuery query7 = new BooleanQuery(); query7.Add(query3, Occur.SHOULD); query7.Add(query5, Occur.SHOULD); // Queries do not automatically lower-case search terms: Query query8 = new WildcardQuery(new Term("body", "M*tal*")); AssertMatches(searcher, query1, 1); AssertMatches(searcher, query2, 2); AssertMatches(searcher, query3, 1); AssertMatches(searcher, query4, 2); AssertMatches(searcher, query5, 1); AssertMatches(searcher, query6, 1); AssertMatches(searcher, query7, 2); AssertMatches(searcher, query8, 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1); AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2); reader.Dispose(); indexStore.Dispose(); }
static void Main(string[] args) { SearchSection Config = (SearchSection)ConfigurationManager.GetSection("searches"); string action = Console.ReadLine(); if (action != String.Empty) { Analyzer analyzer = new StandardAnalyzer(); IndexSearcher searcher = new IndexSearcher(Config.Searches["product"].IndexDirectory); MultiFieldQueryParser parserName = new MultiFieldQueryParser(new string[] { "productname", "keywords", "description" }, analyzer); Query queryName = parserName.Parse("三星"); Query queryCategory = new WildcardQuery(new Term("catepath", "*82*")); //Query queryCategory = parserCategory.Parse("82"); BooleanQuery bQuery = new BooleanQuery(); bQuery.Add(queryName, BooleanClause.Occur.MUST); bQuery.Add(queryCategory, BooleanClause.Occur.MUST); Hits hits = searcher.Search(bQuery); for (int i = 0; i < hits.Length(); i++) { Document d = hits.Doc(i); Console.WriteLine(d.Get("productname")); } } else { DataProcessor.Process("product"); Console.Read(); } }
// Random rnd is passed in so that the exact same random query may be created // more than once. public static BooleanQuery RandBoolQuery(Random rnd, bool allowMust, int level, string field, string[] vals, Callback cb) { BooleanQuery current = new BooleanQuery(rnd.Next() < 0); for (int i = 0; i < rnd.Next(vals.Length) + 1; i++) { int qType = 0; // term query if (level > 0) { qType = rnd.Next(10); } Query q; if (qType < 3) { q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)])); } else if (qType < 4) { Term t1 = new Term(field, vals[rnd.Next(vals.Length)]); Term t2 = new Term(field, vals[rnd.Next(vals.Length)]); PhraseQuery pq = new PhraseQuery(); pq.Add(t1); pq.Add(t2); pq.Slop = 10; // increase possibility of matching q = pq; } else if (qType < 7) { q = new WildcardQuery(new Term(field, "w*")); } else { q = RandBoolQuery(rnd, allowMust, level - 1, field, vals, cb); } int r = rnd.Next(10); Occur occur; if (r < 2) { occur = Occur.MUST_NOT; } else if (r < 5) { if (allowMust) { occur = Occur.MUST; } else { occur = Occur.SHOULD; } } else { occur = Occur.SHOULD; } current.Add(q, occur); } if (cb != null) { cb.PostCreate(current); } return(current); }
public virtual void TestParsingAndSearching() { string field = "content"; string[] docs = new string[] { "\\ abcdefg1", "\\x00079 hijklmn1", "\\\\ opqrstu1" }; // queries that should find all docs Query[] matchAll = new Query[] { new WildcardQuery(new Term(field, "*")), new WildcardQuery(new Term(field, "*1")), new WildcardQuery(new Term(field, "**1")), new WildcardQuery(new Term(field, "*?")), new WildcardQuery(new Term(field, "*?1")), new WildcardQuery(new Term(field, "?*1")), new WildcardQuery(new Term(field, "**")), new WildcardQuery(new Term(field, "***")), new WildcardQuery(new Term(field, "\\\\*")) }; // queries that should find no docs Query[] matchNone = new Query[] { new WildcardQuery(new Term(field, "a*h")), new WildcardQuery(new Term(field, "a?h")), new WildcardQuery(new Term(field, "*a*h")), new WildcardQuery(new Term(field, "?a")), new WildcardQuery(new Term(field, "a?")) }; PrefixQuery[][] matchOneDocPrefix = new PrefixQuery[][] { new PrefixQuery[] { new PrefixQuery(new Term(field, "a")), new PrefixQuery(new Term(field, "ab")), new PrefixQuery(new Term(field, "abc")) }, new PrefixQuery[] { new PrefixQuery(new Term(field, "h")), new PrefixQuery(new Term(field, "hi")), new PrefixQuery(new Term(field, "hij")), new PrefixQuery(new Term(field, "\\x0007")) }, new PrefixQuery[] { new PrefixQuery(new Term(field, "o")), new PrefixQuery(new Term(field, "op")), new PrefixQuery(new Term(field, "opq")), new PrefixQuery(new Term(field, "\\\\")) } }; WildcardQuery[][] matchOneDocWild = new WildcardQuery[][] { new WildcardQuery[] { new WildcardQuery(new Term(field, "*a*")), new WildcardQuery(new Term(field, "*ab*")), new WildcardQuery(new Term(field, "*abc**")), new WildcardQuery(new Term(field, "ab*e*")), new WildcardQuery(new Term(field, "*g?")), new WildcardQuery(new Term(field, "*f?1")) }, new WildcardQuery[] { new WildcardQuery(new Term(field, "*h*")), new WildcardQuery(new Term(field, "*hi*")), new WildcardQuery(new Term(field, "*hij**")), new WildcardQuery(new Term(field, "hi*k*")), new WildcardQuery(new Term(field, "*n?")), new WildcardQuery(new Term(field, "*m?1")), new WildcardQuery(new Term(field, "hij**")) }, new WildcardQuery[] { new WildcardQuery(new Term(field, "*o*")), new WildcardQuery(new Term(field, "*op*")), new WildcardQuery(new Term(field, "*opq**")), new WildcardQuery(new Term(field, "op*q*")), new WildcardQuery(new Term(field, "*u?")), new WildcardQuery(new Term(field, "*t?1")), new WildcardQuery(new Term(field, "opq**")) } }; // prepare the index Directory dir = NewDirectory(); RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMergePolicy(NewLogMergePolicy())); for (int i = 0; i < docs.Length; i++) { Document doc = new Document(); doc.Add(NewTextField(field, docs[i], Field.Store.NO)); iw.AddDocument(doc); } iw.Dispose(); IndexReader reader = DirectoryReader.Open(dir); IndexSearcher searcher = NewSearcher(reader); // test queries that must find all foreach (Query q in matchAll) { if (VERBOSE) { Console.WriteLine("matchAll: q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(docs.Length, hits.Length); } // test queries that must find none foreach (Query q in matchNone) { if (VERBOSE) { Console.WriteLine("matchNone: q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(0, hits.Length); } // thest the prefi queries find only one doc for (int i = 0; i < matchOneDocPrefix.Length; i++) { for (int j = 0; j < matchOneDocPrefix[i].Length; j++) { Query q = matchOneDocPrefix[i][j]; if (VERBOSE) { Console.WriteLine("match 1 prefix: doc=" + docs[i] + " q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].Doc); } } // test the wildcard queries find only one doc for (int i = 0; i < matchOneDocWild.Length; i++) { for (int j = 0; j < matchOneDocWild[i].Length; j++) { Query q = matchOneDocWild[i][j]; if (VERBOSE) { Console.WriteLine("match 1 wild: doc=" + docs[i] + " q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].Doc); } } reader.Dispose(); dir.Dispose(); }
private static Query ParseQuery(string searchTerm) { var fields = new Dictionary<string, float> { { "Id", 1.2f }, { "Title", 1.0f }, { "Tags", 0.8f }, { "Description", 0.1f }, { "Author", 1.0f } }; var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion); var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields.Keys.ToArray(), analyzer, fields); var conjuctionQuery = new BooleanQuery(); conjuctionQuery.SetBoost(2.0f); var disjunctionQuery = new BooleanQuery(); disjunctionQuery.SetBoost(0.1f); var wildCardQuery = new BooleanQuery(); wildCardQuery.SetBoost(0.5f); // Escape the entire term we use for exact searches. var escapedSearchTerm = Escape(searchTerm); var exactIdQuery = new TermQuery(new Term("Id-Exact", escapedSearchTerm)); exactIdQuery.SetBoost(2.5f); var wildCardIdQuery = new WildcardQuery(new Term("Id-Exact", "*" + escapedSearchTerm + "*")); foreach(var term in GetSearchTerms(searchTerm)) { var termQuery = queryParser.Parse(term); conjuctionQuery.Add(termQuery, BooleanClause.Occur.MUST); disjunctionQuery.Add(termQuery, BooleanClause.Occur.SHOULD); foreach (var field in fields) { var wildCardTermQuery = new WildcardQuery(new Term(field.Key, term + "*")); wildCardTermQuery.SetBoost(0.7f * field.Value); wildCardQuery.Add(wildCardTermQuery, BooleanClause.Occur.SHOULD); } } var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); return new CustomScoreQuery(conjuctionQuery.Combine(new Query[] { exactIdQuery, wildCardIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery }), downloadCountBooster); }
// search_subset_uris is a list of Uris that this search should be // limited to. static protected void QueryPartToQuery (QueryPart abstract_part, bool only_build_primary_query, ArrayList term_list, QueryPartHook query_part_hook, out LNS.Query primary_query, out LNS.Query secondary_query, out HitFilter hit_filter) { primary_query = null; secondary_query = null; // By default, we assume that our lucene queries will return exactly the // matching set of objects. We need to set the hit filter if further // refinement of the search results is required. (As in the case of // date range queries, for example.) We essentially have to do this // to make OR queries work correctly. hit_filter = true_hit_filter; // The exception is when dealing with a prohibited part. Just return // null for the hit filter in that case. This works since // prohibited parts are not allowed inside of OR queries. if (abstract_part.Logic == QueryPartLogic.Prohibited) hit_filter = null; if (abstract_part == null) return; // Run the backend hook first. // This gives a chance to modify create new queries based on // backend specific properties if (query_part_hook != null) abstract_part = query_part_hook (abstract_part); if (abstract_part == null) return; if (abstract_part is QueryPart_Text) { QueryPart_Text part = (QueryPart_Text) abstract_part; if (! (part.SearchFullText || part.SearchTextProperties)) return; LNS.BooleanQuery p_query = new LNS.BooleanQuery (); LNS.BooleanQuery s_query = new LNS.BooleanQuery (); bool added_subquery = false; if (part.SearchFullText) { LNS.Query subquery; subquery = StringToQuery ("Text", part.Text, term_list); if (subquery != null) { p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); added_subquery = true; } // FIXME: HotText is ignored for now! // subquery = StringToQuery ("HotText", part.Text); // if (subquery != null) { // p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); // added_subquery = true; // } } if (part.SearchTextProperties) { LNS.Query subquery; subquery = StringToQuery ("PropertyText", part.Text, term_list); if (subquery != null) { p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); // Properties can live in either index if (! only_build_primary_query) s_query.Add (subquery.Clone () as LNS.Query, LNS.BooleanClause.Occur.SHOULD); added_subquery = true; } // The "added_subquery" check is to handle the situation where // a part of the text is a stop word. Normally, a search for // "hello world" would break down into this query: // // (Text:hello OR PropertyText:hello OR PropertyKeyword:hello) // AND (Text:world OR PropertText:world OR PropertyKeyword:world) // // This fails with stop words, though. Let's assume that "world" // is a stop word. You would end up with: // // (Text:hello OR PropertyText:hello OR PropertyKeyword:hello) // AND (PropertyKeyword:world) // // Which is not what we want. We'd want to match documents that // had only "hello" without also having a keyword "world". In // this case, don't create the PropertyKeyword part of the query, // since it would be included in the larger set if it weren't // required anyway. if (added_subquery) { Term term; term = new Term ("PropertyKeyword", part.Text.ToLower ()); // make sure text is lowercased // FIXME: terms are already added in term_list. But they may have been tokenized // The term here is non-tokenized version. Should this be added to term_list ? // term_list is used to calculate scores if (term_list != null) term_list.Add (term); subquery = new LNS.TermQuery (term); p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); // Properties can live in either index if (! only_build_primary_query) s_query.Add (subquery.Clone () as LNS.Query, LNS.BooleanClause.Occur.SHOULD); } else { // Reset these so we return a null query p_query = null; s_query = null; } } primary_query = p_query; if (! only_build_primary_query) secondary_query = s_query; return; } if (abstract_part is QueryPart_Wildcard) { QueryPart_Wildcard part = (QueryPart_Wildcard) abstract_part; LNS.BooleanQuery p_query = new LNS.BooleanQuery (); LNS.BooleanQuery s_query = new LNS.BooleanQuery (); Term term; LNS.Query subquery; // Lower case the terms for searching string query_string_lower = part.QueryString.ToLower (); // Search text content if (! part.PropertyOnly) { term = new Term ("Text", query_string_lower); subquery = new LNS.WildcardQuery (term); p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); term_list.Add (term); } // Search text properties term = new Term ("PropertyText", query_string_lower); subquery = new LNS.WildcardQuery (term); p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); // Properties can live in either index if (! only_build_primary_query) s_query.Add (subquery.Clone () as LNS.Query, LNS.BooleanClause.Occur.SHOULD); term_list.Add (term); if (! part.PropertyOnly) { // Search property keywords term = new Term ("PropertyKeyword", query_string_lower); term_list.Add (term); subquery = new LNS.WildcardQuery (term); p_query.Add (subquery, LNS.BooleanClause.Occur.SHOULD); // Properties can live in either index if (! only_build_primary_query) s_query.Add (subquery.Clone () as LNS.Query, LNS.BooleanClause.Occur.SHOULD); } primary_query = p_query; if (! only_build_primary_query) secondary_query = s_query; return; } if (abstract_part is QueryPart_DateRange) { QueryPart_DateRange part = (QueryPart_DateRange) abstract_part; // FIXME: We don't handle prohibited queries with sub-date // accuracy. For example, if we say we prohibit matches // between 5 May 2007 at 2 PM and 8 May at 5 AM, we'll // miss any matches that happen between midnight and 2 PM // on 5 May 2007 and between midnight and 5 AM on 8 May. primary_query = GetDateRangeQuery (part, out hit_filter); // Date properties can live in either index if (! only_build_primary_query && primary_query != null) secondary_query = primary_query.Clone () as LNS.Query; return; } if (abstract_part is QueryPart_Or) { QueryPart_Or part = (QueryPart_Or) abstract_part; // Assemble a new BooleanQuery combining all of the sub-parts. LNS.BooleanQuery p_query; p_query = new LNS.BooleanQuery (); LNS.BooleanQuery s_query = null; if (! only_build_primary_query) s_query = new LNS.BooleanQuery (); primary_query = p_query; secondary_query = s_query; OrHitFilter or_hit_filter = null; foreach (QueryPart sub_part in part.SubParts) { LNS.Query p_subq, s_subq; HitFilter sub_hit_filter; // FIXME: This is (and must be) ignored // FIXME: Any subpart in an OR which has a hit filter won't work // correctly, because we can't tell which part of an OR we matched // against to filter correctly. This affects date range queries. QueryPartToQuery (sub_part, only_build_primary_query, term_list, query_part_hook, out p_subq, out s_subq, out sub_hit_filter); if (p_subq != null) p_query.Add (p_subq, LNS.BooleanClause.Occur.SHOULD); if (s_subq != null) s_query.Add (s_subq, LNS.BooleanClause.Occur.SHOULD); if (sub_hit_filter != null) { if (or_hit_filter == null) or_hit_filter = new OrHitFilter (); or_hit_filter.Add (sub_hit_filter); } } if (or_hit_filter != null) hit_filter = new HitFilter (or_hit_filter.HitFilter); return; } if (abstract_part is QueryPart_Uri) { QueryPart_Uri part = (QueryPart_Uri) abstract_part; // Do a term query on the Uri field. // This is probably less efficient that using a TermEnum; // but this is required for the query API where the uri query // can be part of a prohibited query or a boolean or query. Term term; term = new Term ("Uri", UriFu.UriToEscapedString (part.Uri)); if (term_list != null) term_list.Add (term); primary_query = new LNS.TermQuery (term); // Query only the primary index return; } if (abstract_part is QueryPart_Property) { QueryPart_Property part = (QueryPart_Property) abstract_part; string field_name; if (part.Key == QueryPart_Property.AllProperties) field_name = TypeToWildcardField (part.Type); else field_name = PropertyToFieldName (part.Type, part.Key); // Details of the conversion here depends on BeagrepAnalyzer::TokenStream if (part.Type == PropertyType.Text) primary_query = StringToQuery (field_name, part.Value, term_list); else { Term term; // FIXME: Handle date queries for other date fields if (part.Type == PropertyType.Internal || field_name.StartsWith ("prop:k:" + Property.PrivateNamespace)) term = new Term (field_name, part.Value); else term = new Term (field_name, part.Value.ToLower ()); if (term_list != null) term_list.Add (term); primary_query = new LNS.TermQuery (term); } // Properties can live in either index if (! only_build_primary_query && primary_query != null) secondary_query = primary_query.Clone () as LNS.Query; return; } throw new Exception ("Unhandled QueryPart type! " + abstract_part.ToString ()); }
public void TestLotsOfAsterisks() { RAMDirectory indexStore = GetIndexStore("body", new String[] { "metal", "metals" }); IndexSearcher searcher = new IndexSearcher(indexStore, true); System.Text.StringBuilder term = new System.Text.StringBuilder(); term.Append("m"); for (int i = 0; i < 512; i++) term.Append("*"); term.Append("tal"); Query query3 = new WildcardQuery(new Term("body", term.ToString())); AssertMatches(searcher, query3, 1); searcher.Close(); indexStore.Close(); }