public static Query NotEqual(string fieldName, LuceneTermType termType, string value, bool exact) { return(new BooleanQuery { { new MatchAllDocsQuery(), Occur.MUST }, { Term(fieldName, value, termType, exact: exact), Occur.MUST_NOT } }); }
public static Query NotEqual(string fieldName, LuceneTermType termType, double value) { return(new BooleanQuery { { Term(fieldName, Asterisk, LuceneTermType.WildCard), Occur.MUST }, { CreateRange(fieldName, value, true, value, true), Occur.MUST_NOT } }); }
public static Query NotEqual(string fieldName, LuceneTermType termType, string value, bool exact) { return(new BooleanQuery { { Term(fieldName, Asterisk, LuceneTermType.WildCard), Occur.MUST }, { Term(fieldName, value, termType, exact: exact), Occur.MUST_NOT } }); }
public static Query NotEqual(string fieldName, LuceneTermType termType, double value) { return(new BooleanQuery { { new MatchAllDocsQuery(), Occur.MUST }, { CreateRange(fieldName, value, true, value, true), Occur.MUST_NOT } }); }
private static Query CreateRange(string fieldName, string minValue, LuceneTermType minValueType, bool inclusiveMin, string maxValue, LuceneTermType maxValueType, bool inclusiveMax, bool exact) { var minTermIsNullOrStar = minValueType == LuceneTermType.Null || minValue.Equals(Asterisk); var maxTermIsNullOrStar = maxValueType == LuceneTermType.Null || maxValue.Equals(Asterisk); if (minTermIsNullOrStar && maxTermIsNullOrStar) { return(new WildcardQuery(new Term(fieldName, Asterisk))); } return(new TermRangeQuery(fieldName, minTermIsNullOrStar ? null : GetTermValue(minValue, minValueType, exact), maxTermIsNullOrStar ? null : GetTermValue(maxValue, maxValueType, exact), inclusiveMin, inclusiveMax)); }
public static Query Term(string fieldName, string term, LuceneTermType type, float?boost = null, float?similarity = null, bool exact = false) { if (boost.HasValue == false) { boost = 1; } if (type == LuceneTermType.Double || type == LuceneTermType.Long) { return new TermQuery(new Term(fieldName, term)) { Boost = boost.Value } } ; term = GetTermValue(term, type, exact); if (type == LuceneTermType.WildCard) { return(new WildcardQuery(new Term(fieldName, term)) { Boost = boost.Value }); } if (type == LuceneTermType.Prefix) { var actualTerm = term[term.Length - 1] == AsteriskChar?term.Substring(0, term.Length - 1) : term; return(new PrefixQuery(new Term(fieldName, actualTerm)) { Boost = boost.Value }); } return(new TermQuery(new Term(fieldName, term)) { Boost = boost.Value }); }
public static unsafe string GetTermValue(string value, LuceneTermType type, bool exact) { switch (type) { case LuceneTermType.Double: case LuceneTermType.Long: return(value); default: { if (value == null) { return(Constants.Documents.Indexing.Fields.NullValue); } if (value == string.Empty) { return(Constants.Documents.Indexing.Fields.EmptyString); } if (exact) return(value); fixed(char *pValue = value) { var result = LazyStringParser.TryParseDateTime(pValue, value.Length, out DateTime _, out DateTimeOffset _); switch (result) { case LazyStringParser.Result.DateTime: case LazyStringParser.Result.DateTimeOffset: return(value); default: return(value.ToLowerInvariant()); } } } } }
public static Query Between(string fieldName, LuceneTermType termType, double fromValue, double toValue) { return(CreateRange(fieldName, fromValue, true, toValue, true)); }
private static Lucene.Net.Search.Query ToLuceneQuery(JsonOperationContext context, Query query, QueryExpression expression, QueryMetadata metadata, BlittableJsonReaderObject parameters, Analyzer analyzer, Func <string, SpatialField> getSpatialField, bool exact = false) { if (expression == null) { return(new MatchAllDocsQuery()); } if (expression is BinaryExpression where) { switch (where.Operator) { case OperatorType.Equal: case OperatorType.NotEqual: case OperatorType.GreaterThan: case OperatorType.LessThan: case OperatorType.LessThanEqual: case OperatorType.GreaterThanEqual: { var fieldName = ExtractIndexFieldName(query, parameters, where.Left, metadata); var(value, valueType) = GetValue(fieldName, query, metadata, parameters, where.Right); var(luceneFieldName, fieldType, termType) = GetLuceneField(fieldName, valueType); switch (fieldType) { case LuceneFieldType.String: var valueAsString = GetValueAsString(value); if (exact && metadata.IsDynamic) { luceneFieldName = AutoIndexField.GetExactAutoIndexFieldName(luceneFieldName); } switch (where.Operator) { case OperatorType.Equal: return(LuceneQueryHelper.Equal(luceneFieldName, termType, valueAsString, exact)); case OperatorType.NotEqual: return(LuceneQueryHelper.NotEqual(luceneFieldName, termType, valueAsString, exact)); case OperatorType.LessThan: return(LuceneQueryHelper.LessThan(luceneFieldName, termType, valueAsString, exact)); case OperatorType.GreaterThan: return(LuceneQueryHelper.GreaterThan(luceneFieldName, termType, valueAsString, exact)); case OperatorType.LessThanEqual: return(LuceneQueryHelper.LessThanOrEqual(luceneFieldName, termType, valueAsString, exact)); case OperatorType.GreaterThanEqual: return(LuceneQueryHelper.GreaterThanOrEqual(luceneFieldName, termType, valueAsString, exact)); } break; case LuceneFieldType.Long: var valueAsLong = (long)value; switch (where.Operator) { case OperatorType.Equal: return(LuceneQueryHelper.Equal(luceneFieldName, termType, valueAsLong)); case OperatorType.NotEqual: return(LuceneQueryHelper.NotEqual(luceneFieldName, termType, valueAsLong)); case OperatorType.LessThan: return(LuceneQueryHelper.LessThan(luceneFieldName, termType, valueAsLong)); case OperatorType.GreaterThan: return(LuceneQueryHelper.GreaterThan(luceneFieldName, termType, valueAsLong)); case OperatorType.LessThanEqual: return(LuceneQueryHelper.LessThanOrEqual(luceneFieldName, termType, valueAsLong)); case OperatorType.GreaterThanEqual: return(LuceneQueryHelper.GreaterThanOrEqual(luceneFieldName, termType, valueAsLong)); } break; case LuceneFieldType.Double: var valueAsDouble = (double)value; switch (where.Operator) { case OperatorType.Equal: return(LuceneQueryHelper.Equal(luceneFieldName, termType, valueAsDouble)); case OperatorType.NotEqual: return(LuceneQueryHelper.NotEqual(luceneFieldName, termType, valueAsDouble)); case OperatorType.LessThan: return(LuceneQueryHelper.LessThan(luceneFieldName, termType, valueAsDouble)); case OperatorType.GreaterThan: return(LuceneQueryHelper.GreaterThan(luceneFieldName, termType, valueAsDouble)); case OperatorType.LessThanEqual: return(LuceneQueryHelper.LessThanOrEqual(luceneFieldName, termType, valueAsDouble)); case OperatorType.GreaterThanEqual: return(LuceneQueryHelper.GreaterThanOrEqual(luceneFieldName, termType, valueAsDouble)); } break; default: throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, null); } throw new NotSupportedException("Should not happen!"); } case OperatorType.And: case OperatorType.AndNot: var andPrefix = where.Operator == OperatorType.AndNot ? LucenePrefixOperator.Minus : LucenePrefixOperator.None; return(LuceneQueryHelper.And( ToLuceneQuery(context, query, where.Left, metadata, parameters, analyzer, getSpatialField, exact), LucenePrefixOperator.None, ToLuceneQuery(context, query, where.Right, metadata, parameters, analyzer, getSpatialField, exact), andPrefix)); case OperatorType.Or: case OperatorType.OrNot: var orPrefix = where.Operator == OperatorType.OrNot ? LucenePrefixOperator.Minus : LucenePrefixOperator.None; return(LuceneQueryHelper.Or( ToLuceneQuery(context, query, where.Left, metadata, parameters, analyzer, getSpatialField, exact), LucenePrefixOperator.None, ToLuceneQuery(context, query, where.Right, metadata, parameters, analyzer, getSpatialField, exact), orPrefix)); } } if (expression is BetweenExpression be) { var fieldName = ExtractIndexFieldName(query, parameters, be.Source, metadata); var(valueFirst, valueFirstType) = GetValue(fieldName, query, metadata, parameters, be.Min); var(valueSecond, _) = GetValue(fieldName, query, metadata, parameters, be.Max); var(luceneFieldName, fieldType, termType) = GetLuceneField(fieldName, valueFirstType); switch (fieldType) { case LuceneFieldType.String: var valueFirstAsString = GetValueAsString(valueFirst); var valueSecondAsString = GetValueAsString(valueSecond); return(LuceneQueryHelper.Between(luceneFieldName, termType, valueFirstAsString, valueSecondAsString, exact)); case LuceneFieldType.Long: var valueFirstAsLong = (long)valueFirst; var valueSecondAsLong = (long)valueSecond; return(LuceneQueryHelper.Between(luceneFieldName, termType, valueFirstAsLong, valueSecondAsLong)); case LuceneFieldType.Double: var valueFirstAsDouble = (double)valueFirst; var valueSecondAsDouble = (double)valueSecond; return(LuceneQueryHelper.Between(luceneFieldName, termType, valueFirstAsDouble, valueSecondAsDouble)); default: throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, null); } } if (expression is InExpression ie) { var fieldName = ExtractIndexFieldName(query, parameters, ie.Source, metadata); LuceneTermType termType = LuceneTermType.Null; bool hasGotTheRealType = false; if (ie.All) { var allInQuery = new BooleanQuery(); foreach (var value in GetValuesForIn(context, query, ie, metadata, parameters, fieldName)) { if (hasGotTheRealType == false) { // here we assume that all the values are of the same type termType = GetLuceneField(fieldName, value.Type).LuceneTermType; hasGotTheRealType = true; } if (exact && metadata.IsDynamic) { fieldName = new QueryFieldName(AutoIndexField.GetExactAutoIndexFieldName(fieldName.Value), fieldName.IsQuoted); } allInQuery.Add(LuceneQueryHelper.Equal(fieldName, termType, value.Value, exact), Occur.MUST); } return(allInQuery); } var matches = new List <string>(); foreach (var tuple in GetValuesForIn(context, query, ie, metadata, parameters, fieldName)) { if (hasGotTheRealType == false) { // we assume that the type of all the parameters is the same termType = GetLuceneField(fieldName, tuple.Type).LuceneTermType; hasGotTheRealType = true; } matches.Add(LuceneQueryHelper.GetTermValue(tuple.Value, termType, exact)); } return(new TermsMatchQuery(fieldName, matches)); } if (expression is TrueExpression) { return(new MatchAllDocsQuery()); } if (expression is MethodExpression me) { var methodName = me.Name.Value; var methodType = QueryMethod.GetMethodType(methodName); switch (methodType) { case MethodType.Search: return(HandleSearch(query, me, metadata, parameters, analyzer)); case MethodType.Boost: return(HandleBoost(context, query, me, metadata, parameters, analyzer, getSpatialField, exact)); case MethodType.StartsWith: return(HandleStartsWith(query, me, metadata, parameters)); case MethodType.EndsWith: return(HandleEndsWith(query, me, metadata, parameters)); case MethodType.Lucene: return(HandleLucene(query, me, metadata, parameters, analyzer)); case MethodType.Exists: return(HandleExists(query, parameters, me, metadata)); case MethodType.Exact: return(HandleExact(context, query, me, metadata, parameters, analyzer, getSpatialField)); case MethodType.Within: case MethodType.Contains: case MethodType.Disjoint: case MethodType.Intersects: return(HandleSpatial(query, me, metadata, parameters, methodType, getSpatialField)); default: QueryMethod.ThrowMethodNotSupported(methodType, metadata.QueryText, parameters); return(null); // never hit } } throw new InvalidQueryException("Unable to understand query", query.QueryText, parameters); }
public static Query Equal(string fieldName, LuceneTermType termType, double value) { return(CreateRange(fieldName, value, true, value, true)); }
public static Query Equal(string fieldName, LuceneTermType termType, string value, bool exact) { return(Term(fieldName, value, termType, exact: exact)); }
public static Query GreaterThanOrEqual(string fieldName, LuceneTermType termType, string value, bool exact) { return(CreateRange(fieldName, value, termType, true, Null, LuceneTermType.Null, true, exact)); }
public static Query AnalyzedTerm(string fieldName, string term, LuceneTermType type, Analyzer analyzer, float?boost = null, float?similarity = null) { if (type != LuceneTermType.String && type != LuceneTermType.Prefix && type != LuceneTermType.WildCard) { throw new InvalidOperationException("Analyzed terms can be only created from string values."); } if (boost.HasValue == false) { boost = 1; } if (type == LuceneTermType.WildCard) { return(new WildcardQuery(GetAnalyzedWildcardTerm(fieldName, term, analyzer)) { Boost = boost.Value }); } var tokenStream = analyzer.ReusableTokenStream(fieldName, new StringReader(term)); var terms = new List <string>(); while (tokenStream.IncrementToken()) { var attribute = (TermAttribute)tokenStream.GetAttribute <ITermAttribute>(); terms.Add(attribute.Term); } if (type == LuceneTermType.Prefix) { if (terms.Count != 0) { var first = terms[0]; var actualTerm = first[first.Length - 1] == AsteriskChar?first.Substring(0, first.Length - 1) : first; return(new PrefixQuery(new Term(fieldName, actualTerm)) { Boost = boost.Value }); } // if the term that we are trying to prefix has been removed entirely by the analyzer, then we are going // to cheat a bit, and check for both the term in as specified and the term in lower case format so we can // find it regardless of casing var removeStar = term.Substring(0, term.Length - 1); var booleanQuery = new BooleanQuery { Clauses = { new BooleanClause(new PrefixQuery(new Term(fieldName, removeStar)), Occur.SHOULD), new BooleanClause(new PrefixQuery(new Term(fieldName, removeStar.ToLowerInvariant())), Occur.SHOULD) }, Boost = boost.Value }; return(booleanQuery); } if (terms.Count == 1) { return(new TermQuery(new Term(fieldName, terms[0])) { Boost = boost.Value }); } var pq = new PhraseQuery { Boost = boost.Value }; foreach (var t in terms) { pq.Add(new Term(fieldName, t)); } return(pq); }
public static Query GreaterThan(Index index, string fieldName, LuceneTermType termType, string value, bool exact) { return(CreateRange(index, fieldName, value, termType, false, Null, LuceneTermType.Null, true, exact)); }
public static Query LessThan(string fieldName, LuceneTermType termType, double value) { return(CreateRange(fieldName, double.MinValue, true, value, false)); }
public static Query Between(string fieldName, LuceneTermType termType, string fromValue, string toValue, bool exact) { return(CreateRange(fieldName, fromValue, termType, true, toValue, termType, true, exact)); }
public static Query GreaterThanOrEqual(string fieldName, LuceneTermType termType, long value) { return(CreateRange(fieldName, value, true, long.MaxValue, true)); }
public static Query LessThanOrEqual(string fieldName, LuceneTermType termType, string value, bool exact) { return(CreateRange(fieldName, Asterisk, LuceneTermType.WildCard, false, value, termType, true, exact)); }
public static Query GreaterThanOrEqual(Raven.Server.Documents.Indexes.Index index, string fieldName, LuceneTermType termType, string value, bool exact) { return(CreateRange(index, fieldName, value, termType, true, Null, LuceneTermType.Null, true, exact)); }