I am trying to find all posts in RavenDB containing a word (index is there)
Here is a query that works, finds everything that starts with 'Liv'
let post = query {
for post in session.Query<MyType>() do
where (post.Text.StartsWith("Liv"))
select post
}
An attempt to use string.Contains() method as condition of Where closure, will throw NotSupportedException. Here
So I am trying to use Search method where:
Expression<Func<T, object>> fieldSelector,
// Expression marking a field in which terms should be looked for.
C# equivalent from docs:
List<User> users = session
.Query<User>("Users/ByNameAndHobbies")
.Search(x => x.Name, "Adam")
.Search(x => x.Hobbies, "sport")
.ToList();
My first try was to go with
let x = session.Query<MyType>(index).Search((fun xe -> xe.Text ), "Liv")
But getting error because it expects object out. Tried to downcast String to Object (what a strange idea), but getting:
Cannot understand how to translate x => x.Invoke(xe)
At the moment, I am out of ideas. I am supposed to mark field for search and return object. Any ideas?
Thank you.
EDIT 1: My expression. Gets runtime InvalidCastException because it can't cast string to obj.
let expr =
<@ Func<MyType, _>(fun xe -> xe.Text ) @>
|> LeafExpressionConverter.QuotationToExpression
|> unbox<System.Linq.Expressions.Expression<Func<MyType, _>>>
You mentioned that you tried casting the string
to object
. I tried it using :> obj
and it does work.
Here is my working query:
let expr = <@ Func<MyType,_>(fun x -> x.Text :> obj ) @>
|> LeafExpressionConverter.QuotationToExpression
|> unbox<Linq.Expressions.Expression<Func<MyType,_>>>
let events = session.Query<MyType>()
.Search(expr, "Liv*", decimal 1, SearchOptions.Or, EscapeQueryOptions.AllowAllWildcards)
|> List.ofSeq
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With