A low level client for Elasticsearch written in Lua. This is a fork of the original repo by Dhaval Kapil, which he no longer maintains. This repo does not try to be all things to all people. It is tested against and supports Elasticsearch 7,x, 8,x and Amazon Opensearch 2.0 and Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT.
In accordance with the official low level clients, the client accepts associative arrays in the form of lua tables as parameters.
- One-to-one mapping with REST API and other language clients.
- Proper load balancing across all nodes.
- Pluggable and multiple connection, selection strategies and connection pool.
- Console logging facility.
- Almost every parameter is configurable.
Elasticsearch Version | elasticsearch-lua Branch |
---|---|
ES 7.0 - 8.x + OS 2.0 | master |
elasticsearch-lua
works for lua >= 5.1 <= 5.4 version and luajit.
It can be installed using luarocks
[sudo] luarocks install elasticsearch-lua
The complete documetation is here.
local elasticsearch = require "elasticsearch"
local client = elasticsearch.client{
hosts = {
{ -- Ignoring any of the following hosts parameters is allowed.
-- The default shall be set
connectionstring = "", -- Ignore host, port and protocol and parse this string instead
protocol = "http",
host = "localhost",
port = 9200,
username = "elastic",
password = "changeme",
verify = "peer",
cafile = ""
}
},
-- Optional parameters
params = {
pingTimeout = 2
}
}
The verify
and cafile
params are only applicable if the protocol
is https
.
-- Will connect to default host/port
local client = elasticsearch.client()
Full list of params
:
pingTimeout
: The timeout of a connection for ping and sniff request. Default is 1.selector
: The type of selection strategy to be used. Default isRoundRobinSelector
.connectionPool
: The type of connection pool to be used. Default isStaticConnectionPool
.connectionPoolSettings
: The connection pool settings,maxRetryCount
: The maximum times to retry if a particular connection fails.logLevel
: The level of logging to be done. Default iswarning
.
local param1, param2 = client:<func>()
param1
: Stores the data returned or nil
on error
param2
: Stores the HTTP status code on success or the error message on failure
local data, err = client:info()
Elasticsearch 7 and above have deprecated and removed type mappings. To that end, if the type parameter is supplied, it will be ignored.
Everything is represented as a lua table.
local data, err = client:index{
index = "my_index",
id = "my_doc",
body = {
my_key = "my_param"
}
}
data, err = client:get{
index = "my_index",
id = "my_doc"
}
data, err = client:delete{
index = "my_index",
id = "my_doc"
}
You can search a document using either query string:
data, err = client:search{
index = "my_index",
q = "my_key:my_param"
}
Or either a request body:
data, err = client:search{
index = "my_index",
body = {
query = {
match = {
my_key = "my_param"
}
}
}
}
data, err = client:update{
index = "my_index",
id = "my_doc",
body = {
doc = {
my_key = "new_param"
}
}
}
Feel free to file issues and submit pull requests – contributions are welcome. Please try to follow the code style used in the repository.
elasticsearch-lua is licensed under the MIT license.