Skip to content

Instantly share code, notes, and snippets.

@dterziev
Created October 7, 2016 12:52
Show Gist options
  • Save dterziev/8d8b0f59efb808e55a4a45b04e645a2c to your computer and use it in GitHub Desktop.
Save dterziev/8d8b0f59efb808e55a4a45b04e645a2c to your computer and use it in GitHub Desktop.
elasticsearch + fsharp
namespace Flexisearch
module Core =
open Chiron
open Chiron.Operators
open Hopac
open HttpFs.Client
[<AutoOpen>]
module DomainTypes =
type Environment =
{ BaseUrl: System.Uri }
type MetaDocument =
{ Id: string
Index: string
Source: Json option
Type: string
Version: decimal option
Found: bool }
static member FromJson (_: MetaDocument) =
fun id index source ``type`` version found ->
{ Id = id
Index = index
Source = source
Type = ``type``
Version = version
Found = found }
<!> Json.read "_id"
<*> Json.read "_index"
<*> Json.readOrDefault "_source" None
<*> Json.read "_type"
<*> Json.readOrDefault "_version" None
<*> Json.read "found"
static member ToJson (x: MetaDocument) =
Json.write "_id" x.Id
*> Json.write "_index" x.Index
*> Json.writeUnlessDefault "_source" None x.Source
*> Json.write "_type" x.Type
*> Json.writeUnlessDefault "_version" None x.Version
*> Json.write "found" x.Found
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module MetaDocument =
let inline source metaData =
match metaData.Source with
| Some json -> Json.deserialize json
| None -> failwith (sprintf "%s %s %s not found" metaData.Index metaData.Type metaData.Id)
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Environment =
let make baseUrl =
{ BaseUrl = System.Uri(baseUrl) }
let baseUrl env = env.BaseUrl
let url (relativeUrl: string) env =
System.Uri(Environment.baseUrl env,relativeUrl)
let get (url: System.Uri) : Job<MetaDocument> =
url.ToString()
|> Request.createUrl Get
|> Request.responseAsString
|> Hopac.Job.map (Json.parse >> Json.deserialize)
let put body (url: System.Uri) =
url.ToString()
|> Request.createUrl Put
|> Request.bodyString body
|> Request.responseAsString
let run job =
Hopac.run job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment