Skip to content

Commit

Permalink
Insert MatchAllDocsQuery into OrElse BinaryExpressions.
Browse files Browse the repository at this point in the history
Fixes incorrect query translation of e.g. (d.Name == "A" || d.Year != 2001).
  • Loading branch information
chriseldredge committed Jan 5, 2015
1 parent 5c1b9d7 commit fa04787
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
21 changes: 17 additions & 4 deletions source/Lucene.Net.Linq.Tests/Integration/SelectTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Lucene.Net.Analysis;
using Lucene.Net.Linq.Analysis;
using Lucene.Net.Linq.Mapping;
Expand Down Expand Up @@ -88,6 +89,18 @@ public void Where()
Assert.That(result.Single().Name, Is.EqualTo("My Document"));
}

[Test]
public void BinaryOrWithNegation()
{
AddDocument(new SampleDocument { Name = "Interstellar", Scalar = 2010});
AddDocument(new SampleDocument { Name = "Gladiator", Scalar = 2000 });

var documents = provider.AsQueryable<SampleDocument>();
var result = documents.Where(d => d.Scalar == 2010 || d.Name != "Interstellar").ToList();

Assert.That(result.Select(d => d.Name), Is.EquivalentTo(new[] {"Interstellar", "Gladiator"}));
}

[Test]
public void Where_FlagEqualTrue()
{
Expand Down Expand Up @@ -260,7 +273,7 @@ public void Where_IgnoresToLowerWithinNullSafetyCondition()

Assert.That(result.Single().Name, Is.EqualTo("My Document"));
}

[Test]
public void Where_Keyword_StartsWith()
{
Expand Down Expand Up @@ -347,7 +360,7 @@ public void Where_StartsWith_Negate_NullSafe()
AddDocument(new SampleDocument { Name = "My Document", NullableScalar = 12 });

var documents = provider.AsQueryable<SampleDocument>();

var result = from doc in documents where (bool)(!((bool?)doc.Name.StartsWith("other"))) select doc;

Assert.That(result.Single().Name, Is.EqualTo("My Document"));
Expand Down Expand Up @@ -515,5 +528,5 @@ public void Where_AnyField_StartsWith()
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected override Expression VisitLuceneQueryPredicateExpression(LuceneQueryPre
var pattern = GetPattern(expression, mapping);

var occur = expression.Occur;

if (string.IsNullOrEmpty(pattern))
{
pattern = "*";
Expand Down Expand Up @@ -121,7 +121,7 @@ protected override Expression VisitLuceneRangeQueryExpression(LuceneRangeQueryEx
var mapping = fieldMappingInfoProvider.GetMappingInfo(expression.QueryField.FieldName);

var query = CreateRangeQuery(mapping, expression.LowerQueryType, expression.Lower, expression.UpperQueryType, expression.Upper);

queries.Push(new BooleanQuery {{query, expression.Occur}});

return base.VisitLuceneRangeQueryExpression(expression);
Expand All @@ -147,8 +147,8 @@ private string GetPattern(LuceneQueryPredicateExpression expression, IFieldMappi
if (expression.Fuzzy.HasValue)
{
pattern += string.Format(
CultureInfo.InvariantCulture,
"~{0}",
CultureInfo.InvariantCulture,
"~{0}",
expression.Fuzzy.Value);
}

Expand Down Expand Up @@ -179,7 +179,7 @@ private static Occur Negate(Occur occur)
? Occur.MUST
: Occur.MUST_NOT;
}

private Expression MakeBooleanQuery(BinaryExpression expression)
{
var result = base.VisitBinaryExpression(expression);
Expand All @@ -201,7 +201,15 @@ private void Combine(BooleanQuery target, BooleanQuery source, Occur occur)
{
if (source.GetClauses().Length == 1)
{
var clause = source.GetClauses()[0];
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;
Expand All @@ -228,7 +236,7 @@ private object EvaluateExpression(Expression expression)
private string EvaluateExpressionToString(LuceneQueryPredicateExpression expression, IFieldMappingInfo mapping)
{
var result = EvaluateExpression(expression);

var str = mapping == null ? result.ToString() : mapping.ConvertToQueryExpression(result);

if (expression.AllowSpecialCharacters)
Expand All @@ -237,4 +245,4 @@ private string EvaluateExpressionToString(LuceneQueryPredicateExpression express
return mapping != null ? mapping.EscapeSpecialCharacters(str) : str;
}
}
}
}

0 comments on commit fa04787

Please sign in to comment.