本文整理汇总了C#中Lucene.Net.Search.Searcher.Search方法的典型用法代码示例。如果您正苦于以下问题:C# Searcher.Search方法的具体用法?C# Searcher.Search怎么用?C# Searcher.Search使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Search.Searcher
的用法示例。
在下文中一共展示了Searcher.Search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckHits_
public static void CheckHits_(Query query, System.String defaultFieldName, Searcher searcher, int[] results, TestCase testCase)
{
Hits hits = searcher.Search(query);
System.Collections.Hashtable correct = new System.Collections.Hashtable();
for (int i = 0; i < results.Length; i++)
{
correct.Add((System.Int32) results[i], null);
}
System.Collections.Hashtable actual = new System.Collections.Hashtable();
for (int i = 0; i < hits.Length(); i++)
{
actual.Add((System.Int32) hits.Id(i), null);
}
//Assert.AreEqual(correct, actual, query.ToString(defaultFieldName));
if (correct.Count != 0)
{
System.Collections.IDictionaryEnumerator iter = correct.GetEnumerator();
bool status = false;
while (iter.MoveNext())
{
status = actual.ContainsKey(iter.Key);
if (status == false)
break;
}
Assert.IsTrue(status, query.ToString(defaultFieldName));
}
}
示例2: AssertMatchesPattern
// make sure the documents returned by the search match the expected list pattern
private void AssertMatchesPattern(Searcher searcher, Query query, Sort sort, System.String pattern)
{
Hits result = searcher.Search(query, sort);
System.Text.StringBuilder buff = new System.Text.StringBuilder(10);
int n = result.Length();
for (int i = 0; i < n; ++i)
{
Document doc = result.Doc(i);
System.String[] v = doc.GetValues("tracer");
for (int j = 0; j < v.Length; ++j)
{
buff.Append(v[j]);
}
}
// System.out.println ("matching \""+buff+"\" against pattern \""+pattern+"\"");
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
Assert.IsTrue(regex.IsMatch(buff.ToString()));
}
示例3: AssertMatches
// make sure the documents returned by the search match the expected list
private void AssertMatches(Searcher searcher, Query query, Sort sort, System.String expectedResult)
{
Hits result = searcher.Search(query, sort);
System.Text.StringBuilder buff = new System.Text.StringBuilder(10);
int n = result.Length();
for (int i = 0; i < n; ++i)
{
Document doc = result.Doc(i);
System.String[] v = doc.GetValues("tracer");
for (int j = 0; j < v.Length; ++j)
{
buff.Append(v[j]);
}
}
Assert.AreEqual(expectedResult, buff.ToString());
}
示例4: SetUp
public override void SetUp()
{
base.SetUp();
System.String[] docText = new System.String[]{"docThatNeverMatchesSoWeCanRequireLastDocCollectedToBeGreaterThanZero", "one blah three", "one foo three multiOne", "one foobar three multiThree", "blueberry pancakes", "blueberry pie", "blueberry strudel", "blueberry pizza"};
Directory directory = new RAMDirectory();
IndexWriter iw = new IndexWriter(directory, new WhitespaceAnalyzer(), true, MaxFieldLength.UNLIMITED);
for (int i = 0; i < N_DOCS; i++)
{
Add(docText[i % docText.Length], iw);
}
iw.Close();
searcher = new IndexSearcher(directory);
System.String qtxt = "one";
for (int i = 0; i < docText.Length; i++)
{
qtxt += (' ' + docText[i]); // large query so that search will be longer
}
QueryParser queryParser = new QueryParser(FIELD_NAME, new WhitespaceAnalyzer());
query = queryParser.Parse(qtxt);
// warm the searcher
searcher.Search(query, null, 1000);
}
示例5: AssertMatches
// make sure the documents returned by the search match the expected list
private void AssertMatches(Searcher searcher, Query query, Sort sort, System.String expectedResult)
{
//ScoreDoc[] result = searcher.search (query, null, 1000, sort).scoreDocs;
TopDocs hits = searcher.Search(query, null, expectedResult.Length, sort);
ScoreDoc[] result = hits.ScoreDocs;
Assert.AreEqual(hits.TotalHits, expectedResult.Length);
System.Text.StringBuilder buff = new System.Text.StringBuilder(10);
int n = result.Length;
for (int i = 0; i < n; ++i)
{
Document doc = searcher.Doc(result[i].Doc);
System.String[] v = doc.GetValues("tracer");
for (int j = 0; j < v.Length; ++j)
{
buff.Append(v[j]);
}
}
Assert.AreEqual(expectedResult, buff.ToString());
}
示例6: MatchHits
// make sure the documents returned by the search match the expected list
private void MatchHits(Searcher searcher, Sort sort)
{
// make a query without sorting first
ScoreDoc[] hitsByRank = searcher.Search(query, null, 1000).ScoreDocs;
CheckHits(hitsByRank, "Sort by rank: "); // check for duplicates
System.Collections.IDictionary resultMap = new System.Collections.SortedList();
// store hits in TreeMap - TreeMap does not allow duplicates; existing entries are silently overwritten
for (int hitid = 0; hitid < hitsByRank.Length; ++hitid)
{
resultMap[hitsByRank[hitid].Doc] = hitid; // Value: Hits-Objekt Index
}
// now make a query using the sort criteria
ScoreDoc[] resultSort = searcher.Search(query, null, 1000, sort).ScoreDocs;
CheckHits(resultSort, "Sort by custom criteria: "); // check for duplicates
// besides the sorting both sets of hits must be identical
for (int hitid = 0; hitid < resultSort.Length; ++hitid)
{
System.Int32 idHitDate = (System.Int32) resultSort[hitid].Doc; // document ID from sorted search
if (!resultMap.Contains(idHitDate))
{
Log("ID " + idHitDate + " not found. Possibliy a duplicate.");
}
Assert.IsTrue(resultMap.Contains(idHitDate)); // same ID must be in the Map from the rank-sorted search
// every hit must appear once in both result sets --> remove it from the Map.
// At the end the Map must be empty!
resultMap.Remove(idHitDate);
}
if (resultMap.Count == 0)
{
// log("All hits matched");
}
else
{
Log("Couldn't match " + resultMap.Count + " hits.");
}
Assert.AreEqual(resultMap.Count, 0);
}
示例7: SearchFor
private void SearchFor(int n, Searcher searcher)
{
System.Console.Out.WriteLine("Searching for " + n);
Hits hits = searcher.Search(QueryParsers.QueryParser.Parse(English.IntToEnglish(n), "contents", Lucene.Net.ThreadSafetyTest.ANALYZER));
System.Console.Out.WriteLine("Search for " + n + ": total=" + hits.Length());
for (int j = 0; j < System.Math.Min(3, hits.Length()); j++)
{
System.Console.Out.WriteLine("Hit for " + n + ": " + hits.Doc(j).Get("id"));
}
}
示例8: CheckHits_Renamed_Method
/// <summary> Tests that a query matches the an expected set of documents using Hits.
///
/// <p/>
/// Note that when using the Hits API, documents will only be returned
/// if they have a positive normalized score.
/// <p/>
/// </summary>
/// <param name="query">the query to test
/// </param>
/// <param name="searcher">the searcher to test the query against
/// </param>
/// <param name="defaultFieldName">used for displaing the query in assertion messages
/// </param>
/// <param name="results">a list of documentIds that must match the query
/// </param>
/// <seealso cref="Searcher.Search(Query)">
/// </seealso>
/// <seealso cref="checkHitCollector">
/// </seealso>
public static void CheckHits_Renamed_Method(Query query, System.String defaultFieldName, Searcher searcher, int[] results)
{
if (searcher is IndexSearcher)
{
QueryUtils.Check(query, searcher);
}
ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
System.Collections.ArrayList correct = new System.Collections.ArrayList();
for (int i = 0; i < results.Length; i++)
{
SupportClass.CollectionsHelper.AddIfNotContains(correct, results[i]);
}
correct.Sort();
System.Collections.ArrayList actual = new System.Collections.ArrayList();
for (int i = 0; i < hits.Length; i++)
{
SupportClass.CollectionsHelper.AddIfNotContains(actual, hits[i].doc);
}
actual.Sort();
Assert.AreEqual(correct, actual, query.ToString(defaultFieldName));
QueryUtils.Check(query, searcher);
}
示例9: Expand
/// <summary>
/// Perform synonym expansion on a query.
/// </summary>
/// <param name="query">query</param>
/// <param name="syns">syns</param>
/// <param name="a">a</param>
/// <param name="field">field</param>
/// <param name="boost">boost</param>
public static Query Expand(String query,
Searcher syns,
Analyzer a,
String field,
float boost)
{
already = new List<String>(); // avoid dups
var top = new List<String>(); // needs to be separately listed..
var ts = a.TokenStream(field, new StringReader(query));
var termAtt = ts.AddAttribute<TermAttribute>();
while (ts.IncrementToken())
{
var word = termAtt.Term;
if (!already.Contains(word))
{
already.Add(word);
top.Add(word);
}
}
tmp = new BooleanQuery();
// [2] form query
System.Collections.IEnumerator it = top.GetEnumerator();
while (it.MoveNext())
{
// [2a] add to level words in
var word = (String)it.Current;
var tq = new TermQuery(new Term(field, word));
tmp.Add(tq, Occur.SHOULD);
var c = new CollectorImpl(field, boost);
syns.Search(new TermQuery(new Term(Syns2Index.F_WORD, word)), c);
}
return tmp;
}
示例10: CheckExplanations
/// <summary> Asserts that the explanation value for every document matching a
/// query corresponds with the true score. Optionally does "deep"
/// testing of the explanation details.
///
/// </summary>
/// <seealso cref="ExplanationAsserter">
/// </seealso>
/// <param name="query">the query to test
/// </param>
/// <param name="searcher">the searcher to test the query against
/// </param>
/// <param name="defaultFieldName">used for displaing the query in assertion messages
/// </param>
/// <param name="deep">indicates whether a deep comparison of sub-Explanation details should be executed
/// </param>
public static void CheckExplanations(Query query, System.String defaultFieldName, Searcher searcher, bool deep)
{
searcher.Search(query, new ExplanationAsserter(query, defaultFieldName, searcher, deep));
}
示例11: CheckHits_Renamed
/// <summary> Tests that a query matches the an expected set of documents using Hits.
///
/// <p>
/// Note that when using the Hits API, documents will only be returned
/// if they have a positive normalized score.
/// </p>
/// </summary>
/// <param name="query">the query to test
/// </param>
/// <param name="searcher">the searcher to test the query against
/// </param>
/// <param name="defaultFieldName">used for displaing the query in assertion messages
/// </param>
/// <param name="results">a list of documentIds that must match the query
/// </param>
/// <seealso cref="Searcher.Search(Query)">
/// </seealso>
/// <seealso cref="CheckHitCollector">
/// </seealso>
public static void CheckHits_Renamed(Query query, System.String defaultFieldName, Searcher searcher, int[] results)
{
if (searcher is IndexSearcher)
{
QueryUtils.Check(query, (IndexSearcher) searcher);
}
Hits hits = searcher.Search(query);
System.Collections.ArrayList correct = new System.Collections.ArrayList(results.Length);
for (int i = 0; i < results.Length; i++)
{
correct.Add(results[i]);
}
System.Collections.ArrayList actual = new System.Collections.ArrayList(hits.Length());
for (int i = 0; i < hits.Length(); i++)
{
actual.Add(hits.Id(i));
}
Assert.AreEqual(correct.Count, actual.Count);
correct.Sort();
actual.Sort();
for (int i = 0; i < correct.Count; i++)
{
Assert.AreEqual(correct[i], actual[i]);
}
QueryUtils.Check(query, searcher);
}
示例12: CheckHitCollector
/// <summary> Tests that a query matches the an expected set of documents using a
/// HitCollector.
///
/// <p>
/// Note that when using the HitCollector API, documents will be collected
/// if they "match" regardless of what their score is.
/// </p>
/// </summary>
/// <param name="query">the query to test
/// </param>
/// <param name="searcher">the searcher to test the query against
/// </param>
/// <param name="defaultFieldName">used for displaing the query in assertion messages
/// </param>
/// <param name="results">a list of documentIds that must match the query
/// </param>
/// <seealso cref="Searcher.Search(Query,HitCollector)">
/// </seealso>
/// <seealso cref="checkHits">
/// </seealso>
public static void CheckHitCollector(Query query, System.String defaultFieldName, Searcher searcher, int[] results)
{
System.Collections.ArrayList correct = new System.Collections.ArrayList(results.Length);
for (int i = 0; i < results.Length; i++)
{
correct.Add(results[i]);
}
System.Collections.Hashtable actual = new System.Collections.Hashtable();
searcher.Search(query, new AnonymousClassHitCollector(actual));
System.Collections.IDictionaryEnumerator e = actual.GetEnumerator();
while (e.MoveNext())
{
Assert.Contains(e.Key, correct, query.ToString(defaultFieldName));
}
QueryUtils.Check(query, searcher);
}
示例13: DoPagingSearch
/// <summary> This demonstrates a typical paging search scenario, where the search engine presents
/// pages of size n to the user. The user can then go to the next page if interested in
/// the next hits.
///
/// When the query is executed for the first time, then only enough results are collected
/// to fill 5 result pages. If the user wants to page beyond this limit, then the query
/// is executed another time and all hits are collected.
///
/// </summary>
public static void DoPagingSearch(StreamReader input, Searcher searcher, Query query, int hitsPerPage, bool raw, bool interactive)
{
// Collect enough docs to show 5 pages
var collector = TopScoreDocCollector.Create(5 * hitsPerPage, false);
searcher.Search(query, collector);
var hits = collector.TopDocs().ScoreDocs;
int numTotalHits = collector.TotalHits;
Console.Out.WriteLine(numTotalHits + " total matching documents");
int start = 0;
int end = Math.Min(numTotalHits, hitsPerPage);
while (true)
{
if (end > hits.Length)
{
Console.Out.WriteLine("Only results 1 - " + hits.Length + " of " + numTotalHits + " total matching documents collected.");
Console.Out.WriteLine("Collect more (y/n) ?");
String line = input.ReadLine();
if (String.IsNullOrEmpty(line) || line[0] == 'n')
{
break;
}
collector = TopScoreDocCollector.Create(numTotalHits, false);
searcher.Search(query, collector);
hits = collector.TopDocs().ScoreDocs;
}
end = Math.Min(hits.Length, start + hitsPerPage);
for (int i = start; i < end; i++)
{
if (raw)
{
// output raw format
Console.Out.WriteLine("doc=" + hits[i].Doc + " score=" + hits[i].Score);
continue;
}
Document doc = searcher.Doc(hits[i].Doc);
String path = doc.Get("path");
if (path != null)
{
Console.Out.WriteLine((i + 1) + ". " + path);
String title = doc.Get("title");
if (title != null)
{
Console.Out.WriteLine(" Title: " + doc.Get("title"));
}
}
else
{
Console.Out.WriteLine((i + 1) + ". " + "No path for this document");
}
}
if (!interactive)
{
break;
}
if (numTotalHits >= end)
{
bool quit = false;
while (true)
{
Console.Out.Write("Press ");
if (start - hitsPerPage >= 0)
{
Console.Out.Write("(p)revious page, ");
}
if (start + hitsPerPage < numTotalHits)
{
Console.Out.Write("(n)ext page, ");
}
Console.Out.WriteLine("(q)uit or enter number to jump to a page.");
String line = input.ReadLine();
if (String.IsNullOrEmpty(line) || line[0] == 'q')
{
quit = true;
break;
}
if (line[0] == 'p')
{
start = Math.Max(0, start - hitsPerPage);
break;
}
//.........这里部分代码省略.........
示例14: DoStreamingSearch
/// <summary>
/// This method uses a custom HitCollector implementation which simply prints out
/// the docId and score of every matching document.
///
/// This simulates the streaming search use case, where all hits are supposed to
/// be processed, regardless of their relevance.
/// </summary>
public static void DoStreamingSearch(Searcher searcher, Query query)
{
Collector streamingHitCollector = new AnonymousClassCollector();
searcher.Search(query, streamingHitCollector);
}
示例15: CheckHitCollector
/// <summary> Tests that a query matches the an expected set of documents using a
/// HitCollector.
///
/// <p/>
/// Note that when using the HitCollector API, documents will be collected
/// if they "match" regardless of what their score is.
/// <p/>
/// </summary>
/// <param name="query">the query to test
/// </param>
/// <param name="searcher">the searcher to test the query against
/// </param>
/// <param name="defaultFieldName">used for displaying the query in assertion messages
/// </param>
/// <param name="results">a list of documentIds that must match the query
/// </param>
/// <seealso cref="Searcher.Search(Query,HitCollector)">
/// </seealso>
/// <seealso cref="checkHits">
/// </seealso>
public static void CheckHitCollector(Query query, System.String defaultFieldName, Searcher searcher, int[] results)
{
QueryUtils.Check(query, searcher);
System.Collections.Hashtable correct = new System.Collections.Hashtable();
for (int i = 0; i < results.Length; i++)
{
SupportClass.CollectionsHelper.AddIfNotContains(correct, (System.Int32) results[i]);
}
System.Collections.Hashtable actual = new System.Collections.Hashtable();
Collector c = new SetCollector(actual);
searcher.Search(query, c);
Assert.AreEqual(correct, actual, "Simple: " + query.ToString(defaultFieldName));
for (int i = - 1; i < 2; i++)
{
actual.Clear();
QueryUtils.WrapSearcher(searcher, i).Search(query, c);
Assert.AreEqual(correct, actual, "Wrap Searcher " + i + ": " + query.ToString(defaultFieldName));
}
if (!(searcher is IndexSearcher))
return ;
for (int i = - 1; i < 2; i++)
{
actual.Clear();
QueryUtils.WrapUnderlyingReader((IndexSearcher) searcher, i).Search(query, c);
Assert.AreEqual(correct, actual, "Wrap Reader " + i + ": " + query.ToString(defaultFieldName));
}
}