ElasticSearch的LUA客户端

2k 词

作者:糖果

从Github上看,有一位叫做Dhaval Kapil老师完成了ElasticSearch for Lua的工作,另一位来自阿根廷的叫做Cristian Haunsen的老师,完成了ElasticSearch for Lapis的客户端程序写作工作,dhavalkapil还有一个博客可以访问:dhavalkapil.com

这次实验的目标,是测试一下本地直接运行ES for Lua,然后在Lapis中访问ES,我们的日志在ES,可以用Lua,也可以用Python完成ES的访问工作, Python没有问题,Lua就看这个实验了。

测试代码,如下:

local elasticsearch = require "elasticsearch"

local client = elasticsearch.client{
    hosts = {
      { -- Ignoring any of the following hosts parameters is allowed.
        -- The default shall be set
        protocol = "http",
        host = "localhost",
        port = 9200
      }
    },
    -- Optional parameters
    params = {
      pingTimeout = 2
    }
}

-- Will connect to default host/port
local client = elasticsearch.client()

local data, err = client:info()

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 is RoundRobinSelector.
connectionPool : The type of connection pool to be used. Default is StaticConnectionPool.
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 is warning.
Getting info of elasticsearch server(取得ES服务器信息)
local data, err = client:info()

Index a document(创建索引文档)

Everything is represented as a lua table.(一切皆为Lua Table)

local data, err = client:index{
  index = "my_index",
  type = "my_type",
  id = "my_doc",
  body = {
    my_key = "my_param"
  }
}

Get a document(取得文档)

data, err = client:get{
  index = "my_index",
  type = "my_type",
  id = "my_doc"
}

Delete a document(删除文档)

data, err = client:delete{
  index = "my_index",
  type = "my_type",
  id = "my_doc"
}

Searching a document (检索文档)

You can search a document using either query string:(使有查询字符进行检索)

data, err = client:search{
  index = "my_index",
  type = "my_type",
  q = "my_key:my_param"
}

Or either a request body:(或是请求体)

data, err = client:search{
  index = "my_index",
  type = "my_type",
  body = {
    query = {
      match = {
        my_key = "my_param"
      }
    }
  }
}

Update a document(更新文档)

data, err = client:update{
  index = "my_index",
  type = "my_type",
  id = "my_doc",
  body = {
    doc = {
      my_key = "new_param"
    }
  }
}

原文

PS:本文测式用代码都来自至官方ES-LUA的Github的Readme。