You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@community.apache.org by se...@apache.org on 2023/03/18 14:24:37 UTC
svn commit: r1908491 - /comdev/helpwanted.apache.org/site/lib/elastic.lua
Author: sebb
Date: Sat Mar 18 14:24:37 2023
New Revision: 1908491
URL: http://svn.apache.org/viewvc?rev=1908491&view=rev
Log:
ES no longer supports application/x-www-form-urlencoded for POST requests
Modified:
comdev/helpwanted.apache.org/site/lib/elastic.lua
Modified: comdev/helpwanted.apache.org/site/lib/elastic.lua
URL: http://svn.apache.org/viewvc/comdev/helpwanted.apache.org/site/lib/elastic.lua?rev=1908491&r1=1908490&r2=1908491&view=diff
==============================================================================
--- comdev/helpwanted.apache.org/site/lib/elastic.lua (original)
+++ comdev/helpwanted.apache.org/site/lib/elastic.lua Sat Mar 18 14:24:37 2023
@@ -18,12 +18,32 @@
-- This is elastic.lua - ElasticSearch library
local http = require 'socket.http'
+local ltn12 = require 'ltn12'
local JSON = require 'cjson'
local config = {
es_url = "http://localhost:9200/helpwanted/"
}
local default_doc = "item"
+-- ES no longer supports Content-type: application/x-www-form-urlencoded
+-- which is the default with the simple interface http.request(url, body)
+-- we have to use the rather more complicated generic interface
+local function _http_request(url, body)
+ local hdrs = {}
+ source=ltn12.source.string(body)
+ hdrs['content-length'] = #body
+ hdrs['content-type'] = 'application/json'
+ local result = {}
+ http.request{
+ url = url,
+ method = 'POST',
+ headers = hdrs,
+ sink=ltn12.sink.table(result),
+ source=source
+ }
+ return table.concat(result)
+end
+
-- Standard ES query, returns $size results of any doc of type $doc, sorting by $sitem (desc)
local function find(query, size, doc, sitem)
doc = doc or default_doc
@@ -61,7 +81,7 @@ local function raw(query, doctype)
local js = JSON.encode(query)
doctype = doctype or default_doc
local url = config.es_url .. doctype .. "/_search"
- local result = http.request(url, js)
+ local result = _http_request(url, js)
local json = JSON.decode(result)
return json or {}
end
@@ -71,7 +91,7 @@ local function update(doctype, id, query
local js = JSON.encode({doc = query })
doctype = doctype or default_doc
local url = config.es_url .. doctype .. "/" .. id .. "/_update"
- local result = http.request(url, js)
+ local result = _http_request(url, js)
local json = JSON.decode(result)
return json or {}
end
@@ -83,7 +103,7 @@ local function index(r, id, ty, body)
id = r:sha1(ty .. (math.random(1,99999999)*os.time()) .. ':' .. r:clock())
end
local url = config.es_url .. ty .. "/" .. id
- local result = http.request(url, body)
+ local result = _http_request(url, body)
local json = JSON.decode(result)
return json or {}
end