Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to search within a nullable int field #82

Open
xumix opened this issue Jun 30, 2015 · 3 comments
Open

Unable to search within a nullable int field #82

xumix opened this issue Jun 30, 2015 · 3 comments

Comments

@xumix
Copy link

xumix commented Jun 30, 2015

The query looks like:

var bdates = parameter.ChildBirthDates.Select(s => s.TryParseDateYyyyMmDd().Value.GetDiffInYears(date1)).ToArray();
query = query.Where(q => (!q.ChildFromAge.HasValue || q.ChildFromAge <= bdates.Min()) && (!q.ChildToAge.HasValue || q.ChildToAge <= bdates.Max()));

So I want items where there are no limitsor the limits are in range.
The error looks like this:

System.NotSupportedException occurred
  HResult=-2146233067
  Message=Expected Left or Right to be LuceneQueryFieldExpression
  Source=Lucene.Net.Linq
  StackTrace:
       at Lucene.Net.Linq.Transformation.TreeVisitors.BinaryToQueryExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
       at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at Lucene.Net.Linq.Transformation.TreeVisitors.BinaryToQueryExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
       at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at Lucene.Net.Linq.Transformation.TreeVisitors.BinaryToQueryExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
       at Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func`2 transformation)
       at Lucene.Net.Linq.Transformation.QueryModelTransformer.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
       at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
       at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
       at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
       at Remotion.Linq.QueryModel.Accept(IQueryModelVisitor visitor)
       at Lucene.Net.Linq.LuceneQueryExecutorBase`1.PrepareQuery(QueryModel queryModel)
       at Lucene.Net.Linq.LuceneQueryExecutorBase`1.<ExecuteCollection>d__3`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at RestChild.Booking.Logic.TourSearchService.SearchTours(SearchToursParams parameter) in c:\Projects\RestChild\RestChild.Booking.Logic\TourSearchService.cs:line 90
  InnerException: 
@xumix
Copy link
Author

xumix commented Jun 30, 2015

The expression that causes this error looks like this:
((LuceneField(ChildFromAge) != null) == False)

@chriseldredge
Copy link
Owner

As a work around to using HasValue which is currently not recognized, you might have some luck using == null, as in:

query = query.Where(q => (q.ChildFromAge == null || q.ChildFromAge <= bdates.Min()) && (q.ChildToAge == null || q.ChildToAge <= bdates.Max()));

Alternatively, your data model could be altered to default ChildFromAge to zero and ChildFromAge to 200 (for example). This would likely result in better query execution performance since the less-than and greater-than expressions would be reduced to a Numeric Range Query.

@xumix
Copy link
Author

xumix commented Jul 9, 2015

Thanks for the tip! I'll use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants