Commit 7f0f08d618dc636be2ad02d8104b41d3132ab703
1 parent
eeba2dd6
Exists in
elasticsearch_api
Adding community.rb and fixing searchable_fields
Signed-off-by: Daniel Henrique <danielhmarinho@gmail.com> Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Showing
2 changed files
with
26 additions
and
11 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
@@ -13,10 +13,14 @@ class ElasticsearchPluginController < ApplicationController | @@ -13,10 +13,14 @@ class ElasticsearchPluginController < ApplicationController | ||
13 | 13 | ||
14 | if params[:model].present? | 14 | if params[:model].present? |
15 | params[:model].keys.each do |model| | 15 | params[:model].keys.each do |model| |
16 | + model = model.singularize | ||
16 | @checkbox[model.to_sym] = true | 17 | @checkbox[model.to_sym] = true |
17 | klass = model.classify.constantize | 18 | klass = model.classify.constantize |
18 | query = get_query params[:q], klass | 19 | query = get_query params[:q], klass |
20 | + | ||
19 | @results |= klass.__elasticsearch__.search(query).records.to_a | 21 | @results |= klass.__elasticsearch__.search(query).records.to_a |
22 | + puts "="*80 | ||
23 | + puts @results.inspect | ||
20 | end | 24 | end |
21 | end | 25 | end |
22 | 26 | ||
@@ -27,28 +31,29 @@ class ElasticsearchPluginController < ApplicationController | @@ -27,28 +31,29 @@ class ElasticsearchPluginController < ApplicationController | ||
27 | def get_query text, klass | 31 | def get_query text, klass |
28 | query = {} | 32 | query = {} |
29 | unless text.blank? | 33 | unless text.blank? |
34 | + puts "="*80 | ||
30 | 35 | ||
31 | - fields = klass.indexable_fields.map do |key, value| | 36 | + fields = klass::SEARCHABLE_FIELDS.map do |key, value| |
32 | if value[:weight] | 37 | if value[:weight] |
33 | - "#{k}^#{v[:weight]}" | 38 | + "#{key}^#{value[:weight]}" |
34 | else | 39 | else |
35 | - "#{k}" | 40 | + "#{key}" |
36 | end | 41 | end |
37 | end | 42 | end |
38 | 43 | ||
39 | query = { | 44 | query = { |
40 | query: { | 45 | query: { |
41 | - multi_match: { | ||
42 | - query: text, | ||
43 | - fields: fields, | ||
44 | - operator: "and" | 46 | + regexp: { |
47 | + name: { | ||
48 | + value: "*" + text + "*" | ||
49 | + | ||
50 | + } | ||
51 | + } | ||
45 | } | 52 | } |
46 | - }, | ||
47 | - filter: { | ||
48 | - term: {visible: "true"} | ||
49 | - } | ||
50 | } | 53 | } |
51 | end | 54 | end |
55 | + puts "="*80 | ||
56 | + puts query.inspect | ||
52 | query | 57 | query |
53 | end | 58 | end |
54 | 59 |