Commit 9a95d9f4efcdbe8ae2e77b2232fdb33ef8f8dd63

Authored by Andrew Kane
1 parent da93f5b9

Added fields method

README.md
... ... @@ -94,7 +94,7 @@ Product.search("apples").where(in_stock: true).limit(10).offset(50)
94 94 Search specific fields
95 95  
96 96 ```ruby
97   -fields: [:name, :brand]
  97 +fields(:name, :brand)
98 98 ```
99 99  
100 100 Where
... ... @@ -181,7 +181,7 @@ results.response
181 181 Boost important fields
182 182  
183 183 ```ruby
184   -fields: ["title^10", "description"]
  184 +fields("title^10", "description")
185 185 ```
186 186  
187 187 Boost by the value of a field (field must be numeric)
... ...
lib/searchkick/relation.rb
... ... @@ -109,6 +109,15 @@ module Searchkick
109 109 self
110 110 end
111 111  
  112 + def fields(*fields)
  113 + spawn.fields!(*fields)
  114 + end
  115 +
  116 + def fields!(*fields)
  117 + options[:fields] = Array(options[:fields]) + fields
  118 + self
  119 + end
  120 +
112 121 # same as Active Record
113 122 def inspect
114 123 entries = results.first(11).map!(&:inspect)
... ...
test/sql_test.rb
... ... @@ -29,6 +29,7 @@ class SqlTest < Minitest::Test
29 29 {name: "blue", color: "red fish"}
30 30 ]
31 31 assert_search "blue", ["red"], fields: ["color"]
  32 + assert_equal ["red"], Product.search("blue", relation: true).fields(:color).map(&:name)
32 33 end
33 34  
34 35 def test_non_existent_field
... ...