Commit d3ddfc64492fdcc3e4332247a845e6f304f13c81

Authored by Andrew Kane
2 parents 06661b2e a677d983

Merge branch 'bogdanovich-master'

CHANGELOG.md
... ... @@ -2,6 +2,7 @@
2 2  
3 3 - Much better performance for where queries if no facets
4 4 - Added basic support for regex
  5 +- Added support for routing
5 6  
6 7 ## 0.8.7
7 8  
... ...
README.md
... ... @@ -647,6 +647,22 @@ Also supports [additional options](http://www.elasticsearch.org/guide/en/elastic
647 647 City.search "san", boost_by_distance: {field: :location, origin: [37, -122], function: :linear, scale: "30mi", decay: 0.5}
648 648 ```
649 649  
  650 +### Routing [master]
  651 +
  652 +Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing).
  653 +
  654 +```ruby
  655 +class Contact < ActiveRecord::Base
  656 + searchkick routing: :user_id
  657 +end
  658 +```
  659 +
  660 +Reindex and search with:
  661 +
  662 +```ruby
  663 +Contact.search "John", routing: current_user.id
  664 +```
  665 +
650 666 ## Inheritance
651 667  
652 668 Searchkick supports single table inheritance.
... ...
lib/searchkick/index.rb
... ... @@ -423,9 +423,15 @@ module Searchkick
423 423 }
424 424 end
425 425  
  426 + routing = {}
  427 + if options[:routing]
  428 + routing = {required: true, path: options[:routing].to_s}
  429 + end
  430 +
426 431 mappings = {
427 432 _default_: {
428 433 properties: mapping,
  434 + _routing: routing,
429 435 # https://gist.github.com/kimchy/2898285
430 436 dynamic_templates: [
431 437 {
... ...
lib/searchkick/query.rb
... ... @@ -379,6 +379,11 @@ module Searchkick
379 379 if options[:type] || klass != searchkick_klass
380 380 @type = [options[:type] || klass].flatten.map { |v| searchkick_index.klass_document_type(v) }
381 381 end
  382 +
  383 + # routing
  384 + if options[:routing]
  385 + @routing = options[:routing]
  386 + end
382 387 end
383 388  
384 389 @body = payload
... ... @@ -407,6 +412,7 @@ module Searchkick
407 412 body: body
408 413 }
409 414 params.merge!(type: @type) if @type
  415 + params.merge!(routing: @routing) if @routing
410 416 params
411 417 end
412 418  
... ...
test/routing_test.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +require_relative "test_helper"
  2 +
  3 +class TestRouting < Minitest::Test
  4 +
  5 + def test_routing_query
  6 + query = Store.search("Dollar Tree", routing: "Dollar Tree", execute: false)
  7 + assert_equal query.params[:routing], "Dollar Tree"
  8 + end
  9 +
  10 + def test_routing_mappings
  11 + index_options = Store.searchkick_index.index_options
  12 + assert_equal index_options[:mappings][:_default_][:_routing], {required: true, path: "name"}
  13 + end
  14 +end
... ...
test/test_helper.rb
... ... @@ -212,12 +212,15 @@ class Product
212 212 end
213 213  
214 214 class Store
215   - searchkick mappings: {
216   - store: {
217   - properties: {
218   - name: {type: "string", analyzer: "keyword"}
  215 + searchkick \
  216 + routing: :name,
  217 + merge_mappings: true,
  218 + mappings: {
  219 + store: {
  220 + properties: {
  221 + name: {type: "string", analyzer: "keyword"},
  222 + }
219 223 }
220   - }
221 224 }
222 225 end
223 226  
... ...