public LuceneQueryPredicateExpression(LuceneQueryFieldExpression field, Expression pattern, Occur occur, QueryType queryType) : base(typeof(bool), (ExpressionType)LuceneExpressionType.LuceneQueryPredicateExpression) { this.field = field; this.pattern = pattern; this.occur = occur; this.queryType = queryType; }
public LuceneRangeQueryExpression(LuceneQueryFieldExpression field, Expression lower, QueryType lowerQueryType, Expression upper, QueryType upperQueryType, Occur occur) : base(typeof(bool), (ExpressionType)LuceneExpressionType.LuceneRangeQueryExpression) { this.field = field; this.lower = lower; this.lowerQueryType = lowerQueryType; this.upper = upper; this.upperQueryType = upperQueryType; this.occur = occur; }
private static void AssertResult(LuceneQueryPredicateExpression result, Occur expectedOccur) { Assert.That(result, Is.Not.Null, "Expected LuceneQueryPredicateExpression to be returned."); Assert.That(result, Is.Not.SameAs(predicate)); Assert.That(result.QueryField, Is.SameAs(predicate.QueryField)); Assert.That(result.QueryPattern, Is.SameAs(predicate.QueryPattern)); Assert.That(result.QueryType, Is.EqualTo(predicate.QueryType)); Assert.That(result.Occur, Is.EqualTo(expectedOccur)); Assert.That(result.Boost, Is.EqualTo(predicate.Boost)); Assert.That(result.AllowSpecialCharacters, Is.EqualTo(predicate.AllowSpecialCharacters)); }
public static Filter CreateQuery(ISearchCriteria criteria, ISearchFilter filter, Occur clause) { var values = filter.GetValues(); if (values == null) return null; var query = new BooleanFilter(); foreach (var value in values) { var valueQuery = CreateQueryForValue(criteria, filter, value); query.Add(new FilterClause(valueQuery, Occur.SHOULD)); } return query; }
/// <summary> Parses a query, searching on the fields specified. Use this if you need /// to specify certain fields as required, and others as prohibited. /// <p/> /// /// <pre> /// Usage: /// <code> /// String[] fields = {"filename", "contents", "description"}; /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD, /// BooleanClause.Occur.MUST, /// BooleanClause.Occur.MUST_NOT}; /// MultiFieldQueryParser.parse("query", fields, flags, analyzer); /// </code> /// </pre> /// <p/> /// The code above would construct a query: /// /// <pre> /// <code> /// (filename:query) +(contents:query) -(description:query) /// </code> /// </pre> /// /// </summary> /// <param name="matchVersion">Lucene version to match; this is passed through to /// QueryParser. /// </param> /// <param name="query">Query string to parse /// </param> /// <param name="fields">Fields to search on /// </param> /// <param name="flags">Flags describing the fields /// </param> /// <param name="analyzer">Analyzer to use /// </param> /// <throws> ParseException </throws> /// <summary> if query parsing fails /// </summary> /// <throws> IllegalArgumentException </throws> /// <summary> if the length of the fields array differs from the length of /// the flags array /// </summary> public new static Query Parse(Lucene.Net.Util.Version matchVersion, string query, string[] fields, Occur[] flags, Analyzer analyzer) { if (fields.Length > flags.Length) throw new System.ArgumentException("fields.length != flags.length"); BooleanQuery bQuery = new BooleanQuery(); for (int i = 0; i < fields.Length; i++) { QueryParser qp = new HebrewQueryParser(matchVersion, fields[i], analyzer); Query q = qp.Parse(query); if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0)) { bQuery.Add(q, flags[i]); } } return bQuery; }
/// <summary> /// Adds the content sub query. /// </summary> /// <param name="query">The boolean query.</param> /// <param name="key">The field key.</param> /// <param name="value">The field value.</param> /// <param name="matchVariant">The match variant.</param> /// <param name="condition">The condition.</param> /// <param name="isFirst">if set to <c>true</c> [is first].</param> private void AddContentSubQuery(LuceneSearch.BooleanQuery query, string key, string value, MatchVariant matchVariant, QueryCondition condition) { if (matchVariant == MatchVariant.NotEquals) { query.Add(new LuceneSearch.TermQuery(new Term(key, value)), LuceneSearch.Occur.MUST_NOT); return; } LuceneSearch.Occur occurrence = this.GetOccur(condition); LuceneSearch.TermRangeQuery rangeQuery = this.GetRangeQuery(key, value, matchVariant); if (rangeQuery != null) { query.Add(rangeQuery, occurrence); return; } string[] keywords = value.Split(' '); if (keywords.Length > 1) { LuceneSearch.PhraseQuery phraseQuery = new Lucene.Net.Search.PhraseQuery(); foreach (string keyword in keywords) { phraseQuery.Add(new Term(key, keyword)); } query.Add(phraseQuery, occurrence); } else if (matchVariant == MatchVariant.Like) { query.Add(new LuceneSearch.WildcardQuery(new Term(key, value + "*")), occurrence); } else { query.Add(new LuceneSearch.TermQuery(new Term(key, value)), occurrence); } }
public virtual void SetOccur(Occur occur) { this.occur = occur; }
private void InitPendingClause() { _occur = Occur.SHOULD; _exactMatch = false; _query = null; _boost = 0; _asFilter = false; }
public ISearchBuilder Mandatory() { _occur = Occur.MUST; return this; }
private static void TagClause(BooleanQuery query, Analyzer analyzer, IEnumerable<string> values, Occur occur) { query.Add(ConstructClauseQuery(analyzer, "Tags", values, Occur.SHOULD, 2.0f), occur); }
private static void TitleClause(BooleanQuery query, Analyzer analyzer, IEnumerable<string> values, Occur occur) { if (occur == Occur.MUST) { BooleanQuery subQuery = new BooleanQuery(); query.Add(subQuery, Occur.MUST); query = subQuery; } query.Add(ConstructClauseQuery(analyzer, "Title", values, Occur.SHOULD), Occur.SHOULD); if (values.Count() > 1) { query.Add(ConstructClauseQuery(analyzer, "Title", values, Occur.MUST, 4.0f), Occur.SHOULD); } }
private void Combine(BooleanQuery target, BooleanQuery source, Occur occur) { if (source.GetClauses().Length == 1) { var clause = source.GetClauses().Single(); if (clause.IsProhibited && occur == Occur.SHOULD) { source = (BooleanQuery)source.Clone(); source.Add(new MatchAllDocsQuery(), Occur.SHOULD); target.Add(source, occur); return; } if (clause.Occur == Occur.MUST) { clause.Occur = occur; } target.Add(clause); } else { target.Add(source, occur); } }
/// <summary> /// Adds the simple query. /// </summary> /// <param name="query">The boolean query.</param> /// <param name="key">The field key.</param> /// <param name="value">The field value.</param> /// <param name="condition">The condition.</param> /// <param name="isFirst">if set to <c>true</c> [is first].</param> private void AddIdQuery(LuceneSearch.BooleanQuery query, string key, string value, QueryCondition condition) { value = this.GetItemPath(new ID(value)); LuceneSearch.Occur occurrence = this.GetOccur(condition); query.Add(new LuceneSearch.TermQuery(new Term(key, value)), occurrence); }
/// <summary> /// Builds the query. /// </summary> /// <param name="query">The result query.</param> /// <param name="node">The query node.</param> protected virtual void BuildQuery(LuceneSearch.BooleanQuery query, QueryNode node) { Query subQuery = node.Element as Query; if (subQuery != null && !subQuery.IsEmpty()) { LuceneSearch.BooleanQuery booleanQuery = new LuceneSearch.BooleanQuery(); if (!string.IsNullOrEmpty(subQuery.SearchRoot)) { this.AddSearchRoot(booleanQuery, subQuery.SearchRoot); } this.BuildQuery(booleanQuery, subQuery.FirstNode); LuceneSearch.Occur occurance = LuceneSearch.Occur.MUST; if (node.IsFirst) { if (!node.IsLast && (node.NextNode.Element is Condition)) { occurance = this.GetOccur(((Condition)node.NextNode.Element).QueryCondition); } } else { if (!node.IsFirst && (node.PreviousNode.Element is Condition)) { occurance = this.GetOccur(((Condition)node.PreviousNode.Element).QueryCondition); } } query.Add(booleanQuery, occurance); } else if (node.Element is AttributeQuery || node.Element is FieldQuery) { QueryCondition condition = QueryCondition.And; if (node.IsFirst) { if (!node.IsLast && (node.NextNode.Element is Condition)) { condition = ((Condition)node.NextNode.Element).QueryCondition; } } else { if (!node.IsFirst && (node.PreviousNode.Element is Condition)) { condition = ((Condition)node.PreviousNode.Element).QueryCondition; } } this.AddSubQuery(query, node.Element as SubQuery, condition); } if (!node.IsLast) { this.BuildQuery(query, node.NextNode); } this.resultQuery = query; }
private IndexQuery Boolean(Query query, Occur occur) { if (query == null) { return this; } if (_nativeQuery is MatchAllDocsQuery) { _nativeQuery = query; } else { var newQuery = new BooleanQuery(); newQuery.Add(_nativeQuery, occur); newQuery.Add(query, occur); _nativeQuery = newQuery; } return this; }
private void Combine(BooleanQuery target, BooleanQuery source, Occur occur) { if (source.GetClauses().Length == 1) { var clause = source.GetClauses()[0]; if (clause.Occur == Occur.MUST) { clause.Occur = occur; } target.Add(clause); } else { target.Add(source, occur); } }
/// <summary>Constructs a BooleanClause.</summary> public BooleanClause(Query query, Occur occur) { this.query = query; this.occur = occur; SetFields(occur); }
private static Query ConstructClauseQuery(Analyzer analyzer, string field, IEnumerable<string> values, Occur occur = Occur.SHOULD, float queryBoost = 1.0f, float termBoost = 1.0f) { BooleanQuery query = new BooleanQuery(); foreach (string text in values) { Query termQuery = ExecuteAnalyzer(analyzer, field, text); termQuery.Boost = termBoost; query.Add(termQuery, occur); } query.Boost = queryBoost; return query; }
public virtual void SetOccur(Occur occur) { this.occur = occur; SetFields(occur); }
private static void DescriptionClause(BooleanQuery query, Analyzer analyzer, IEnumerable<string> values, Occur occur) { query.Add(ConstructClauseQuery(analyzer, "Description", values), occur); }
private void SetFields(Occur occur) { if (occur == Occur.MUST) { required = true; prohibited = false; } else if (occur == Occur.SHOULD) { required = false; prohibited = false; } else if (occur == Occur.MUST_NOT) { required = false; prohibited = true; } else { throw new System.ArgumentException("Unknown operator " + occur); } }
private static void AuthorClause(BooleanQuery query, Analyzer analyzer, IEnumerable<string> values, Occur occur) { query.Add(ConstructClauseQuery(analyzer, "Authors", values), occur); }
/// <summary>Constructs a BooleanClause with query <code>q</code>, required /// <code>r</code> and prohibited <code>p</code>. /// </summary> /// <deprecated> use BooleanClause(Query, Occur) instead /// <ul> /// <li>For BooleanClause(query, true, false) use BooleanClause(query, BooleanClause.Occur.MUST) /// <li>For BooleanClause(query, false, false) use BooleanClause(query, BooleanClause.Occur.SHOULD) /// <li>For BooleanClause(query, false, true) use BooleanClause(query, BooleanClause.Occur.MUST_NOT) /// </ul> /// </deprecated> public BooleanClause(Query q, bool r, bool p) { // TODO: remove for Lucene 2.0 query = q; required = r; prohibited = p; if (required) { if (prohibited) { // prohibited && required doesn't make sense, but we want the old behaviour: occur = Occur.MUST_NOT; } else { occur = Occur.MUST; } } else { if (prohibited) { occur = Occur.MUST_NOT; } else { occur = Occur.SHOULD; } } }
public ISearchBuilder Forbidden() { _occur = Occur.MUST_NOT; return this; }
private void InitPendingClause() { _occur = Occur.SHOULD; _exactMatch = false; _query = null; _boost = 0; _asFilter = false; _sort = String.Empty; _comparer = 0; }
public LuceneQueryPredicateExpression(LuceneQueryFieldExpression field, Expression pattern, Occur occur) : this(field, pattern, occur, QueryType.Default) { }
/** * Create a new FilterClause * @param filter A Filter object containing a BitSet * @param occur A parameter implementation indicating SHOULD, MUST or MUST NOT */ public FilterClause(Filter filter, Occur occur) { this.occur = occur; this.filter = filter; }
/// <summary>Constructs a BooleanClause.</summary> public BooleanClause(Query query, Occur occur) { this.query = query; this.occur = occur; }
private static Occur Negate(Occur occur) { return (occur == Occur.MUST_NOT) ? Occur.MUST : Occur.MUST_NOT; }
/* * Makes a boolean query based upon a collection of queries and a logical operator. * * @param queries the query collection * @param occur the logical operator * @return the query */ BooleanQuery MakeQuery(Query[] queries, Occur occur) { var bq = new BooleanQuery(); foreach (Query query in queries) { bq.Add(query, occur); } return bq; }
private void AssertLuceneQueryExpression(Expression expression, string expectedQueryFieldName, ConstantExpression expectedPatternExpression, QueryType expectedQueryType, Occur expectedOccur) { Assert.That(expression, Is.InstanceOf<LuceneQueryPredicateExpression>()); var result = (LuceneQueryPredicateExpression)expression; Assert.That(result.Occur, Is.EqualTo(expectedOccur)); Assert.That(result.QueryType, Is.EqualTo(expectedQueryType)); Assert.That(result.QueryField.FieldName, Is.EqualTo(expectedQueryFieldName)); Assert.That(result.QueryPattern, Is.EqualTo(expectedPatternExpression)); }