What is the correct syntax of the RavenDB search method in F #

I try to find all posts in RavenDB containing the word (index is)

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 } 

Trying to use the string.Contains () method as a Where clause condition will throw a NotSupportedException. Here

Therefore, I am trying to use a 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 attempt was to go with

 let x = session.Query<MyType>(index).Search((fun xe -> xe.Text ), "Liv") 

But get an error because it expects an object. Tried dragging String into Object (what a strange idea), but getting:

Unable to figure out how to translate x => x.Invoke (xe)

At the moment I have no ideas. I have to check the box for searching and returning an object. Any ideas?

Thanks.

EDIT 1: My expression. Gets a runtime InvalidCastException because it cannot distinguish a string from obj.

 let expr = <@ Func<MyType, _>(fun xe -> xe.Text ) @> |> LeafExpressionConverter.QuotationToExpression |> unbox<System.Linq.Expressions.Expression<Func<MyType, _>>> 
+6
source share
1 answer

You mentioned that you tried pouring string on object . I tried it with :> obj and it works.

Here is my working request:

 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 
+3
source

Source: https://habr.com/ru/post/987276/


All Articles