From 10138016fe9d5bf3d43422687cc931e527117cfb Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Mar 2018 13:12:26 -0800 Subject: [PATCH] Better organization [skip ci] --- lib/searchkick/index.rb | 90 +++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 5ae3fd8..10fb544 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -54,6 +54,23 @@ module Searchkick client.indices.put_settings index: name, body: settings end + def tokens(text, options = {}) + client.indices.analyze(body: {text: text}.merge(options), index: name)["tokens"].map { |t| t["token"] } + end + + def total_docs + response = + client.search( + index: name, + body: { + query: {match_all: {}}, + size: 0 + } + ) + + response["hits"]["total"] + end + def promote(new_name, update_refresh_interval: false) if update_refresh_interval new_index = Searchkick::Index.new(new_name) @@ -73,6 +90,34 @@ module Searchkick end alias_method :swap, :promote + def retrieve(record) + client.get( + index: name, + type: document_type(record), + id: search_id(record) + )["_source"] + end + + def all_indices(unaliased: false) + indices = + begin + client.indices.get_aliases + rescue Elasticsearch::Transport::Transport::Errors::NotFound + {} + end + indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased + indices.select { |k, _v| k =~ /\A#{Regexp.escape(name)}_\d{14,17}\z/ }.keys + end + + # remove old indices that start w/ index_name + def clean_indices + indices = all_indices(unaliased: true) + indices.each do |index| + Searchkick::Index.new(index).delete + end + indices + end + # record based # use helpers for notifications @@ -101,14 +146,6 @@ module Searchkick bulk_indexer.bulk_update(records, method_name) end - def retrieve(record) - client.get( - index: name, - type: document_type(record), - id: search_id(record) - )["_source"] - end - def search_id(record) RecordData.new(self, record).search_id end @@ -169,39 +206,6 @@ module Searchkick index end - def all_indices(unaliased: false) - indices = - begin - client.indices.get_aliases - rescue Elasticsearch::Transport::Transport::Errors::NotFound - {} - end - indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased - indices.select { |k, _v| k =~ /\A#{Regexp.escape(name)}_\d{14,17}\z/ }.keys - end - - # remove old indices that start w/ index_name - def clean_indices - indices = all_indices(unaliased: true) - indices.each do |index| - Searchkick::Index.new(index).delete - end - indices - end - - def total_docs - response = - client.search( - index: name, - body: { - query: {match_all: {}}, - size: 0 - } - ) - - response["hits"]["total"] - end - def import_scope(scope, **options) bulk_indexer.import_scope(scope, **options) end @@ -212,10 +216,6 @@ module Searchkick # other - def tokens(text, options = {}) - client.indices.analyze(body: {text: text}.merge(options), index: name)["tokens"].map { |t| t["token"] } - end - def klass_document_type(klass, ignore_type = false) @klass_document_type[[klass, ignore_type]] ||= begin if !ignore_type && klass.searchkick_klass.searchkick_options[:_type] -- libgit2 0.21.0