本文整理汇总了C#中Lucene.Net.Search.TermQuery类的典型用法代码示例。如果您正苦于以下问题:C# TermQuery类的具体用法?C# TermQuery怎么用?C# TermQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TermQuery类属于Lucene.Net.Search命名空间,在下文中一共展示了TermQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReverseDateSort
public virtual void TestReverseDateSort()
{
IndexSearcher searcher = NewSearcher(Reader);
Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.Type_e.STRING, true));
Query query = new TermQuery(new Term(TEXT_FIELD, "document"));
// Execute the search and process the search results.
string[] actualOrder = new string[5];
ScoreDoc[] hits = searcher.Search(query, null, 1000, sort).ScoreDocs;
for (int i = 0; i < hits.Length; i++)
{
Document document = searcher.Doc(hits[i].Doc);
string text = document.Get(TEXT_FIELD);
actualOrder[i] = text;
}
// Set up the expected order (i.e. Document 5, 4, 3, 2, 1).
string[] expectedOrder = new string[5];
expectedOrder[0] = "Document 5";
expectedOrder[1] = "Document 4";
expectedOrder[2] = "Document 3";
expectedOrder[3] = "Document 2";
expectedOrder[4] = "Document 1";
Assert.AreEqual(Arrays.AsList(expectedOrder), Arrays.AsList(actualOrder));
}
示例2: ParseRange
public static BooleanQuery ParseRange(string fieldName, long lowerValue, long upperValue, bool inclusive)
{
if (lowerValue > upperValue)
{
return null;
}
//var rangeQuery = new BooleanQuery();
var dateQuery = new BooleanQuery();
BooleanQuery.SetMaxClauseCount(int.MaxValue);
for (long i = lowerValue; i < upperValue; i++)
{
var term = new Term(fieldName, i.ToString());
var q = new TermQuery(term);
dateQuery.Add(q, BooleanClause.Occur.SHOULD);
}
if (inclusive)
{
var term = new Term(fieldName, upperValue.ToString());
var q = new TermQuery(term);
dateQuery.Add(q, BooleanClause.Occur.SHOULD);
}
//if (dateQuery.GetClauses() != null || dateQuery.GetClauses().Length != 0)
//{
// rangeQuery.Add(dateQuery, BooleanClause.Occur.MUST);
//}
return dateQuery;
}
示例3: TermWeight
public TermWeight(TermQuery enclosingInstance, Searcher searcher)
{
InitBlock(enclosingInstance);
this.similarity = Enclosing_Instance.GetSimilarity(searcher);
idfExp = similarity.IdfExplain(Enclosing_Instance.term, searcher);
idf = idfExp.Idf;
}
示例4: MultiSearchBIMXchange
public static LuceneResult MultiSearchBIMXchange(Dictionary<string,string> terms, int pageSize, int pageNumber)
{
var directory = FSDirectory.Open(new DirectoryInfo("LuceneIndex"));
var booleanQuery = new BooleanQuery();
foreach(var term in terms)
{
var query = new TermQuery(new Term(term.Key, term.Value));
booleanQuery.Add(query,BooleanClause.Occur.MUST);
}
var searcher = new IndexSearcher(directory, true);
var topDocs = searcher.Search(booleanQuery, 10);
var docs = new List<Document>();
var start = (pageNumber - 1) * pageSize;
for (var i = start; i < start + pageSize && i < topDocs.TotalHits; i++)
{
var scoreDoc = topDocs.ScoreDocs[i];
var docId = scoreDoc.doc;
var doc = searcher.Doc(docId);
docs.Add(doc);
}
searcher.Close();
directory.Close();
var result = new LuceneResult {Results = docs, TotalCount = topDocs.TotalHits};
return result;
}
示例5: GetDocIDs
private IEnumerable<int> GetDocIDs(IndexReader reader, string groupByField, string group)
{
var query = new TermQuery(new Term(groupByField, group));
Filter filter = new CachingWrapperFilter(new QueryWrapperFilter(query));
return filter.GetDocIdSet(reader).AsEnumerable().ToList();
}
示例6: WithCategory
public AppQuery WithCategory(int category) {
if (category != 0) {
Query query = new TermQuery(new Term("Category", category.ToString()));
AddQuery(query, BooleanClause.Occur.MUST);
}
return this;
}
示例7: SearchCore
private static IList<int> SearchCore(SearchFilter searchFilter, out int totalHits)
{
if (!Directory.Exists(LuceneCommon.IndexDirectory))
{
totalHits = 0;
return new int[0];
}
SortField sortField = GetSortField(searchFilter);
int numRecords = searchFilter.Skip + searchFilter.Take;
using (var directory = new LuceneFileSystem(LuceneCommon.IndexDirectory))
{
var searcher = new IndexSearcher(directory, readOnly: true);
var query = ParseQuery(searchFilter);
var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));
Filter filter = new QueryWrapperFilter(termQuery);
var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(sortField));
var keys = results.scoreDocs.Skip(searchFilter.Skip)
.Select(c => ParseKey(searcher.Doc(c.doc).Get("Key")))
.ToList();
totalHits = results.totalHits;
searcher.Close();
return keys;
}
}
示例8: TestBasic
public virtual void TestBasic()
{
// create a sort field and sort by it (reverse order)
Query query = new TermQuery(new Term("body", "contents"));
IndexReader r = searcher.IndexReader;
// Just first pass query
TopDocs hits = searcher.Search(query, 10);
AreEqual(3, hits.TotalHits);
AreEqual("3", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
AreEqual("2", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
// Now, rescore:
Expression e = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
SimpleBindings bindings = new SimpleBindings();
bindings.Add(new SortField("popularity", SortField.Type_e.INT));
bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
Rescorer rescorer = e.GetRescorer(bindings);
hits = rescorer.Rescore(searcher, hits, 10);
AreEqual(3, hits.TotalHits);
AreEqual("2", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
AreEqual("3", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
string expl = rescorer.Explain(searcher, searcher.Explain(query, hits.ScoreDocs[0].Doc), hits.ScoreDocs[0].Doc).ToString();
// Confirm the explanation breaks out the individual
// variables:
IsTrue(expl.Contains("= variable \"popularity\""));
// Confirm the explanation includes first pass details:
IsTrue(expl.Contains("= first pass score"));
IsTrue(expl.Contains("body:contents in"));
}
示例9: TestTermSearch
public void TestTermSearch()
{
using (var dir = FSDirectory.Open(TestEnvironment.TestIndexDirectory))
using (var indexSearcher = new IndexSearcher(dir))
{
var termSubjectAnt = new Term("subject", "ant");
var termQuerySubjectAnt = new TermQuery(termSubjectAnt);
var topDocsSubjectAnt = indexSearcher.Search(termQuerySubjectAnt, 10);
// title=Ant in Action
// subject=apache ant build tool junit java development
Assert.Equal(1, topDocsSubjectAnt.TotalHits);
var termSubjectJUnit = new Term("subject", "junit");
var termQuerySubjectJUnit = new TermQuery(termSubjectJUnit);
var topDocsSubjectJUnit = indexSearcher.Search(termQuerySubjectJUnit, 10);
// ExplainResults(indexSearcher, termQuerySubjectJUnit, topDocsSubjectJUnit);
// title=JUnit in Action, Second Edition
// subject=junit unit testing mock objects
// title=Ant in Action
// subject=apache ant build tool junit java development
Assert.Equal(2, topDocsSubjectJUnit.TotalHits); // Ants in Action, "JUnit in Action, Second Edition"
}
}
示例10: 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);
}
示例11: FilterQueryByClasses
public static Lucene.Net.Search.Query FilterQueryByClasses(IESI.ISet<System.Type> classesAndSubclasses, Lucene.Net.Search.Query luceneQuery)
{
// A query filter is more practical than a manual class filtering post query (esp on scrollable resultsets)
// it also probably minimise the memory footprint
if (classesAndSubclasses == null)
{
return luceneQuery;
}
BooleanQuery classFilter = new BooleanQuery();
// annihilate the scoring impact of DocumentBuilder.CLASS_FIELDNAME
classFilter.SetBoost(0);
foreach (System.Type clazz in classesAndSubclasses)
{
Term t = new Term(DocumentBuilder.CLASS_FIELDNAME, TypeHelper.LuceneTypeName(clazz));
TermQuery termQuery = new TermQuery(t);
classFilter.Add(termQuery, BooleanClause.Occur.SHOULD);
}
BooleanQuery filteredQuery = new BooleanQuery();
filteredQuery.Add(luceneQuery, BooleanClause.Occur.MUST);
filteredQuery.Add(classFilter, BooleanClause.Occur.MUST);
return filteredQuery;
}
示例12: TestNullOrSubScorer
public virtual void TestNullOrSubScorer()
{
Directory dir = new MockRAMDirectory();
IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.Add(new Field("field", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
w.AddDocument(doc);
IndexReader r = w.GetReader();
IndexSearcher s = new IndexSearcher(r);
BooleanQuery q = new BooleanQuery();
q.Add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
// LUCENE-2617: make sure that a term not in the index still contributes to the score via coord factor
float score = s.Search(q, 10).MaxScore;
Query subQuery = new TermQuery(new Term("field", "not_in_index"));
subQuery.Boost = 0;
q.Add(subQuery, Occur.SHOULD);
float score2 = s.Search(q, 10).MaxScore;
Assert.AreEqual(score * .5, score2, 1e-6);
// LUCENE-2617: make sure that a clause not in the index still contributes to the score via coord factor
BooleanQuery qq = (BooleanQuery)q.Clone();
PhraseQuery phrase = new PhraseQuery();
phrase.Add(new Term("field", "not_in_index"));
phrase.Add(new Term("field", "another_not_in_index"));
phrase.Boost = 0;
qq.Add(phrase, Occur.SHOULD);
score2 = s.Search(qq, 10).MaxScore;
Assert.AreEqual(score * (1.0 / 3), score2, 1e-6);
// now test BooleanScorer2
subQuery = new TermQuery(new Term("field", "b"));
subQuery.Boost = 0;
q.Add(subQuery, Occur.MUST);
score2 = s.Search(q, 10).MaxScore;
Assert.AreEqual(score * (2.0 / 3), score2, 1e-6);
// PhraseQuery w/ no terms added returns a null scorer
PhraseQuery pq = new PhraseQuery();
q.Add(pq, Occur.SHOULD);
Assert.AreEqual(1, s.Search(q, 10).TotalHits);
// A required clause which returns null scorer should return null scorer to
// IndexSearcher.
q = new BooleanQuery();
pq = new PhraseQuery();
q.Add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
q.Add(pq, Occur.MUST);
Assert.AreEqual(0, s.Search(q, 10).TotalHits);
DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);
dmq.Add(new TermQuery(new Term("field", "a")));
dmq.Add(pq);
Assert.AreEqual(1, s.Search(dmq, 10).TotalHits);
r.Close();
w.Close();
dir.Close();
}
示例13: SearchCore
private IQueryable<Package> SearchCore(SearchFilter searchFilter, out int totalHits)
{
int numRecords = searchFilter.Skip + searchFilter.Take;
var searcher = new IndexSearcher(_directory, readOnly: true);
var query = ParseQuery(searchFilter);
// IF searching by relevance, boost scores by download count.
if (searchFilter.SortProperty == SortProperty.Relevance)
{
var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT);
query = new CustomScoreQuery(query, downloadCountBooster);
}
var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));
var filter = new QueryWrapperFilter(termQuery);
var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter)));
totalHits = results.totalHits;
if (results.totalHits == 0 || searchFilter.CountOnly)
{
return Enumerable.Empty<Package>().AsQueryable();
}
var packages = results.scoreDocs
.Skip(searchFilter.Skip)
.Select(sd => PackageFromDoc(searcher.Doc(sd.doc)))
.ToList();
return packages.AsQueryable();
}
示例14: Rewrite
public override Query Rewrite(IndexReader reader)
{
BooleanQuery query = new BooleanQuery(true);
TermEnum enumerator = reader.Terms(prefix);
try
{
System.String prefixText = prefix.Text();
System.String prefixField = prefix.Field();
do
{
Term term = enumerator.Term();
#if !FRAMEWORK_1_1
if (term != null && term.Text().StartsWith(prefixText, StringComparison.Ordinal) && term.Field() == prefixField)
#else
if (term != null && term.Text().StartsWith(prefixText) && term.Field() == prefixField)
#endif
{
TermQuery tq = new TermQuery(term); // found a match
tq.SetBoost(GetBoost()); // set the boost
query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
//System.out.println("added " + term);
}
else
{
break;
}
}
while (enumerator.Next());
}
finally
{
enumerator.Close();
}
return query;
}
示例15: Test
public virtual void Test()
{
Term allTerm = new Term(FIELD, "all");
TermQuery termQuery = new TermQuery(allTerm);
Weight weight = IndexSearcher.CreateNormalizedWeight(termQuery);
Assert.IsTrue(IndexSearcher.TopReaderContext is AtomicReaderContext);
AtomicReaderContext context = (AtomicReaderContext)IndexSearcher.TopReaderContext;
BulkScorer ts = weight.BulkScorer(context, true, ((AtomicReader)context.Reader()).LiveDocs);
// we have 2 documents with the term all in them, one document for all the
// other values
IList<TestHit> docs = new List<TestHit>();
// must call next first
ts.Score(new CollectorAnonymousInnerClassHelper(this, context, docs));
Assert.IsTrue(docs.Count == 2, "docs Size: " + docs.Count + " is not: " + 2);
TestHit doc0 = docs[0];
TestHit doc5 = docs[1];
// The scores should be the same
Assert.IsTrue(doc0.Score == doc5.Score, doc0.Score + " does not equal: " + doc5.Score);
/*
* Score should be (based on Default Sim.: All floats are approximate tf = 1
* numDocs = 6 docFreq(all) = 2 idf = ln(6/3) + 1 = 1.693147 idf ^ 2 =
* 2.8667 boost = 1 lengthNorm = 1 //there is 1 term in every document coord
* = 1 sumOfSquaredWeights = (idf * boost) ^ 2 = 1.693147 ^ 2 = 2.8667
* queryNorm = 1 / (sumOfSquaredWeights)^0.5 = 1 /(1.693147) = 0.590
*
* score = 1 * 2.8667 * 1 * 1 * 0.590 = 1.69
*/
Assert.IsTrue(doc0.Score == 1.6931472f, doc0.Score + " does not equal: " + 1.6931472f);
}