本文整理汇总了C#中Lucene.Net.Search.WildcardQuery类的典型用法代码示例。如果您正苦于以下问题:C# WildcardQuery类的具体用法?C# WildcardQuery怎么用?C# WildcardQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WildcardQuery类属于Lucene.Net.Search命名空间,在下文中一共展示了WildcardQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddQuery
/// <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());
}
}
}
}
示例2: FindSuggestionsBySubstring
/// <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;
}
示例3: TestAsterisk
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);
}
示例4: TestAsterisk
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();
}
示例5: TestTermWithoutWildcard
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);
}
示例6: TestTermWithoutWildcard
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();
}
示例7: VisitWildcardQuery
public override Query VisitWildcardQuery(WildcardQuery wildcardq)
{
_dump.Append("WildcardQ(");
var q = base.VisitWildcardQuery(wildcardq);
_dump.Append(BoostToString(q));
_dump.Append(")");
return q;
}
示例8: TestTermWithoutWildcard
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);
}
示例9: BuildQuery
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);
}
示例10: BuildQuery
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);
}
示例11: GetTextQuery
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;
}
示例12: GetPlaceNameQuery
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;
}
示例13: Build
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;
}
示例14: TestQuestionmark
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);
}
示例15: GetHint
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 }
};
}