本文整理汇总了C#中Lucene.Net.Search.Query.ExtractTerms方法的典型用法代码示例。如果您正苦于以下问题:C# Query.ExtractTerms方法的具体用法?C# Query.ExtractTerms怎么用?C# Query.ExtractTerms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Search.Query
的用法示例。
在下文中一共展示了Query.ExtractTerms方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: flatten
public void flatten(Query sourceQuery, Dictionary<Query, Query> flatQueries)
{
if (sourceQuery is BooleanQuery)
{
BooleanQuery bq = (BooleanQuery)sourceQuery;
foreach (BooleanClause clause in bq.GetClauses())
{
if (!clause.IsProhibited)
flatten(clause.Query, flatQueries);
}
}
else if (sourceQuery is PrefixQuery)
{
if (!flatQueries.ContainsKey(sourceQuery))
flatQueries.Add(sourceQuery, sourceQuery);
}
else if (sourceQuery is DisjunctionMaxQuery)
{
DisjunctionMaxQuery dmq = (DisjunctionMaxQuery)sourceQuery;
foreach (Query query in dmq)
{
flatten(query, flatQueries);
}
}
else if (sourceQuery is TermQuery)
{
if (!flatQueries.ContainsKey(sourceQuery))
flatQueries.Add(sourceQuery, sourceQuery);
}
else if (sourceQuery is PhraseQuery)
{
if (!flatQueries.ContainsKey(sourceQuery))
{
PhraseQuery pq = (PhraseQuery)sourceQuery;
if (pq.GetTerms().Length > 1)
flatQueries.Add(pq, pq);
else if (pq.GetTerms().Length == 1)
{
Query q = new TermQuery(pq.GetTerms()[0]);
flatQueries.Add(q, q);
}
}
}
else
{
// Fallback to using extracted terms
ISet<Term> terms = SetFactory.CreateHashSet<Term>();
try
{
sourceQuery.ExtractTerms(terms);
}
catch (NotSupportedException)
{ // thrown by default impl
return; // ignore error and discard query
}
foreach (var term in terms)
{
flatten(new TermQuery(term), flatQueries);
}
}
}
示例2: DoSearch
private void DoSearch(Query query, IEnumerable<SortField> sortField, int maxResults)
{
//This try catch is because analyzers strip out stop words and sometimes leave the query
//with null values. This simply tries to extract terms, if it fails with a null
//reference then its an invalid null query, NotSupporteException occurs when the query is
//valid but the type of query can't extract terms.
//This IS a work-around, theoretically Lucene itself should check for null query parameters
//before throwing exceptions.
try
{
var set = new Hashtable();
query.ExtractTerms(set);
}
catch (NullReferenceException)
{
//this means that an analyzer has stipped out stop words and now there are
//no words left to search on
TotalItemCount = 0;
return;
}
catch (NotSupportedException)
{
//swallow this exception, we should continue if this occurs.
}
maxResults = maxResults > 1 ? maxResults : LuceneSearcher.MaxDoc();
if (sortField.Count() == 0)
{
var topDocs = LuceneSearcher.Search(query, null, maxResults, new Sort());
_collector = new AllHitsCollector(topDocs.scoreDocs);
topDocs = null;
}
else
{
var topDocs = LuceneSearcher.Search(query, null, maxResults, new Sort(sortField.ToArray()));
_collector = new AllHitsCollector(topDocs.scoreDocs);
topDocs = null;
}
TotalItemCount = _collector.Count;
}