4

I am attempting to convert a C# codebase over to F# that uses ElasticSearch.NET client. F# seems to not properly infer the correct types.

Here is the working C# code:

var res = es.Search<Question>(s => s
   .Query(q => q
   .FunctionScore(fs => fs
   .Query(fq => fq.MatchAll())
   .Functions(f => f.RandomScore(r.Next()))
)).Size(count).Index("questions"));

The equivalent or matching F# code is below and it doesn't compile:

    let questions = es.Search<Question>(fun s ->
                    s.Query(fun q -> q.FunctionScore(
                                        fun fs -> fs.Query(
                                                    fun fq->fq.MatchAll()).Functions(
                                                            fun f -> f.RandomScore(r.Next())) |> ignore)).Size(count).Index("questions")).Documents.ToList()

This code does properly work using F#, however, it's not what I need:

let questions2 = es.Search<Question>(fun s -> s.Query(fun q -> q.MatchAll())).Documents.ToList()

The F# that does not compile shows the following error:

Lookup on object of indeterminate type based on information prior to this program. A type annotation may be required prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.

The F# type signature of Question follows:

type Question = {id:Guid, Text:string}
bbqchickenrobot
  • 3,592
  • 3
  • 45
  • 67

0 Answers0