Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sabine committed Dec 25, 2023
0 parents commit 1070ad7
Show file tree
Hide file tree
Showing 19 changed files with 2,679 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.ml linguist-language=OCaml
*.mli linguist-language=OCaml
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
_build/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log

# Merlin configuring file for Vim and Emacs
.merlin

# Dune generated files
*.install

# Local OPAM switch
_opam/
Empty file added .ocamlformat
Empty file.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 sabine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# typesense-ocaml
OCaml HTTP client for Typesense

Developed against Typesense 0.25.1
34 changes: 34 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(lang dune 3.7)

(name typesense)

(version dev)

(source (github sabine/typesense-ocaml))
(homepage "https://github.com/sabine/typesense-ocaml")

(license MIT)

(authors
"Sabine Schmaltz")

(maintainers
"Sabine Schmaltz")

(generate_opam_files true)

(package
(name typesense)
(synopsis "OCaml HTTP client for Typesense")
(description "Client bindings for interfacing with Typesense,
an open-source, typo-tolerant search engine.
Abstracts over the HTTP requests to the Typesense server API and
marshalls the responses from the Typesense server into
OCaml data structures to provide a type-safe interface
to your Typesense server.")
(tags ("typesense" "search" "http client" "client bindings"))
(depends
(ocaml (>= 4.08.0))
dune
))

37 changes: 37 additions & 0 deletions typesense.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "dev"
synopsis: "OCaml HTTP client for Typesense"
description: """
Client bindings for interfacing with Typesense,
an open-source, typo-tolerant search engine.
Abstracts over the HTTP requests to the Typesense server API and
marshalls the responses from the Typesense server into
OCaml data structures to provide a type-safe interface
to your Typesense server."""
maintainer: ["Sabine Schmaltz"]
authors: ["Sabine Schmaltz"]
license: "MIT"
tags: ["typesense" "search" "http client" "client bindings"]
homepage: "https://github.com/sabine/typesense-ocaml"
bug-reports: "https://github.com/sabine/typesense-ocaml/issues"
depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "3.7"}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/sabine/typesense-ocaml.git"
Empty file added typesense.opam.template
Empty file.
1 change: 1 addition & 0 deletions typesense/config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t = { url : string; api_key : string }
3 changes: 3 additions & 0 deletions typesense/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name typesense)
(public_name typesense))
36 changes: 36 additions & 0 deletions typesense/filters.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type string_filter =
| Match of { q : string }
| ExactMatch of { q : string }
| NotEquals of { q : string }
| MatchOneOf of { q : string list }
| ExactMatchOneOf of { q : string list }

type string_list_filter = string_filter

type 'a number_filter =
| Match of { q : 'a }
| Less of { q : 'a }
| LessOrEqual of { q : 'a }
| Greater of { q : 'a }
| GreaterOrEqual of { q : 'a }
| InRange of { start : 'a; end_ : 'a }

type 'a number_list_filter = Match of { q : 'a }

(*TODO: number filter needs to be a functor
pub type Int64Filter = NumberFilter<i64>;
pub type Int32Filter = NumberFilter<i32>;
pub type FloatFilter = NumberFilter<f32>;
pub type Int64ListFilter = NumberListFilter<i64>;
pub type Int32ListFilter = NumberListFilter<i64>;
pub type FloatListFilter = NumberListFilter<i64>;
*)

type geopoint_filter =
| InRadiusAroundLocation of {
lat : float;
lon : float;
distance_in_km : float;
}
8 changes: 8 additions & 0 deletions typesense/indexing.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type action = Upsert | Update

type typesense_document_response = {
success : bool;
code : int32 option;
error : string option;
document : string; (* deserialize to JSON value *)
}
45 changes: 45 additions & 0 deletions typesense/query.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type facet_count = { count : int32; value : string }
type facet = { field_name : string; counts : facet_count list }
type 'document hit = { document : 'document }

type 'document typesense_response = {
facet_counts : (string, facet_count list) Hashtbl.t;
found : int32;
hits : 'document hit list;
out_of : int32;
page : int32;
search_time_ms : int32;
}

(* needs to be functors *)

type 'filter query = {
q : string;
query_by_with_weights : (string * int32) list;
filter_by : 'filter list;
facet_by : string list;
page : int32;
per_page : int32;
include_fields : string list;
max_facet_values : int32;
num_typos : int32;
drop_tokens_threshold : int32;
typo_tokens_threshold : int32;
}

let search (_schema : Schema.t) (query : 'filter query) =
let _query_by =
query.query_by_with_weights
|> List.map (fun (field_name, _) -> field_name)
|> String.concat ","
in
let _query_by_weight =
query.query_by_with_weights
|> List.map (fun (_, weight) -> Int32.to_string weight)
|> String.concat ","
in
let _facet_by = query.facet_by |> String.concat "," in

(* filter_by *)
let _include_fields = query.include_fields |> String.concat "," in
failwith "not implemented"
35 changes: 35 additions & 0 deletions typesense/schema.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type field_type =
| String
| StringArray
| Int32
| Int32Array
| Int64
| Int64Array
| Float
| FloatArray
| Geopoint
| GeopointArray
| Object
| ObjectArray
| AutoConvertToString
| Auto

type field = {
name : string;
typesense_type : field_type;
optional : bool;
facet : bool;
index : bool;
locale : string option;
sort : bool;
drop : bool;
infix : bool;
}

type t = {
name : string;
fields : field list;
token_separators : string list;
symbols_to_index : string list;
default_sorting_field : string option;
}
4 changes: 4 additions & 0 deletions typesense/typesense.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Schema = Schema
module Filters = Filters
module Indexing = Indexing
module Query = Query
5 changes: 5 additions & 0 deletions typesense_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
There's nothing here yet, just notes.

There's an OpenAPI spec in the Typesense's Go client bindings:

https://raw.githubusercontent.com/typesense/typesense-go/master/typesense/api/generator/openapi.yml
2 changes: 2 additions & 0 deletions typesense_api/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(library
(name typesense_api))
Loading

0 comments on commit 1070ad7

Please sign in to comment.