On Tue, Feb 08, 2022 at 10:24:24AM +0800, Wu Fengguang wrote:
+module EsDataApi
- ES_ACCOUNTS = ESClient.new(index: 'accounts')
- OPEN_INDEX = Set.new(['jobs'])
- REQUIRED_TOKEN_INDEX = Set.new(['jobs'])
- ES_QUERY_KEYWORD = Set.new(['term', 'match'])
2 empty lines can be merged.
ok.
- def self.search(index, params)
- request_body = JSON.parse(params)
- query = request_body['query'] || {'query' => {}}
- return "index is empty!" if index.nil?
- return "#{index} is not opened" unless OPEN_INDEX.include?(index)
- if REQUIRED_TOKEN_INDEX.include?(index)
cci_authorization = request_body['cci_authorization'] || {}
my_account = cci_authorization['my_account']
my_token = cci_authorization['my_token']
raise "user authentication failed" unless verify_user(my_account, my_token)
加上my_account,把错误信息变得友好些。
ok.
user authentication failed,please check my_account and my_token.
函数体有点大。功能型代码块,适合独立拆分出去。
ok.
- def self.verify_user(my_account, my_token)
- raise "missed my_account" unless my_account
- raise "missed my_token" unless my_token
- query = {}
- query['my_account'] = my_account if my_account
- query['my_token'] = my_token if my_token
这里的两个if前面判断过了,可省略
ok
Thanks, Jihui
- result = ES_ACCOUNTS.multi_field_query(query)['hits']['hits']
- return nil if result.size == 0
- result[0]['_source']['my_token'] == my_token
- end
+end
2.23.0