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:34:48 UTC

svn commit: r1908492 - /comdev/helpwanted.apache.org/site/admin/elastic.lua

Author: sebb
Date: Sat Mar 18 14:34:48 2023
New Revision: 1908492

URL: http://svn.apache.org/viewvc?rev=1908492&view=rev
Log:
Synchronise with main version of module

Modified:
    comdev/helpwanted.apache.org/site/admin/elastic.lua

Modified: comdev/helpwanted.apache.org/site/admin/elastic.lua
URL: http://svn.apache.org/viewvc/comdev/helpwanted.apache.org/site/admin/elastic.lua?rev=1908492&r1=1908491&r2=1908492&view=diff
==============================================================================
--- comdev/helpwanted.apache.org/site/admin/elastic.lua (original)
+++ comdev/helpwanted.apache.org/site/admin/elastic.lua Sat Mar 18 14:34:48 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,19 +91,18 @@ 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
 
 -- Put a new document somewhere
 local function index(r, id, ty, body)
-    local js = JSON.encode(query)
     if not id then
         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