Signed-off-by: cuiyili 2268260388@qq.com --- lib/es_query.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
diff --git a/lib/es_query.rb b/lib/es_query.rb index c027aa9..3827ea8 100644 --- a/lib/es_query.rb +++ b/lib/es_query.rb @@ -11,6 +11,7 @@ class ESQuery PORT = (ENV.key?('ES_PORT') ? ENV['ES_PORT'] : ES_PORT).to_i def initialize(host = HOST, port = PORT, index: 'jobs') @index = index + @scroll_id = '' @client = Elasticsearch::Client.new url: "http://#%7Bhost%7D:#%7Bport%7D" raise 'Connect Elasticsearch error!' unless @client.ping end @@ -33,6 +34,25 @@ class ESQuery @client.search index: 'jobs*', body: query end
+ def traverse_field(size) + if @scroll_id.empty? + query = { + query: { + bool: { + must: { + match_all: {} + } + } + }, size: size + } + result = @client.search index: @index, scroll: '10m', body: query + @scroll_id = result['_scroll_id'] + return result + else + @client.scroll scroll: '10m', scroll_id: @scroll_id + end + end + def query_by_id(id) @client.get_source({ index: @index, type: '_doc', id: id }) rescue Elasticsearch::Transport::Transport::Errors::NotFound