阿里云
阿里云多端小程序中小企业获客首选
发表主题 回复主题
  • 2249阅读
  • 1回复

[干货分享]阿里云开放搜索openresty lua sdk

级别: 新人
发帖
3
云币
4
己写的一个阿里开放搜索 lua脚本,可正常使用,分享给大家! HLDv{G'7  
  1. --
  2. -- 阿里云开放搜索 sdk
  3. -- User: bugscaner
  4. -- Date: 2019-01-18 15:24:35
  5. -- Time: 下午 15:24:35
  6. local http = require "resty.http"
  7. local _M = {
  8.     __version = "0.01"
  9. }
  10. local mt = {__index = _M}
  11. function new(search_config)
  12.     return setmetatable(search_config, mt)
  13. end
  14. function search(self, searchquery)
  15.     local searchquery = ngx.encode_args(searchquery)
  16.     local opensearch_path = "/v3/openapi/apps/"..self.appname.."/search";
  17.     searchquery = string.gsub(searchquery,"'","%%27")
  18.     local headers, err = self:_build_auth_headers(searchquery,opensearch_path)
  19.     local url = "http://" .. headers['Host'] .. opensearch_path ..'?' .. searchquery
  20.     if err then return nil, err end
  21.     local res, err = self:_send_http_request(url, "GET", headers)
  22.     if 200 ~= res.status then
  23.         ngx.say("出错啦")
  24.         ngx.log(ngx.ERR, res.status, err)
  25.         return false
  26.     end
  27.     return res.body
  28. end
  29. function _sign(self, str)
  30.     local key = ngx.encode_base64(ngx.hmac_sha1(self.accesskey_secret, str))
  31.     return 'OPENSEARCH '.. self.accesskey_id .. ':' .. key
  32. end
  33. function _send_http_request(self, url, method, headers, body)
  34.     local httpc = http.new()
  35.     httpc:set_timeout(30000)
  36.     local res, err = httpc:request_uri(url, {
  37.         method = method,
  38.         headers = headers,
  39.         body = body
  40.     })
  41.     httpc:set_keepalive(30000, 10)
  42.     return res, err
  43. end
  44. function _build_auth_headers(self, searchquery,opensearch_path)
  45.     --这里传入的searchquery 是一个table   就是python里面所说的字典形式,比如:{query="query=title:'搜索'&&config=start:0,hit:1,format:json",fetch_fields="id;title"}
  46.     --需要注意
  47.     local utctime = string.gsub(ngx.utctime(ngx.time())," ","T").."Z" --这个是 Date
  48.     math.randomseed(tostring(ngx.now()):reverse():sub(1, 6))
  49.     local nonce = ngx.time()..math.random(10000,99999) --这个是
  50.     local appname = self.appname
  51.     local host = self.internet_host
  52.     local http_params = opensearch_path.."?"..searchquery
  53.     local check_param       =   "GET\n\napplication/json\n"..utctime.."\n".."x-opensearch-nonce:"..nonce.."\n"..http_params
  54.     local headers  =    {
  55.         ['Date']            =    utctime,
  56.         ['X-Opensearch-Nonce']        =    nonce,
  57.         ['Content-Type']        =    "application/json",
  58.         ['Authorization']    =    self:_sign(check_param),
  59.         ['Connection']        =    'keep-alive',
  60.         ['Host']            =   host
  61.     }
  62.     return headers
  63. end
  64. -- public
  65. _M.new = new
  66. _M.search = search
  67. -- private
  68. _M._build_auth_headers = _build_auth_headers
  69. _M._send_http_request = _send_http_request
  70. _M._sign = _sign
  71. return _M
`;}qjm0a  
Xk%eU>d  
GOdWc9Ta!  
GPh;r7xg6  
4uO88[=  
OCdX'HN5Y  
[ 此帖被满脸菊花在2019-01-30 21:25重新编辑 ]
级别: 新人
发帖
3
云币
4
只看该作者 沙发  发表于: 01-30
Re阿里云开放搜索openresty lua sdk
留言试试看!!!!!!!!!! eb:A1f4L  
fZryG  
*7gT}O;p 5  
1JS2SxF  
[attachment=12345]
[ 此帖被满脸菊花在2019-01-30 21:49重新编辑 ]
发表主题 回复主题
« 返回列表上一主题下一主题

限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
验证问题: 4 + 5 = ?
上一个 下一个
      ×
      全新阿里云开发者社区, 去探索开发者的新世界吧!
      一站式的体验,更多的精彩!
      通过下面领域大门,一起探索新的技术世界吧~ (点击图标进入)