/// <summary> /// Takes the manaually generated query and runs it through a Lucene analyzer /// </summary> /// <param name="analyzer">Analyzer to use when parsing this query</param> /// <param name="occurrence">Occurrence type of this query</param> internal void Analyze(Lucene29.Net.Analysis.Analyzer analyzer, ClauseOccurrence occurrence) { if (analyzer == null) { throw new ArgumentNullException("analyzer", "Analyzer cannot be null"); } try { AnalyzerType requestedType = TypeConverter.GetAnalyzerType(analyzer); if (cachedAnalyzer != requestedType) { lock (syncRoot) { if (cachedAnalyzer != requestedType) { cachedParser = new Lucene29.Net.QueryParsers.QueryParser(StaticValues.LibraryVersion, "Analyzer", analyzer); cachedAnalyzer = requestedType; cachedParser.SetAllowLeadingWildcard(this.allowLeadingWildcard); } } } Query query = cachedParser.Parse(this.luceneQuery.ToString()); this.luceneQuery = null; this.luceneQuery = new BooleanQuery(this.disableCoord); this.luceneQuery.Add(query, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); } catch (Exception ex) { throw new FormatException("There was an unexpected exception thrown during the analyzing process of the instance.", ex); } }
/// <summary> /// Adds a standard type clause to this instance /// </summary> /// <param name="term">Term to add to this query.</param> /// <param name="occurrence">Defines how the term is added to this query.</param> /// <param name="slop">The amount of allowed slop in a phrase query.</param> /// <remarks> /// Slop is the amount of movement each word is allowed in a non-exact phrase query. /// For instance if you search for "Adobe Systems Incorporated" and the slop is set to 0 then /// only results with that term is allowed. If you set the slop to 2 then two movements can be /// made, max, for each word. In the same example with slop set to 2 results would be returned /// for "Adobe Systems Incorporated", "Adobe Incorporated Systems", "Systems Adobe Incorporated", /// and "Systems Incorporated Adobe". /// </remarks> public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence, int slop) { if (term == null) { throw new ArgumentNullException("term", "term cannot be null"); } IncrementTotalClauses(1); if (term.IsPhrase) { PhraseQuery phraseQuery = new PhraseQuery(); phraseQuery.Add(term.GetLuceneTerm()); phraseQuery.SetSlop(slop); phraseQuery.SetBoost(term.Boost); this.luceneQuery.Add(phraseQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); phraseQuery = null; } else { TermQuery termQuery = new TermQuery(term.GetLuceneTerm()); termQuery.SetBoost(term.Boost); this.luceneQuery.Add(termQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); termQuery = null; } }
/// <summary> /// Takes the manaually generated query and runs it through a specified Lucene analyzer /// </summary> /// <param name="analyzerType">Lucene analyzer to run current query through</param> /// <param name="occurrence">Occurrence type of this query</param> public void Analyze(AnalyzerType analyzerType, ClauseOccurrence occurrence) { if (analyzerType == AnalyzerType.None) { throw new ArgumentException("analyzerType cannot be set to None", "analyzerType"); } if (analyzerType == AnalyzerType.Unknown) { throw new ArgumentException("analyzerType cannot be set to Unknown", "analyzerType"); } Analyze(TypeConverter.GetAnalyzer(analyzerType), occurrence); }
/// <summary> /// Appends the clauses from the specified builders into this one. /// </summary> /// <param name="builders">The builders.</param> /// <param name="occurrence">The occurrence.</param> /// /// <remarks> /// Append is simply a way of adding another query to this boolean query /// </remarks> public void Append(QueryBuilder[] builders, ClauseOccurrence occurrence) { if (builders == null) { throw new ArgumentNullException("builders", "builders cannot be null"); } int totalBuilders = builders.Length; for (int i = 0; i < totalBuilders; i++) { Append(builders[i], occurrence); } }
/// <summary> /// Adds a fuzzy clause to this instance /// </summary> /// <remarks>Fuzzy clauses find results within a particular relevance distance of each hit</remarks> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="minimumSimilarity">Defines the amount of similarity that is allowed between matches</param> public void AddFuzzyClause(SearchTerm term, ClauseOccurrence occurrence, float minimumSimilarity) { if (term == null) { throw new ArgumentNullException("term", "term cannot be null"); } IncrementTotalClauses(1); FuzzyQuery fuzzyQuery = new FuzzyQuery(term.GetLuceneTerm(), minimumSimilarity); fuzzyQuery.SetBoost(term.Boost); this.luceneQuery.Add(fuzzyQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); fuzzyQuery = null; }
/// <summary> /// Adds a wildcard clause to this instance /// </summary> /// <remarks>Wildcard clauses include '*' and '?' character</remarks> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddWildcardClause(SearchTerm term, ClauseOccurrence occurrence) { if (term == null) { throw new ArgumentNullException("term", "term cannot be null"); } IncrementTotalClauses(1); WildcardQuery wildcardQuery = new WildcardQuery(term.GetLuceneTerm()); wildcardQuery.SetBoost(term.Boost); this.luceneQuery.Add(wildcardQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); wildcardQuery = null; }
/// <summary> /// Converts a <see cref="IndexLibrary.ClauseOccurrence"/> to the Lucene equivalent object, <c>BooleanClause.Occur</c> /// </summary> /// <param name="occurrence">The <c>ClauseOccurrence</c> to convert into a Lucene object</param> /// <returns>A Lucene <c>BooleanClause.Occur</c> that is equivalent to the specified <c>ClauseOccurrence</c></returns> public static BooleanClause.Occur ConvertToLuceneClauseOccurrence(ClauseOccurrence occurrence) { switch (occurrence) { case ClauseOccurrence.MustNotOccur: return BooleanClause.Occur.MUST_NOT; case ClauseOccurrence.MustOccur: return BooleanClause.Occur.MUST; case ClauseOccurrence.Default: case ClauseOccurrence.ShouldOccur: default: return BooleanClause.Occur.SHOULD; } }
/// <summary> /// Converts a <see cref="IndexLibrary.ClauseOccurrence"/> to the Lucene equivalent object, <c>BooleanClause.Occur</c> /// </summary> /// <param name="occurrence">The <c>ClauseOccurrence</c> to convert into a Lucene object</param> /// <returns>A Lucene <c>BooleanClause.Occur</c> that is equivalent to the specified <c>ClauseOccurrence</c></returns> public static BooleanClause.Occur ConvertToLuceneClauseOccurrence(ClauseOccurrence occurrence) { switch (occurrence) { case ClauseOccurrence.MustNotOccur: return(BooleanClause.Occur.MUST_NOT); case ClauseOccurrence.MustOccur: return(BooleanClause.Occur.MUST); case ClauseOccurrence.Default: case ClauseOccurrence.ShouldOccur: default: return(BooleanClause.Occur.SHOULD); } }
/// <summary> /// Adds a range clause to this instance /// </summary> /// <remarks>Adds a range to search into the query</remarks> /// <remarks>All values specified in the startTerm are applied to this entire clause</remarks> /// <param name="startTerm">Starting half of the range to add to this query</param> /// <param name="endValue">Ending half of the range to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> public void AddTermRangeClause(SearchTerm startTerm, string endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue) { if (startTerm == null) { throw new ArgumentNullException("startTerm", "startTerm cannot be null"); } if (string.IsNullOrEmpty(endValue)) { throw new ArgumentNullException("endValue", "endValue cannot be null or empty"); } IncrementTotalClauses(1); TermRangeQuery rangeQuery = new TermRangeQuery(startTerm.FieldName, startTerm.FieldValue, endValue, includeStartValue, includeEndValue); rangeQuery.SetBoost(startTerm.Boost); this.luceneQuery.Add(rangeQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); rangeQuery = null; }
/// <summary> /// Appends the clauses from the specified builder into this one. /// </summary> /// <param name="builder">The builder.</param> /// <param name="occurrence">The occurrence.</param> /// /// <remarks> /// Append is simply a way of adding another query to this boolean query /// </remarks> public void Append(QueryBuilder builder, ClauseOccurrence occurrence) { if (builder == null) { throw new ArgumentNullException("builder", "QueryBuilder cannot be null"); } if (builder.luceneQuery == null) { throw new ArgumentException("QueryBuilder is not valid", "builder"); } if (builder.TotalClauses == 0) { return; } IncrementTotalClauses(builder.totalClauses); this.luceneQuery.Add(builder.luceneQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); }
/// <summary> /// Adds the string query. /// </summary> /// <param name="queryText">The query text.</param> /// <param name="occurrence">The occurrence.</param> /// <param name="analyzerType">Type of the analyzer.</param> /// <param name="merge">if set to <c>true</c> [merge].</param> /// <returns>True if query string is parsed and appended successfully</returns> public bool AddStringQuery(string queryText, ClauseOccurrence occurrence, AnalyzerType analyzerType, bool merge) { if (string.IsNullOrEmpty(queryText)) { throw new ArgumentNullException("queryText", "queryText cannot be null or empty"); } // this try catch is here to protect you from lucene specific exceptions bool success = true; IncrementTotalClauses(1); try { Lucene29.Net.QueryParsers.QueryParser parser = new Lucene29.Net.QueryParsers.QueryParser(StaticValues.LibraryVersion, "QueryParser", TypeConverter.GetAnalyzer(analyzerType)); Query query = parser.Parse(queryText); if (query == null) { success = false; } else { if (merge) { this.luceneQuery.Combine(new Query[] { query }); } else { this.luceneQuery.Add(query, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); } } } catch (Exception) { //System.Diagnostics.Debug.WriteLine("Lucene exception -> " + ex.Message); success = false; this.totalClauses--; } return(success); }
/// <summary> /// Adds an integer range clause to this instance. /// </summary> /// <param name="fieldName">Name of the field to add.</param> /// <param name="startValue">The start value of the range to search.</param> /// <param name="endValue">The end value of the range to search.</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddIntRangeClause(string fieldName, int startValue, int endValue, ClauseOccurrence occurrence) { this.AddIntRangeClause(fieldName, startValue, endValue, occurrence, true, true, StaticValues.DefaultBoost); }
/// <summary> /// Takes the manaually generated query and runs it through a Lucene analyzer /// </summary> /// <param name="analyzer">Analyzer to use when parsing this query</param> /// <param name="occurrence">Occurrence type of this query</param> internal void Analyze(Lucene29.Net.Analysis.Analyzer analyzer, ClauseOccurrence occurrence) { if (analyzer == null) throw new ArgumentNullException("analyzer", "Analyzer cannot be null"); try { AnalyzerType requestedType = TypeConverter.GetAnalyzerType(analyzer); if (cachedAnalyzer != requestedType) { lock (syncRoot) { if (cachedAnalyzer != requestedType) { cachedParser = new Lucene29.Net.QueryParsers.QueryParser(StaticValues.LibraryVersion, "Analyzer", analyzer); cachedAnalyzer = requestedType; cachedParser.SetAllowLeadingWildcard(this.allowLeadingWildcard); } } } Query query = cachedParser.Parse(this.luceneQuery.ToString()); this.luceneQuery = null; this.luceneQuery = new BooleanQuery(this.disableCoord); this.luceneQuery.Add(query, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); } catch (Exception ex) { throw new FormatException("There was an unexpected exception thrown during the analyzing process of the instance.", ex); } }
/// <summary> /// Appends the clauses from the specified builder into this one. /// </summary> /// <param name="builder">The builder.</param> /// <param name="occurrence">The occurrence.</param> /// /// <remarks> /// Append is simply a way of adding another query to this boolean query /// </remarks> public void Append(QueryBuilder builder, ClauseOccurrence occurrence) { if (builder == null) throw new ArgumentNullException("builder", "QueryBuilder cannot be null"); if (builder.luceneQuery == null) throw new ArgumentException("QueryBuilder is not valid", "builder"); if (builder.TotalClauses == 0) return; IncrementTotalClauses(builder.totalClauses); this.luceneQuery.Add(builder.luceneQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); }
/// <summary> /// Adds a wildcard clause to this instance /// </summary> /// <remarks>Wildcard clauses include '*' and '?' character</remarks> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddWildcardClause(SearchTerm term, ClauseOccurrence occurrence) { if (term == null) throw new ArgumentNullException("term", "term cannot be null"); IncrementTotalClauses(1); WildcardQuery wildcardQuery = new WildcardQuery(term.GetLuceneTerm()); wildcardQuery.SetBoost(term.Boost); this.luceneQuery.Add(wildcardQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); wildcardQuery = null; }
/// <summary> /// Adds a range clause to this instance /// </summary> /// <remarks>Adds a range to search into the query</remarks> /// <remarks>All values specified in the startTerm are applied to this entire clause</remarks> /// <param name="startTerm">Starting half of the range to add to this query</param> /// <param name="endValue">Ending half of the range to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddTermRangeClause(SearchTerm startTerm, string endValue, ClauseOccurrence occurrence) { this.AddTermRangeClause(startTerm, endValue, occurrence, true, true); }
/// <summary> /// Adds the string query. /// </summary> /// <param name="queryText">The query text.</param> /// <param name="occurrence">The occurrence.</param> /// <param name="analyzerType">Type of the analyzer.</param> /// <returns>True if query string is parsed and appended successfully</returns> public bool AddStringQuery(string queryText, ClauseOccurrence occurrence, AnalyzerType analyzerType) { return AddStringQuery(queryText, occurrence, analyzerType, this.totalClauses > 0); }
/// <summary> /// Adds a fuzzy clause to this instance /// </summary> /// <remarks>Fuzzy clauses find results within a particular relevance distance of each hit</remarks> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddFuzzyClause(SearchTerm term, ClauseOccurrence occurrence) { this.AddFuzzyClause(term, occurrence, FuzzyQuery.defaultMinSimilarity); }
/// <summary> /// Adds a long range clause to this instance. /// </summary> /// <param name="fieldName">Name of the field to add.</param> /// <param name="startValue">The start value of the range to search.</param> /// <param name="endValue">The end value of the range to search.</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> public void AddLongRangeClause(string fieldName, long startValue, long endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue) { this.AddLongRangeClause(fieldName, startValue, endValue, occurrence, includeStartValue, includeEndValue, StaticValues.DefaultBoost); }
/// <summary> /// Adds a long range clause to this instance. /// </summary> /// <param name="fieldName">Name of the field to add.</param> /// <param name="startValue">The start value of the range to search.</param> /// <param name="endValue">The end value of the range to search.</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> /// <param name="boost">The amount of boost to apply to this term</param> public void AddLongRangeClause(string fieldName, long startValue, long endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue, float boost) { if (string.IsNullOrEmpty(fieldName)) { throw new ArgumentNullException("fieldName", "fieldName cannot be null or empty"); } if (boost < StaticValues.MinimumAllowedBoost) { throw new ArgumentOutOfRangeException("boost", "boost cannot be less than " + StaticValues.MinimumAllowedBoost); } IncrementTotalClauses(1); NumericRangeQuery rangeQuery = NumericRangeQuery.NewLongRange(fieldName, startValue, endValue, includeStartValue, includeEndValue); rangeQuery.SetBoost(boost); this.luceneQuery.Add(rangeQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); rangeQuery = null; }
private static QueryBuilder CreateBuilderFromExpression(string expression, out ClauseOccurrence occurrence) { // FirstName# = 'Tim // +(LastName+ = 'James', Type+ = '0') occurrence = ClauseOccurrence.Default; if (string.IsNullOrEmpty(expression)) { return(new QueryBuilder()); } QueryBuilder builder = new QueryBuilder(); if (expression.StartsWith("+")) { occurrence = ClauseOccurrence.MustOccur; expression = expression.Substring(1); } else if (expression.StartsWith("#")) { occurrence = ClauseOccurrence.ShouldOccur; expression = expression.Substring(1); } else if (expression.StartsWith("-")) { occurrence = ClauseOccurrence.MustNotOccur; expression = expression.Substring(1); } if (expression.StartsWith("(")) { expression = expression.Substring(1); } if (expression.EndsWith(")")) { expression = expression.Substring(0, expression.Length - 1); } string[] parts = expression.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { string searchTerm; string boolOperator; string searchValue; ClauseOccurrence co = ClauseOccurrence.Default; string[] words = part.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (words.Length < 3) { throw new ArgumentNullException("expression", "Malformed expression"); } searchTerm = words[0]; boolOperator = words[1]; searchValue = string.Join(" ", words, 2, words.Length - 2).Replace("\'", ""); if (searchTerm.EndsWith("+")) { co = ClauseOccurrence.MustOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } else if (searchTerm.EndsWith("#")) { co = ClauseOccurrence.ShouldOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } else if (searchTerm.EndsWith("-")) { co = ClauseOccurrence.MustNotOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } if (boolOperator.Equals("=")) { builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co); } else if (boolOperator.Equals(">")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals(">=")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("<")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("<=")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("!=") || boolOperator.Equals("<>")) { if (co == ClauseOccurrence.MustNotOccur) { co = ClauseOccurrence.MustOccur; } else if (co == ClauseOccurrence.MustOccur) { co = ClauseOccurrence.MustNotOccur; } builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co); } else { throw new ArgumentException("Malformed boolean operator in expression"); } } return(builder); }
/// <summary> /// Adds a long range clause to this instance. /// </summary> /// <param name="fieldName">Name of the field to add.</param> /// <param name="startValue">The start value of the range to search.</param> /// <param name="endValue">The end value of the range to search.</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> /// <param name="boost">The amount of boost to apply to this term</param> public void AddLongRangeClause(string fieldName, long startValue, long endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue, float boost) { if (string.IsNullOrEmpty(fieldName)) throw new ArgumentNullException("fieldName", "fieldName cannot be null or empty"); if (boost < StaticValues.MinimumAllowedBoost) throw new ArgumentOutOfRangeException("boost", "boost cannot be less than " + StaticValues.MinimumAllowedBoost); IncrementTotalClauses(1); NumericRangeQuery rangeQuery = NumericRangeQuery.NewLongRange(fieldName, startValue, endValue, includeStartValue, includeEndValue); rangeQuery.SetBoost(boost); this.luceneQuery.Add(rangeQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); rangeQuery = null; }
private static QueryBuilder CreateBuilderFromExpression(string expression, out ClauseOccurrence occurrence) { // FirstName# = 'Tim // +(LastName+ = 'James', Type+ = '0') occurrence = ClauseOccurrence.Default; if (string.IsNullOrEmpty(expression)) return new QueryBuilder(); QueryBuilder builder = new QueryBuilder(); if (expression.StartsWith("+")) { occurrence = ClauseOccurrence.MustOccur; expression = expression.Substring(1); } else if (expression.StartsWith("#")) { occurrence = ClauseOccurrence.ShouldOccur; expression = expression.Substring(1); } else if (expression.StartsWith("-")) { occurrence = ClauseOccurrence.MustNotOccur; expression = expression.Substring(1); } if (expression.StartsWith("(")) expression = expression.Substring(1); if (expression.EndsWith(")")) expression = expression.Substring(0, expression.Length - 1); string[] parts = expression.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { string searchTerm; string boolOperator; string searchValue; ClauseOccurrence co = ClauseOccurrence.Default; string[] words = part.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (words.Length < 3) throw new ArgumentNullException("expression", "Malformed expression"); searchTerm = words[0]; boolOperator = words[1]; searchValue = string.Join(" ", words, 2, words.Length - 2).Replace("\'", ""); if (searchTerm.EndsWith("+")) { co = ClauseOccurrence.MustOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } else if (searchTerm.EndsWith("#")) { co = ClauseOccurrence.ShouldOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } else if (searchTerm.EndsWith("-")) { co = ClauseOccurrence.MustNotOccur; searchTerm = searchTerm.Substring(0, searchTerm.Length - 1); } if (boolOperator.Equals("=")) { builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co); } else if (boolOperator.Equals(">")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals(">=")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("<")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("<=")) { throw new NotSupportedException("Boolean operator not yet supported"); } else if (boolOperator.Equals("!=") || boolOperator.Equals("<>")) { if (co == ClauseOccurrence.MustNotOccur) co = ClauseOccurrence.MustOccur; else if (co == ClauseOccurrence.MustOccur) co = ClauseOccurrence.MustNotOccur; builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co); } else { throw new ArgumentException("Malformed boolean operator in expression"); } } return builder; }
/// <summary> /// Adds the string query. /// </summary> /// <param name="queryText">The query text.</param> /// <param name="occurrence">The occurrence.</param> /// <param name="analyzerType">Type of the analyzer.</param> /// <param name="merge">if set to <c>true</c> [merge].</param> /// <returns>True if query string is parsed and appended successfully</returns> public bool AddStringQuery(string queryText, ClauseOccurrence occurrence, AnalyzerType analyzerType, bool merge) { if (string.IsNullOrEmpty(queryText)) throw new ArgumentNullException("queryText", "queryText cannot be null or empty"); // this try catch is here to protect you from lucene specific exceptions bool success = true; IncrementTotalClauses(1); try { Lucene29.Net.QueryParsers.QueryParser parser = new Lucene29.Net.QueryParsers.QueryParser(StaticValues.LibraryVersion, "QueryParser", TypeConverter.GetAnalyzer(analyzerType)); Query query = parser.Parse(queryText); if (query == null) { success = false; } else { if (merge) this.luceneQuery.Combine(new Query[] { query }); else this.luceneQuery.Add(query, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); } } catch (Exception) { //System.Diagnostics.Debug.WriteLine("Lucene exception -> " + ex.Message); success = false; this.totalClauses--; } return success; }
/// <summary> /// Adds a float range clause to this instance. /// </summary> /// <param name="fieldName">Name of the field to add.</param> /// <param name="startValue">The start value of the range to search.</param> /// <param name="endValue">The end value of the range to search.</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> public void AddFloatRangeClause(string fieldName, float startValue, float endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue) { AddFloatRangeClause(fieldName, startValue, endValue, occurrence, includeStartValue, includeEndValue, StaticValues.DefaultBoost); }
/// <summary> /// Adds a range clause to this instance /// </summary> /// <remarks>Adds a range to search into the query</remarks> /// <remarks>All values specified in the startTerm are applied to this entire clause</remarks> /// <param name="startTerm">Starting half of the range to add to this query</param> /// <param name="endValue">Ending half of the range to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="includeStartValue">Determines if the starting term is included or excluded in the search results</param> /// <param name="includeEndValue">Determines if the ending term is included or excluded in the search results</param> public void AddTermRangeClause(SearchTerm startTerm, string endValue, ClauseOccurrence occurrence, bool includeStartValue, bool includeEndValue) { if (startTerm == null) throw new ArgumentNullException("startTerm", "startTerm cannot be null"); if (string.IsNullOrEmpty(endValue)) throw new ArgumentNullException("endValue", "endValue cannot be null or empty"); IncrementTotalClauses(1); TermRangeQuery rangeQuery = new TermRangeQuery(startTerm.FieldName, startTerm.FieldValue, endValue, includeStartValue, includeEndValue); rangeQuery.SetBoost(startTerm.Boost); this.luceneQuery.Add(rangeQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); rangeQuery = null; }
/// <summary> /// Takes the manaually generated query and runs it through a specified Lucene analyzer /// </summary> /// <param name="analyzerType">Lucene analyzer to run current query through</param> /// <param name="occurrence">Occurrence type of this query</param> public void Analyze(AnalyzerType analyzerType, ClauseOccurrence occurrence) { if (analyzerType == AnalyzerType.None) throw new ArgumentException("analyzerType cannot be set to None", "analyzerType"); if (analyzerType == AnalyzerType.Unknown) throw new ArgumentException("analyzerType cannot be set to Unknown", "analyzerType"); Analyze(TypeConverter.GetAnalyzer(analyzerType), occurrence); }
/// <summary> /// Appends the clauses from the specified builders into this one. /// </summary> /// <param name="builders">The builders.</param> /// <param name="occurrence">The occurrence.</param> /// /// <remarks> /// Append is simply a way of adding another query to this boolean query /// </remarks> public void Append(QueryBuilder[] builders, ClauseOccurrence occurrence) { if (builders == null) throw new ArgumentNullException("builders", "builders cannot be null"); int totalBuilders = builders.Length; for (int i = 0; i < totalBuilders; i++) Append(builders[i], occurrence); }
/// <summary> /// Adds a fuzzy clause to this instance /// </summary> /// <remarks>Fuzzy clauses find results within a particular relevance distance of each hit</remarks> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> /// <param name="minimumSimilarity">Defines the amount of similarity that is allowed between matches</param> public void AddFuzzyClause(SearchTerm term, ClauseOccurrence occurrence, float minimumSimilarity) { if (term == null) throw new ArgumentNullException("term", "term cannot be null"); IncrementTotalClauses(1); FuzzyQuery fuzzyQuery = new FuzzyQuery(term.GetLuceneTerm(), minimumSimilarity); fuzzyQuery.SetBoost(term.Boost); this.luceneQuery.Add(fuzzyQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); fuzzyQuery = null; }
/// <summary> /// Adds a standard type clause to this instance /// </summary> /// <param name="term">Term to add to this query</param> /// <param name="occurrence">Defines how the term is added to this query</param> public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence) { this.AddBooleanClause(term, occurrence, StaticValues.DefaultSlop); }
/// <summary> /// Adds the string query. /// </summary> /// <param name="queryText">The query text.</param> /// <param name="occurrence">The occurrence.</param> /// <param name="analyzerType">Type of the analyzer.</param> /// <returns>True if query string is parsed and appended successfully</returns> public bool AddStringQuery(string queryText, ClauseOccurrence occurrence, AnalyzerType analyzerType) { return(AddStringQuery(queryText, occurrence, analyzerType, this.totalClauses > 0)); }
/// <summary> /// Adds a standard type clause to this instance /// </summary> /// <param name="term">Term to add to this query.</param> /// <param name="occurrence">Defines how the term is added to this query.</param> /// <param name="slop">The amount of allowed slop in a phrase query.</param> /// <remarks> /// Slop is the amount of movement each word is allowed in a non-exact phrase query. /// For instance if you search for "Adobe Systems Incorporated" and the slop is set to 0 then /// only results with that term is allowed. If you set the slop to 2 then two movements can be /// made, max, for each word. In the same example with slop set to 2 results would be returned /// for "Adobe Systems Incorporated", "Adobe Incorporated Systems", "Systems Adobe Incorporated", /// and "Systems Incorporated Adobe". /// </remarks> public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence, int slop) { if (term == null) throw new ArgumentNullException("term", "term cannot be null"); IncrementTotalClauses(1); if (term.IsPhrase) { PhraseQuery phraseQuery = new PhraseQuery(); phraseQuery.Add(term.GetLuceneTerm()); phraseQuery.SetSlop(slop); phraseQuery.SetBoost(term.Boost); this.luceneQuery.Add(phraseQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); phraseQuery = null; } else { TermQuery termQuery = new TermQuery(term.GetLuceneTerm()); termQuery.SetBoost(term.Boost); this.luceneQuery.Add(termQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); termQuery = null; } }