本文整理汇总了C#中Lucene.Net.Search.BooleanQuery类的典型用法代码示例。如果您正苦于以下问题:C# BooleanQuery类的具体用法?C# BooleanQuery怎么用?C# BooleanQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BooleanQuery类属于Lucene.Net.Search命名空间,在下文中一共展示了BooleanQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildQuery
public static Query BuildQuery(string query)
{
var matchCollection = untokenizedQuery.Matches(query);
if (matchCollection.Count == 0)
return new QueryParser(Version.LUCENE_CURRENT, "", new StandardAnalyzer(Version.LUCENE_CURRENT)).Parse(query);
var sb = new StringBuilder(query);
var booleanQuery = new BooleanQuery();
foreach (Match match in matchCollection)
{
BooleanClause.Occur occur;
switch (match.Groups[1].Value)
{
case "+":
occur=BooleanClause.Occur.MUST;
break;
case "-":
occur=BooleanClause.Occur.MUST_NOT;
break;
default:
occur=BooleanClause.Occur.SHOULD;
break;
}
booleanQuery.Add(new TermQuery(new Term(match.Groups[2].Value, match.Groups[3].Value)), occur);
sb.Replace(match.Value, "");
}
var remaining = sb.ToString().Trim();
if(remaining.Length > 0)
{
booleanQuery.Add(new QueryParser(Version.LUCENE_CURRENT, "", new StandardAnalyzer(Version.LUCENE_CURRENT)).Parse(remaining), BooleanClause.Occur.SHOULD);
}
return booleanQuery;
}
示例2: 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")), BooleanClause.Occur.SHOULD);
// PhraseQuery w/ no terms added returns a null scorer
PhraseQuery pq = new PhraseQuery();
q.Add(pq, BooleanClause.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")), BooleanClause.Occur.SHOULD);
q.Add(pq, BooleanClause.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();
}
示例3: TestQuery
public virtual void TestQuery()
{
RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true);
AddDoc("one", iw);
AddDoc("two", iw);
AddDoc("three four", iw);
iw.Close();
IndexSearcher is_Renamed = new IndexSearcher(dir);
Hits hits = is_Renamed.Search(new MatchAllDocsQuery());
Assert.AreEqual(3, hits.Length());
// some artificial queries to trigger the use of skipTo():
BooleanQuery bq = new BooleanQuery();
bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
hits = is_Renamed.Search(bq);
Assert.AreEqual(3, hits.Length());
bq = new BooleanQuery();
bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
bq.Add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
hits = is_Renamed.Search(bq);
Assert.AreEqual(1, hits.Length());
// delete a document:
is_Renamed.GetIndexReader().DeleteDocument(0);
hits = is_Renamed.Search(new MatchAllDocsQuery());
Assert.AreEqual(2, hits.Length());
is_Renamed.Close();
}
示例4: GetQuery
public Query GetQuery()
{
if (String.IsNullOrWhiteSpace(Term) && String.IsNullOrWhiteSpace(Type) && !CreatedOnTo.HasValue && !CreatedOnFrom.HasValue && Parent == null)
return new MatchAllDocsQuery();
var booleanQuery = new BooleanQuery();
if (!String.IsNullOrWhiteSpace(Term))
{
var indexDefinition = IndexingHelper.Get<AdminWebpageIndexDefinition>();
var analyser = indexDefinition.GetAnalyser();
var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser);
Query query = Term.SafeGetSearchQuery(parser, analyser);
booleanQuery.Add(query, Occur.MUST);
}
if (CreatedOnFrom.HasValue || CreatedOnTo.HasValue)
booleanQuery.Add(GetDateQuery(), Occur.MUST);
if (!string.IsNullOrEmpty(Type))
booleanQuery.Add(new TermQuery(new Term(FieldDefinition.GetFieldName<TypeFieldDefinition>(), Type)),
Occur.MUST);
if (Parent != null)
booleanQuery.Add(
new TermQuery(new Term(FieldDefinition.GetFieldName<ParentIdFieldDefinition>(), Parent.Id.ToString())), Occur.MUST);
return booleanQuery;
}
示例5: TestBQ14
public virtual void TestBQ14()
{
BooleanQuery q = new BooleanQuery(true);
q.Add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
q.Add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
Qtest(q, new int[] { 0, 1, 2, 3 });
}
示例6: Get
public IEnumerable<BeerReview> Get(string reviewText = null, int limit = 10, double minAroma = 0, double minAppearance = 0, double minOverall = 0, double minTaste = 0, double minPalate = 0)
{
var query = new BooleanQuery();
Query reviewTextQuery = new TermQuery(new Term("reviewText", reviewText));
Query reviewAppearanceQuery = NumericRangeQuery.NewDoubleRange("reviewAppearance", minAppearance, null, minInclusive: true, maxInclusive: true);
Query reviewAromaQuery = NumericRangeQuery.NewDoubleRange("reviewAroma", minAroma, null, minInclusive: true, maxInclusive: true);
Query reviewPalateQuery = NumericRangeQuery.NewDoubleRange("reviewPalate", minPalate, null, minInclusive: true, maxInclusive: true);
Query reviewTasteQuery = NumericRangeQuery.NewDoubleRange("reviewTaste", minTaste, null, minInclusive: true, maxInclusive: true);
Query reviewOverallQuery = NumericRangeQuery.NewDoubleRange("reviewOverall", minOverall, null, minInclusive: true, maxInclusive: true);
if (reviewText != null) { query.Add(reviewTextQuery, Occur.MUST); }
query.Add(reviewAppearanceQuery, Occur.MUST);
query.Add(reviewAromaQuery, Occur.MUST);
query.Add(reviewOverallQuery, Occur.MUST);
query.Add(reviewPalateQuery, Occur.MUST);
query.Add(reviewTasteQuery, Occur.MUST);
var hits = indexSearcher.Search(query, limit);
var beers = new List<BeerReview>();
for (int i = 0; i < hits.ScoreDocs.Count(); i++)
{
beers.Add(BeerReviewFromDoc(hits.ScoreDocs[i]));
}
return beers;
}
示例7: 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();
}
示例8: AddDateRangeQuery
internal static void AddDateRangeQuery(BooleanQuery query, DateRangeSearchParam.DateRangeField dateRangeField, BooleanClause.Occur occurance)
{
var startDateTime = dateRangeField.StartDate;
if (dateRangeField.InclusiveStart)
{
if (startDateTime != DateTime.MinValue)
{
startDateTime.ChangeTime(0, 0, 0, 0);
}
}
var endDateTime = dateRangeField.EndDate;
if (dateRangeField.InclusiveStart)
{
if (endDateTime != DateTime.MaxValue)
{
endDateTime = endDateTime.ChangeTime(23, 59, 59, 59);
}
}
BooleanQuery.SetMaxClauseCount(int.MaxValue);
if (!(dateRangeField.StartDate == DateTime.MinValue && dateRangeField.EndDate == DateTime.MaxValue))
{
query.Add(new RangeQuery(new Term(SearchFieldIDs.CreatedDate, startDateTime.ToString("yyyyMMdd")), new Term(SearchFieldIDs.CreatedDate, endDateTime.ToString("yyyyMMdd")), true), occurance);
}
}
示例9: FilterQueryByClasses
public static Lucene.Net.Search.Query FilterQueryByClasses(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;
}
示例10: Build
public virtual Query Build(IQueryNode queryNode)
{
StandardBooleanQueryNode booleanNode = (StandardBooleanQueryNode)queryNode;
BooleanQuery bQuery = new BooleanQuery(booleanNode.DisableCoord);
IList<IQueryNode> children = booleanNode.GetChildren();
if (children != null)
{
foreach (IQueryNode child in children)
{
object obj = child.GetTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
if (obj != null)
{
Query query = (Query)obj;
try
{
bQuery.Add(query, GetModifierValue(child));
}
catch (BooleanQuery.TooManyClauses ex)
{
throw new QueryNodeException(new MessageImpl(
QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
.MaxClauseCount, queryNode
.ToQueryString(new EscapeQuerySyntaxImpl())), ex);
}
}
}
}
return bQuery;
}
示例11: Decorate
/// <summary>
/// Decorates the specified query adding context information.
/// </summary>
/// <param name="query">The source query.</param>
/// <returns>The decorated query.</returns>
public Lucene.Net.Search.Query Decorate(Lucene.Net.Search.Query query)
{
BooleanQuery result = new BooleanQuery(true);
result.Add(query, Occur.MUST);
this.AddDecorations(result);
return result;
}
示例12: 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());
}
}
}
}
示例13: TestMethod
public virtual void TestMethod()
{
Directory directory = NewDirectory();
string[] values = new string[] { "1", "2", "3", "4" };
RandomIndexWriter writer = new RandomIndexWriter(Random(), directory, Similarity, TimeZone);
for (int i = 0; i < values.Length; i++)
{
Document doc = new Document();
doc.Add(NewStringField(FIELD, values[i], Field.Store.YES));
writer.AddDocument(doc);
}
IndexReader ir = writer.Reader;
writer.Dispose();
BooleanQuery booleanQuery1 = new BooleanQuery();
booleanQuery1.Add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
booleanQuery1.Add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
BooleanQuery query = new BooleanQuery();
query.Add(booleanQuery1, BooleanClause.Occur.MUST);
query.Add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
IndexSearcher indexSearcher = NewSearcher(ir);
ScoreDoc[] hits = indexSearcher.Search(query, null, 1000).ScoreDocs;
Assert.AreEqual(2, hits.Length, "Number of matched documents");
ir.Dispose();
directory.Dispose();
}
示例14: TestMethod
public virtual void TestMethod()
{
RAMDirectory directory = new RAMDirectory();
System.String[] values = new System.String[]{"1", "2", "3", "4"};
try
{
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
for (int i = 0; i < values.Length; i++)
{
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
writer.AddDocument(doc);
}
writer.Close();
BooleanQuery booleanQuery1 = new BooleanQuery();
booleanQuery1.Add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
booleanQuery1.Add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
BooleanQuery query = new BooleanQuery();
query.Add(booleanQuery1, BooleanClause.Occur.MUST);
query.Add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
IndexSearcher indexSearcher = new IndexSearcher(directory);
Hits hits = indexSearcher.Search(query);
Assert.AreEqual(2, hits.Length(), "Number of matched documents");
}
catch (System.IO.IOException e)
{
Assert.Fail(e.Message);
}
}
示例15: ParseQuery
private static Query ParseQuery(string searchTerm)
{
var fields = new Dictionary<string, float> { { "Id", 1.2f }, { "Title", 1.0f }, { "Tags", 1.0f}, { "Description", 0.8f }, { "Author", 0.6f } };
var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
searchTerm = QueryParser.Escape(searchTerm).ToLowerInvariant();
var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields.Keys.ToArray(), analyzer, fields);
var conjuctionQuery = new BooleanQuery();
conjuctionQuery.SetBoost(1.5f);
var disjunctionQuery = new BooleanQuery();
var wildCardQuery = new BooleanQuery();
wildCardQuery.SetBoost(0.7f);
var exactIdQuery = new TermQuery(new Term("Id-Exact", searchTerm));
exactIdQuery.SetBoost(2.5f);
foreach(var term in searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
conjuctionQuery.Add(queryParser.Parse(term), BooleanClause.Occur.MUST);
disjunctionQuery.Add(queryParser.Parse(term), 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);
}
}
return conjuctionQuery.Combine(new Query[] { exactIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery });
}