Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Created March 7, 2016 15:16
Show Gist options
  • Save MartinBodocky/9704c9c697313887297b to your computer and use it in GitHub Desktop.
Save MartinBodocky/9704c9c697313887297b to your computer and use it in GitHub Desktop.
ElasticSearch and F#
#r "../packages/Elasticsearch.Net.2.0.4/lib/net46/Elasticsearch.Net.dll"
#r "../packages/NEST.2.0.4/lib/net46/Nest.dll"
#r "../packages/Newtonsoft.Json.8.0.2/lib/net45/Newtonsoft.Json.dll"
open System
open System.Linq
open Nest
module Utils =
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.RuntimeHelpers
open System.Linq.Expressions
(* Building expression for linq!
http://stackoverflow.com/questions/9134475/expressionfunct-bool-from-a-f-func
*)
let toLinq (expr : Expr<'a -> 'b>) =
let linq = LeafExpressionConverter.QuotationToExpression expr
let call = linq :?> MethodCallExpression
let lambda = call.Arguments.[0] :?> LambdaExpression
Expression.Lambda<Func<'a, 'b>>(lambda.Body, lambda.Parameters)
type Post() =
member val UserId : int = -1 with get, set
member val PostDate : DateTime = DateTime.MinValue with get, set
member val PostTest : string = "" with get, set
type Example() =
let mutable _client = Unchecked.defaultof<ElasticClient>
do
let node = new Uri("http://localhost:9200")
let settings = new ConnectionSettings(node)
settings.DefaultIndex("my_blog") |> ignore
_client <- new ElasticClient(settings)
let indexSettings = new IndexSettings()
indexSettings.NumberOfReplicas <- new Nullable<int>(1)
indexSettings.NumberOfShards <- new Nullable<int>(1)
let indexName = new IndexName()
indexName.Name <- "my_blog"
indexName.Type <- typedefof<Post>
let indexState = new IndexState()
indexState.Settings <- indexSettings
_client.CreateIndex
(indexName, fun (c : CreateIndexDescriptor) -> c.InitializeUsing(indexState) :> ICreateIndexRequest)
|> ignore
member x.InsertData : unit =
let newBlogPost = new Post()
newBlogPost.UserId <- 1
newBlogPost.PostDate <- DateTime.Now
newBlogPost.PostTest <- "This is another blog post."
let newBlogPost2 = new Post()
newBlogPost2.UserId <- 2
newBlogPost2.PostDate <- DateTime.Now
newBlogPost2.PostTest <- "This is a third blog post."
let newBlogPost3 = new Post()
newBlogPost3.UserId <- 3
newBlogPost3.PostDate <- DateTime.Now.AddDays(5.0)
newBlogPost3.PostTest <- "This is a blog post from the future."
_client.Index(newBlogPost) |> ignore
_client.Index(newBlogPost2) |> ignore
_client.Index(newBlogPost3) |> ignore
member x.GetAllDocuments = _client.Search<Post>().Documents.ToList()
member x.PerformTermQuery =
_client.Search<Post>(
fun (s: SearchDescriptor<Post>) ->
s.Query(fun (j:QueryContainerDescriptor<Post>) ->
j.Term(Utils.toLinq(<@ fun (q:Post) -> q.PostTest :> obj @>),"blog")) :> ISearchRequest).Documents.ToList()
member x.PerformMatchPhrase =
_client.Search<Post>(
fun (s: SearchDescriptor<Post>) ->
s.Query(fun (q: QueryContainerDescriptor<Post>) ->
q.MatchPhrase(fun (m : MatchPhraseQueryDescriptor<Post>) -> m.Field(Utils.toLinq(<@ fun (q:Post) -> q.PostTest :> obj @>)).Query("this is a third blog post") :> IMatchQuery)) :> ISearchRequest)
.Documents.ToList()
member x.PerformFilter =
_client.Search<Post>(fun (s:SearchDescriptor<Post>) ->
s.Query(fun (q:QueryContainerDescriptor<Post>) ->
q.Term(Utils.toLinq(<@ fun (q:Post) -> q.PostTest :> obj @>),"blog")
).PostFilter(fun (f:QueryContainerDescriptor<Post>) ->
f.DateRange(fun (r:DateRangeQueryDescriptor<Post>) ->
r.Field(Utils.toLinq(<@ fun (q:Post) -> q.PostTest :> obj @>)).GreaterThan(DateMath.FromString("2015-03-08"))
:> IDateRangeQuery
)
) :> ISearchRequest
).Documents.ToList()
let example = new Example()
example.InsertData
let documents = example.GetAllDocuments
documents
|> Seq.toList
|> List.iter (fun i -> printfn "%A" i.PostTest)
let documents2 = example.PerformTermQuery
documents2
|> Seq.toList
|> List.iter (fun i -> printfn "%A" i.PostTest)
let documents3 = example.PerformMatchPhrase
documents3
|> Seq.toList
|> List.iter (fun i -> printfn "%A" i.PostTest)
let documents4 = example.PerformFilter
documents4
|> Seq.toList
|> List.iter (fun i -> printfn "%A" i.PostTest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment