es-elasticity 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a45df14b8d96ab9f324461a6c62c226c3166b03c
4
- data.tar.gz: 78f6f7b959299ddd2836676e236f5e59e653d37d
3
+ metadata.gz: 92bedfd2fa34df6c303c9c0945ba55c7613c7a31
4
+ data.tar.gz: bc68cc7bd8bd36ec8d195a53df1684871b20caec
5
5
  SHA512:
6
- metadata.gz: 86bd2492bb54b2286b380635e9b09fd2a4f1b701ad57648b34b703c1c708d156ae6e1765867d213b539f47d1072003be2a92c00a600a0c083b0be08b30d81ff4
7
- data.tar.gz: c2197c269b55a016880aedd536c85945d0ba9978f32adb19d434da68a3bc22bc7d6dda8fe55f6def39f4ad6fee5cb7671c81b645e4489572a5fb17247f638f0a
6
+ metadata.gz: 87b00d0798444997de4c2fe057a5cd1c7bd4990a38b3cecaa78795fb0dd1a069ab3aff3311df61abfd828175e1db9f7cebb66a36007c643d68b4a4cfc883b92e
7
+ data.tar.gz: ea32e3c290a7ec6238fc64e43f0b2792ea259ab444ac3868eb8a9ff1944ff05c59cd2fdd1de410a9353eb91db52d98a3a8cd811d7c7751534e884628ed30deb0
@@ -23,13 +23,6 @@ module Elasticity
23
23
  self.index.delete
24
24
  end
25
25
 
26
- # The index name to be used for indexing and storing data for this document model.
27
- # By default, it's the class name converted to underscore and plural.
28
- def self.index_name
29
- return @index_name if defined?(@index_name)
30
- @index_name = self.name.underscore.pluralize
31
- end
32
-
33
26
  # Sets the index name to something else than the default
34
27
  def self.index_name=(name)
35
28
  @index_name = name
@@ -38,7 +31,7 @@ module Elasticity
38
31
 
39
32
  # Namespaced index name
40
33
  def self.namespaced_index_name
41
- name = self.index_name
34
+ name = @index_name || self.name.underscore.pluralize
42
35
 
43
36
  if namespace = Elasticity.config.namespace
44
37
  name = "#{namespace}_#{name}"
@@ -99,7 +92,7 @@ module Elasticity
99
92
  end
100
93
 
101
94
  # Define common attributes for all documents
102
- attr_accessor :_id
95
+ attr_accessor :_id, :highlighted
103
96
 
104
97
  # Creates a new Document instance with the provided attributes.
105
98
  def initialize(attributes = {})
@@ -114,6 +107,7 @@ module Elasticity
114
107
 
115
108
  # Defines equality by comparing the ID and values of each instance variable.
116
109
  def ==(other)
110
+ return false if other.nil?
117
111
  return false if _id != other._id
118
112
 
119
113
  instance_variables.all? do |ivar|
@@ -99,7 +99,23 @@ module Elasticity
99
99
 
100
100
  def map(hits)
101
101
  hits.map do |hit|
102
- @document_klass.new(hit["_source"].merge(_id: hit['_id']))
102
+ attrs = hit["_source"].merge(_id: hit['_id'])
103
+
104
+ if hit["highlight"]
105
+ highlighted_attrs = attrs.dup
106
+ attrs_set = Set.new
107
+
108
+ hit["highlight"].each do |name, v|
109
+ name = name.gsub(/\..*\z/, '')
110
+ next if attrs_set.include?(name)
111
+ highlighted_attrs[name] = v
112
+ attrs_set << name
113
+ end
114
+
115
+ highlighted = @document_klass.new(highlighted_attrs)
116
+ end
117
+
118
+ @document_klass.new(attrs.merge(highlighted: highlighted))
103
119
  end
104
120
  end
105
121
  end
@@ -1,3 +1,3 @@
1
1
  module Elasticity
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -47,7 +47,6 @@ RSpec.describe Elasticity::Document do
47
47
  subject { klass }
48
48
 
49
49
  it "extracts index name and document type from the class name" do
50
- expect(subject.index_name).to eq "class_names"
51
50
  expect(subject.namespaced_index_name).to eq "elasticity_test_class_names"
52
51
  expect(subject.document_type).to eq "class_name"
53
52
  expect(subject.index.name).to eq "elasticity_test_class_names"
@@ -5,7 +5,7 @@ RSpec.describe Elasticity::MultiSearch do
5
5
  let :klass do
6
6
  Class.new do
7
7
  include ActiveModel::Model
8
- attr_accessor :_id, :name
8
+ attr_accessor :_id, :name, :highlighted
9
9
 
10
10
  def ==(other)
11
11
  self._id == other._id && self.name == other.name
@@ -23,10 +23,16 @@ RSpec.describe "Search" do
23
23
  { "hits" => { "total" => 0, "hits" => [] }}
24
24
  end
25
25
 
26
+ let :highlight_response do
27
+ { "hits" => { "total" => 1, "hits" => [
28
+ { "_id" => 1, "_source" => { "name" => "foo", "age" => 21 }, "highlight" => { "name" => "<em>foo</em>" } },
29
+ ]}}
30
+ end
31
+
26
32
  let :klass do
27
33
  Class.new do
28
34
  include ActiveModel::Model
29
- attr_accessor :_id, :name
35
+ attr_accessor :_id, :name, :age, :highlighted
30
36
 
31
37
  def ==(other)
32
38
  self._id == other._id && self.name == other.name
@@ -82,6 +88,14 @@ RSpec.describe "Search" do
82
88
 
83
89
  expect(subject.active_records(relation).mapping).to be relation
84
90
  end
91
+
92
+ it "creates highlighted object for documents" do
93
+ expect(index).to receive(:search).with(document_type, body).and_return(highlight_response)
94
+ doc = subject.documents(klass).first
95
+
96
+ expect(doc).to_not be_nil
97
+ expect(doc.highlighted).to eq klass.new(_id: 1, name: "<em>foo</em>", age: 21)
98
+ end
85
99
  end
86
100
 
87
101
  describe Elasticity::DocumentSearchProxy do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es-elasticity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-09 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler