This repository is currently being migrated. It's locked while the migration is in progress.
forked from beingmeta/framerd-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch.scm
69 lines (57 loc) · 2.09 KB
/
elasticsearch.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
;;; -*- Mode: Scheme; -*-
(in-module 'elasticsearch)
(use-module '{fdweb logger texttools jsonout varconfig})
(module-export! '{req->json
elastic/new elastic/upload elastic/delete
elastic/search elastic/search/x})
(define *endpoint* #f)
(varconfig! elastic:endpoint *endpoint*)
;;; Interface to elastic search
(define (req->json s)
(if (response/ok? s)
(jsonparse (get s '%content))
s))
(define (elastic/new name (spec #f) (opts #f) (endpoint) (url))
(default! endpoint (getopt opts 'endpoint *endpoint*))
(set! url (mkpath endpoint name))
(if (response/ok? (urlget url))
url
(let* ((response (urlput url
(if spec (json->string spec) "")
(if spec "application/json" ""))))
(if (response/ok? response)
(if (getopt opts 'verbose)
(jsonparse (get response '%content))
url)
response))))
(define (elastic/delete endpoint (opts #f))
(req->json (urlput endpoint "" "text/plain" [method "DELETE"])))
(define (elastic/upload endpoint doc (opts #f))
(when (string? doc)
(set! doc [content doc content-type "text/plain"]))
(let* ((id (try (get doc 'docid) (getopt opts 'newid #f)))
(url (if id
(mkpath (mkpath endpoint "_doc") id)
(mkpath endpoint "_doc"))))
(req->json (urlput url (json->string doc) "application/json"
[method (if id "PUT" "POST")]))))
(define (elastic/search/x endpoint query (opts #f))
(let* ((size (if (number? opts) opts (getopt opts 'size (getopt opts 'maxrank))))
(url (mkpath endpoint "_search"))
(base (if (table? query) query
(frame-create #f
(getopt opts 'matchop 'match)
(frame-create #f
(getopt opts'matchslot 'norm)
query))))
(query (frame-create #f
'query base
'size (tryif size size))))
(req->json (urlput url (json->string query) "application/json"
[method "GET"]))))
(define (elastic/search endpoint query (opts #f))
(let ((result (elastic/search/x endpoint query opts)))
(forseq (hit (get (get result 'hits) 'hits))
(frame-create hit
'%id (get (parse-arg (get hit '_id)) 'text)
'oid (parse-arg (get hit '_id))))))