From e9727422ba264349cdc136b14477e29fb0f27466 Mon Sep 17 00:00:00 2001 From: Luciano Prestes Cavalcanti Date: Thu, 14 Apr 2016 14:22:22 +0000 Subject: [PATCH] Create elasticsearch structure --- plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb | 6 ++++++ plugins/elasticsearch/lib/ext/community.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/elasticsearch/views/elasticsearch_plugin/communities.html.erb | 8 ++++++++ 3 files changed, 60 insertions(+), 0 deletions(-) create mode 100644 plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb create mode 100644 plugins/elasticsearch/lib/ext/community.rb create mode 100644 plugins/elasticsearch/views/elasticsearch_plugin/communities.html.erb diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb new file mode 100644 index 0000000..92cbf8b --- /dev/null +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -0,0 +1,6 @@ +class ElasticsearchPluginController < ApplicationController + + def communities + @communities = environment.communities + end +end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb new file mode 100644 index 0000000..f921f0b --- /dev/null +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -0,0 +1,46 @@ +require_dependency 'community' + +require 'elasticsearch/model' + +Community.class_eval do + include Elasticsearch::Model + include Elasticsearch::Model::Callbacks + + def self.search(query) + __elasticsearch__.search( + { + query: { + multi_match: { + query: query, + fields: ['name', 'identifier'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + name: {}, + identifier: {} + } + } + } + ) + end + + settings index: { number_of_shards: 1 } do + mappings dynamic: 'false' do + indexes :name, analyzer: 'english', index_options: 'offsets' + indexes :identifier, analyzer: 'english' + end + end +end + +Community.__elasticsearch__.client.indices.delete index: Community.index_name rescue nil + +# Create the new index with the new mapping +Community.__elasticsearch__.client.indices.create \ + index: Community.index_name, + body: { settings: Community.settings.to_hash, mappings: Community.mappings.to_hash } + +# Index all Community records from the DB to Elasticsearch +Community.import diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/communities.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/communities.html.erb new file mode 100644 index 0000000..4fb51b0 --- /dev/null +++ b/plugins/elasticsearch/views/elasticsearch_plugin/communities.html.erb @@ -0,0 +1,8 @@ +

Communities

+ +<% @communities.each do |community| %> + + <%= community.name %> +
+ +<% end %> -- libgit2 0.21.2