Skip to content

Commit

Permalink
docs(ES integration), refactor(ES views).
Browse files Browse the repository at this point in the history
Add two space indentation, add more docs,
  • Loading branch information
Nikhil R committed Aug 15, 2016
1 parent 7c2949a commit 2c8be47
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 49 deletions.
13 changes: 11 additions & 2 deletions docs/manual_ES_model.md
Expand Up @@ -3,10 +3,19 @@

This module makes it possible to store, retrieve and search data from <a href = "https://www.elastic.co/downloads/elasticsearch">Elasticsearch </a>. It uses the <a href = "github.com/dhavalkapil/elasticsearch-lua">elasticsearch-lua</a> client for Lua. Refer the client <a href="http://elasticsearch-lua.readthedocs.io/en/latest/">here</a> to know the parameters which can be sent through the model functions if any. Return responses are also same as the one offered by this client.

After importing "es_model" we proceed to create a new instance of the model. There should be a file inside models/ which contains the keys and types defined for the particular model.
###Installation and getting started.

To get started, install the elasticsearch-lua rock using

luarocks install --server=http://luarocks.org/dev elasticsearch

Make sure you have elasticsearch installed and running. Refer to the instructions given on the official <a href = "https://www.elastic.co/downloads/elasticsearch">website</a>.

The `es_model.lua` file is the main module. After importing "es_model" we proceed to create a new instance of the model. There should be a file inside models/ which contains the keys and types defined for the particular model.

local es_model = require "sailor.es_model"
local contact = contact.new("test")
-- test.lua should be inside models/ folder.
local contact = es.model.new("test")

Now, you can assign attributes to `contact` like

Expand Down
3 changes: 2 additions & 1 deletion test/dev-app/controllers/test.lua
Expand Up @@ -19,7 +19,8 @@ function test.elasticfunctions(page)
local doc = {name = "test"}
local contact = es_model.new("test")
contact.name = "Tester 123"
msg, msg1 = contact.save{id = 101, body = doc, routing = "routing@test.com"}
contact.save{id = 101, body = doc, routing = "routing@test.com"}
if (page.POST) then msg = contact.search{q = page.POST.search} end
page:render('elastic', {msg = msg})
end

Expand Down
102 changes: 56 additions & 46 deletions test/dev-app/views/test/elastic.lp
@@ -1,48 +1,58 @@
<?lua local form = require "sailor.form" ?>

<form method="post">
<h4>Search box</h4>
<input type="text" id="search" name="search" />
<input type="submit" class="btn btn-primary btn-md"/>
</form>
<?lua
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end
end

function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end

function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end
if type(msg) == "table" then
page:print(table.tostring(msg))
else
page:print(msg)
end
?>
<h4>Search box</h4>
<input type="text" id="search" name="search" />
<input type="submit" class="btn btn-primary btn-md"/>
</form>

<?lua

function table.val_to_str ( v )

if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )

if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end

end

function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end

function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end

for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end

return "{" .. table.concat( result, "," ) .. "}"
end

if type(msg) == "table" then
page:print(table.tostring(msg))
else
page:print(msg)
end

?>

0 comments on commit 2c8be47

Please sign in to comment.