Commit f3c000b1d40fe17cf3e04d7cd558830313a71ac6

Authored by Andrew Kane
1 parent 6de83999

Added support for routing for Elasticsearch 2

CHANGELOG.md
  1 +## 1.2.1 [unreleased]
  2 +
  3 +- Added support for routing for Elasticsearch 2
  4 +
1 5 ## 1.2.0
2 6  
3 7 - Fixed deprecation warnings with `alias_method_chain`
... ...
README.md
... ... @@ -829,15 +829,17 @@ Also supports [additional options](https://www.elastic.co/guide/en/elasticsearch
829 829 City.search "san", boost_by_distance: {field: :location, origin: {lat: 37, lon: -122}, function: :linear, scale: "30mi", decay: 0.5}
830 830 ```
831 831  
832   -### Routing
  832 +### Routing [master]
833 833  
834 834 Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing).
835 835  
836   -**Note:** Routing is not yet supported for Elasticsearch 2.0.
837   -
838 836 ```ruby
839 837 class Contact < ActiveRecord::Base
840   - searchkick routing: :user_id
  838 + searchkick routing: true
  839 +
  840 + def searchkick_routing
  841 + user_id
  842 + end
841 843 end
842 844 ```
843 845  
... ...
lib/searchkick/index.rb
... ... @@ -53,14 +53,24 @@ module Searchkick
53 53 end
54 54  
55 55 def bulk_delete(records)
56   - Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: {_index: name, _type: document_type(r), _id: search_id(r)}} })
  56 + Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: record_data(r)} })
57 57 end
58 58  
59 59 def bulk_index(records)
60   - Searchkick.queue_items(records.map { |r| {index: {_index: name, _type: document_type(r), _id: search_id(r), data: search_data(r)}} })
  60 + Searchkick.queue_items(records.map { |r| {index: record_data(r).merge(data: search_data(r))} })
61 61 end
62 62 alias_method :import, :bulk_index
63 63  
  64 + def record_data(r)
  65 + data = {
  66 + _index: name,
  67 + _id: search_id(r),
  68 + _type: document_type(r)
  69 + }
  70 + data[:_routing] = r.search_routing if r.respond_to?(:search_routing)
  71 + data
  72 + end
  73 +
64 74 def retrieve(record)
65 75 client.get(
66 76 index: name,
... ... @@ -463,7 +473,10 @@ module Searchkick
463 473  
464 474 routing = {}
465 475 if options[:routing]
466   - routing = {required: true, path: options[:routing].to_s}
  476 + routing = {required: true}
  477 + unless options[:routing] == true
  478 + routing[:path] = options[:routing].to_s
  479 + end
467 480 end
468 481  
469 482 dynamic_fields = {
... ...
test/routing_test.rb
... ... @@ -2,14 +2,12 @@ require_relative &quot;test_helper&quot;
2 2  
3 3 class RoutingTest < Minitest::Test
4 4 def test_routing_query
5   - skip if elasticsearch2?
6 5 query = Store.search("Dollar Tree", routing: "Dollar Tree", execute: false)
7 6 assert_equal query.params[:routing], "Dollar Tree"
8 7 end
9 8  
10 9 def test_routing_mappings
11   - skip if elasticsearch2?
12 10 index_options = Store.searchkick_index.index_options
13   - assert_equal index_options[:mappings][:_default_][:_routing], required: true, path: "name"
  11 + assert_equal index_options[:mappings][:_default_][:_routing], required: true
14 12 end
15 13 end
... ...
test/test_helper.rb
... ... @@ -244,7 +244,7 @@ end
244 244  
245 245 class Store
246 246 searchkick \
247   - routing: elasticsearch2? ? false : "name",
  247 + routing: true,
248 248 merge_mappings: true,
249 249 mappings: {
250 250 store: {
... ... @@ -253,6 +253,10 @@ class Store
253 253 }
254 254 }
255 255 }
  256 +
  257 + def search_routing
  258 + name
  259 + end
256 260 end
257 261  
258 262 class Animal
... ...