Add an elasticserrch key in your config like this:
config({ "development"}, {
elasticsearch = {
index = "twitter",
hosts = {
{
protocol = "http",
port = 9200,
host = "127.0.0.1"
}
}
}
})
Provides a Model base class for making Lua tables that can be synchronized with elasticsearch type.
local Model = require("lapis.db.elasticsearch.model").Model
local Tweets = Model:extend("tweet", {
per_page = 2
})
The first argument to extend is the name of the type to associate the model to.
Model:create(values, create_opts = nil) - The create class method is used to create new docs. It takes a table of column values to create the docs with. It returns an instance of the model.
local tweet = Tweets:create({
name = "Criztian Haunsen"
}, { id = "my_id"})
local tweet = Tweets:count()
local tweet = Tweets:count({
match_all = {}
})
local tweet = Tweets:find(1)
The find class method fetches a single row from the table by id.
local tweets = Tweets:find_all({1,2,3})
local query = {
match_all = {}
}
local tweet = Tweets:select(query)
local tweets = Tweets:find_all({1,2,3})
local tweet = Tweets:table_name()
local res = Tweets:delete(1)
res will be true on success delete
obj:update(fields = {}) - Instances of models have the update method for updating the row. The values of the primary keys are used to uniquely identify the row for updating.
tweet:update({
author = "Peter"
})
We can create a paginator like so:
local query = {
match_all = {}
}
local paginated = Tweets:paginated(query)
paginated:total_items()
paginated:num_pages()
paginated:get_page(2)