Commit 1234fda07136715c03b9874896ee426eb3f9072f
1 parent
ea3d385f
Exists in
master
and in
21 other branches
Added default_fields option
Showing
6 changed files
with
53 additions
and
36 deletions
Show diff stats
CHANGELOG.md
README.md
@@ -952,26 +952,6 @@ Containing the query shape (Elasticsearch 2.2+) | @@ -952,26 +952,6 @@ Containing the query shape (Elasticsearch 2.2+) | ||
952 | City.search "san", where: {bounds: {geo_shape: {type: "envelope", relation: "contains", coordinates: [{lat: 38, lon: -123}, {lat: 37, lon: -122}]}}} | 952 | City.search "san", where: {bounds: {geo_shape: {type: "envelope", relation: "contains", coordinates: [{lat: 38, lon: -123}, {lat: 37, lon: -122}]}}} |
953 | ``` | 953 | ``` |
954 | 954 | ||
955 | -### Routing | ||
956 | - | ||
957 | -Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing). | ||
958 | - | ||
959 | -```ruby | ||
960 | -class Business < ActiveRecord::Base | ||
961 | - searchkick routing: true | ||
962 | - | ||
963 | - def search_routing | ||
964 | - city_id | ||
965 | - end | ||
966 | -end | ||
967 | -``` | ||
968 | - | ||
969 | -Reindex and search with: | ||
970 | - | ||
971 | -```ruby | ||
972 | -Business.search "ice cream", routing: params[:city_id] | ||
973 | -``` | ||
974 | - | ||
975 | ## Inheritance | 955 | ## Inheritance |
976 | 956 | ||
977 | Searchkick supports single table inheritance. | 957 | Searchkick supports single table inheritance. |
@@ -1121,7 +1101,33 @@ Then deploy and reindex: | @@ -1121,7 +1101,33 @@ Then deploy and reindex: | ||
1121 | rake searchkick:reindex CLASS=Product | 1101 | rake searchkick:reindex CLASS=Product |
1122 | ``` | 1102 | ``` |
1123 | 1103 | ||
1124 | -### Performance | 1104 | +### Automatic Failover |
1105 | + | ||
1106 | +Create an initializer `config/initializers/elasticsearch.rb` with multiple hosts: | ||
1107 | + | ||
1108 | +```ruby | ||
1109 | +Searchkick.client = Elasticsearch::Client.new(hosts: ["localhost:9200", "localhost:9201"], retry_on_failure: true) | ||
1110 | +``` | ||
1111 | + | ||
1112 | +See [elasticsearch-transport](https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-transport) for a complete list of options. | ||
1113 | + | ||
1114 | +### Lograge | ||
1115 | + | ||
1116 | +Add the following to `config/environments/production.rb`: | ||
1117 | + | ||
1118 | +```ruby | ||
1119 | +config.lograge.custom_options = lambda do |event| | ||
1120 | + options = {} | ||
1121 | + options[:search] = event.payload[:searchkick_runtime] if event.payload[:searchkick_runtime].to_f > 0 | ||
1122 | + options | ||
1123 | +end | ||
1124 | +``` | ||
1125 | + | ||
1126 | +See [Production Rails](https://github.com/ankane/production_rails) for other good practices. | ||
1127 | + | ||
1128 | +## Performance | ||
1129 | + | ||
1130 | +### Persistent HTTP Connections | ||
1125 | 1131 | ||
1126 | For the best performance, add [Typhoeus](https://github.com/typhoeus/typhoeus) to your Gemfile. | 1132 | For the best performance, add [Typhoeus](https://github.com/typhoeus/typhoeus) to your Gemfile. |
1127 | 1133 | ||
@@ -1137,29 +1143,35 @@ Ethon.logger = Logger.new("/dev/null") | @@ -1137,29 +1143,35 @@ Ethon.logger = Logger.new("/dev/null") | ||
1137 | 1143 | ||
1138 | If you run into issues on Windows, check out [this post](https://www.rastating.com/fixing-issues-in-typhoeus-and-httparty-on-windows/). | 1144 | If you run into issues on Windows, check out [this post](https://www.rastating.com/fixing-issues-in-typhoeus-and-httparty-on-windows/). |
1139 | 1145 | ||
1140 | -### Automatic Failover | 1146 | +### _all Field [master] |
1141 | 1147 | ||
1142 | -Create an initializer `config/initializers/elasticsearch.rb` with multiple hosts: | 1148 | +Disable the `_all` field by specifying default fields to search. |
1143 | 1149 | ||
1144 | ```ruby | 1150 | ```ruby |
1145 | -Searchkick.client = Elasticsearch::Client.new(hosts: ["localhost:9200", "localhost:9201"], retry_on_failure: true) | 1151 | +class Product < ActiveRecord::Base |
1152 | + searchkick default_fields: [:name] | ||
1153 | +end | ||
1146 | ``` | 1154 | ``` |
1147 | 1155 | ||
1148 | -See [elasticsearch-transport](https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-transport) for a complete list of options. | ||
1149 | - | ||
1150 | -### Lograge | 1156 | +### Routing |
1151 | 1157 | ||
1152 | -Add the following to `config/environments/production.rb`: | 1158 | +Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing). |
1153 | 1159 | ||
1154 | ```ruby | 1160 | ```ruby |
1155 | -config.lograge.custom_options = lambda do |event| | ||
1156 | - options = {} | ||
1157 | - options[:search] = event.payload[:searchkick_runtime] if event.payload[:searchkick_runtime].to_f > 0 | ||
1158 | - options | 1161 | +class Business < ActiveRecord::Base |
1162 | + searchkick routing: true | ||
1163 | + | ||
1164 | + def search_routing | ||
1165 | + city_id | ||
1166 | + end | ||
1159 | end | 1167 | end |
1160 | ``` | 1168 | ``` |
1161 | 1169 | ||
1162 | -See [Production Rails](https://github.com/ankane/production_rails) for other good practices. | 1170 | +Reindex and search with: |
1171 | + | ||
1172 | +```ruby | ||
1173 | +Business.search "ice cream", routing: params[:city_id] | ||
1174 | +``` | ||
1163 | 1175 | ||
1164 | ## Advanced | 1176 | ## Advanced |
1165 | 1177 |
lib/searchkick/index_options.rb
@@ -338,7 +338,7 @@ module Searchkick | @@ -338,7 +338,7 @@ module Searchkick | ||
338 | 338 | ||
339 | mappings = { | 339 | mappings = { |
340 | _default_: { | 340 | _default_: { |
341 | - _all: {type: default_type, index: "analyzed", analyzer: default_analyzer}, | 341 | + _all: options[:default_fields] ? {enabled: false} : {type: default_type, index: "analyzed", analyzer: default_analyzer}, |
342 | properties: mapping, | 342 | properties: mapping, |
343 | _routing: routing, | 343 | _routing: routing, |
344 | # https://gist.github.com/kimchy/2898285 | 344 | # https://gist.github.com/kimchy/2898285 |
lib/searchkick/query.rb
@@ -479,7 +479,7 @@ module Searchkick | @@ -479,7 +479,7 @@ module Searchkick | ||
479 | 479 | ||
480 | def set_fields | 480 | def set_fields |
481 | boost_fields = {} | 481 | boost_fields = {} |
482 | - fields = options[:fields] || searchkick_options[:searchable] | 482 | + fields = options[:fields] || searchkick_options[:default_fields] || searchkick_options[:searchable] |
483 | fields = | 483 | fields = |
484 | if fields | 484 | if fields |
485 | if options[:autocomplete] | 485 | if options[:autocomplete] |
test/boost_test.rb
@@ -164,6 +164,6 @@ class BoostTest < Minitest::Test | @@ -164,6 +164,6 @@ class BoostTest < Minitest::Test | ||
164 | store_names ["Rex"], Animal | 164 | store_names ["Rex"], Animal |
165 | store_names ["Rexx"], Product | 165 | store_names ["Rexx"], Product |
166 | 166 | ||
167 | - assert_order "Rex", ["Rexx", "Rex"], {index_name: [Animal, Product], indices_boost: {Animal => 1, Product => 200}}, Store | 167 | + assert_order "Rex", ["Rexx", "Rex"], {index_name: [Animal, Product], indices_boost: {Animal => 1, Product => 200}, fields: [:name]}, Store |
168 | end | 168 | end |
169 | end | 169 | end |
test/test_helper.rb
@@ -317,6 +317,7 @@ class Product | @@ -317,6 +317,7 @@ class Product | ||
317 | word_end: [:name], | 317 | word_end: [:name], |
318 | highlight: [:name], | 318 | highlight: [:name], |
319 | searchable: [:name, :color], | 319 | searchable: [:name, :color], |
320 | + default_fields: [:name, :color], | ||
320 | filterable: [:name, :color, :description], | 321 | filterable: [:name, :color, :description], |
321 | # unsearchable: [:description], | 322 | # unsearchable: [:description], |
322 | # only_analyzed: [:alt_description], | 323 | # only_analyzed: [:alt_description], |