View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        jan@swi-prolog.org
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2024, SWI-Prolog Solutions b.v.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(elastic,
   36          [ es_get/2,                   % +Path, -JSON
   37            es_put/2,
   38            es_post/2,                  % +Path, +Data
   39            es_post/3,                  % +Path, +Data, -Result
   40            es_create_index/2,
   41            es_delete_index/1,
   42            es_add/4,                   % +Index, +Doc, -Reply, +Options
   43            es_search/3
   44          ]).   45:- use_module(library(http/http_open)).   46:- use_module(library(http/http_json)).   47:- use_module(library(http/json)).   48:- use_module(library(option)).   49:- use_module(library(uri)).   50
   51:- multifile
   52    es_server/2.                        % URL, Options
   53
   54/** <module> Core Elastic search interface
   55
   56This module is a thin layer over  the Elastic REST interface. Eventually
   57this should go into a pack.
   58*/
   59
   60%!  es_get(+Path, -Result) is det.
   61%!  es_put(+Path, +Data) is det.
   62%!  es_post(+Path, +Data) is det.
   63%!  es_post(+Path, +Data, -Result) is det.
   64%
   65%   Low level access to the Elastic HTTP service. Path is either an atom
   66%   or a /-separated segment list, e.g., `Index/'_doc'`.
   67%
   68%   @arg Result is a JSON term represented as a dict.
   69
   70es_get(Path, Result) :-
   71    es_url(Path, URL, Options),
   72    http_open(URL, In,
   73              [ status_code(Status)
   74              | Options
   75              ]),
   76    call_cleanup(reply(Status, In, Result), close(In)).
   77
   78es_put(Path, Data) :-
   79    es_url(Path, URL, Options),
   80    http_open(URL, In, [ method(put),
   81                         post(json(Data)),
   82                         status_code(Status)
   83                       | Options
   84                       ]),
   85    call_cleanup(ok_reply(Status, In), close(In)).
   86
   87es_post(Path, Data) :-
   88    es_url(Path, URL, Options),
   89    http_open(URL, In, [ post(json(Data)),
   90                         status_code(Status)
   91                       | Options
   92                       ]),
   93    call_cleanup(ok_reply(Status, In), close(In)).
   94
   95es_post(Path, Data, Result) :-
   96    es_url(Path, URL, Options),
   97    http_open(URL, In, [ post(json(Data)),
   98                         status_code(Status)
   99                       | Options
  100                       ]),
  101    call_cleanup(reply(Status, In, Result), close(In)).
  102
  103%!  es_add(+Index, +Data, -Reply, +Options) is det.
  104%
  105%   Add a document to  the  index  Index.   Data  is  a  Prolog  dict to
  106%   represent the document. This must  satisfy   the  type as defined by
  107%   es_create_index/2.
  108
  109es_add(Index, Data, Reply, Options) :-
  110    option(id(Id), Options),
  111    !,
  112    es_post(Index/'_doc'/Id, Data, Reply).
  113es_add(Index, Data, Reply, _Options) :-
  114    es_post(Index/'_doc', Data, Reply).
  115
  116%!  es_search(+Index, +Query, -Result) is det.
  117%
  118%   Search using Query, returning a list   of  Prolog dicts that express
  119%   the result.
  120
  121es_search(Index, For, Result) :-
  122    es_post(Index/'_search', For, Result).
  123
  124%!  es_create_index(+Index, +Mapping) is det.
  125%!  es_delete_index(+Index) is det.
  126%
  127%   Create or delete an index. Note that  deleting an index also deletes
  128%   all its data.
  129
  130es_create_index(Index, Mapping) :-
  131    es_put(Index,
  132           #{mappings:
  133             #{properties:Mapping}
  134            }).
  135
  136es_delete_index(Index) :-
  137    es_url(Index, URL, Options),
  138    http_open(URL, In, [ method(delete),
  139                         status_code(Status)
  140                       | Options
  141                       ]),
  142    call_cleanup(ok_reply(Status, In), close(In)).
  143
  144%!  es_url(+Path, -URL, -Options) is det.
  145%
  146%   Etablish the Elastic URL from Path and the options for connecting as
  147%   required  by  http_open/3.  The  latter    typically   contains  TLS
  148%   authentication if this is used.
  149
  150es_url(Path, URL, [connection('Keep-alive')|Options]) :-
  151    es_server(URL0, Options),
  152    path_segments(Path1, Path),
  153    uri_edit(path(Path1), URL0, URL).
  154
  155:- public tls_verify/5.  156tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, verified) :-
  157    !.
  158tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, hostname_mismatch) :-
  159    !.
  160tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, expired) :-
  161    !.
  162tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, _Error) :-
  163    fail.
  164
  165%!  ok_reply(+Status, +Stream) is det.
  166%!  reply(+Status, +Stream, -Reply) is det.
  167%
  168%   Handle the HTTP reply. Either succeed (2xx for ok_reply/2), return a
  169%   Prolog dict with the JSON  reply  (2xx,   for  reply/3)  or raise an
  170%   exception holding with a Prolog dict holding the details.
  171
  172ok_reply(Status, _In) :-
  173    between(200, 299, Status),
  174    !.
  175ok_reply(Status, In) :-
  176    json_read_dict(In, Result, []),
  177    print_message(error, es_error(Status, Result)).
  178
  179reply(Status, In, Reply) :-
  180    between(200, 299, Status),
  181    !,
  182    json_read_dict(In, Reply, []).
  183reply(Status, In, null) :-
  184    json_read_dict(In, Result, []),
  185    throw(error(es_error(Status, Result), _)).
  186
  187%!  path_segments(?Path, ?Segments) is det.
  188%
  189%   Convert between `a/b/c/...` and `'a/b/c/...'`
  190
  191path_segments(Path, Segments) :-
  192    nonvar(Path),
  193    !,
  194    atomic_list_concat(Segments, /, Path).
  195path_segments(Path, Path) :-
  196    atomic(Path),
  197    !.
  198path_segments(Path, Segments) :-
  199    phrase(segments(Segments), List),
  200    atomic_list_concat(List, /, Path).
  201
  202segments(A/B) -->
  203    !,
  204    segments(A),
  205    segments(B).
  206segments(A) -->
  207    [A].
  208
  209		 /*******************************
  210		 *            MESSAGES		*
  211		 *******************************/
  212
  213:- multifile
  214    prolog:message//1,
  215    prolog:error_message//1.  216
  217prolog:error_message(es_error(Code, Message)) -->
  218    [ 'Elastic search: got reply ~w:'-[Code], nl,
  219      '   ~@'-[print_term(Message, [output(current_output)])],
  220      nl
  221    ]