From f3c000b1d40fe17cf3e04d7cd558830313a71ac6 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 3 Feb 2016 21:55:43 -0800 Subject: [PATCH] Added support for routing for Elasticsearch 2 --- CHANGELOG.md | 4 ++++ README.md | 10 ++++++---- lib/searchkick/index.rb | 19 ++++++++++++++++--- test/routing_test.rb | 4 +--- test/test_helper.rb | 6 +++++- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 452b116..de49b02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.1 [unreleased] + +- Added support for routing for Elasticsearch 2 + ## 1.2.0 - Fixed deprecation warnings with `alias_method_chain` diff --git a/README.md b/README.md index e671b5d..7320538 100644 --- a/README.md +++ b/README.md @@ -829,15 +829,17 @@ Also supports [additional options](https://www.elastic.co/guide/en/elasticsearch City.search "san", boost_by_distance: {field: :location, origin: {lat: 37, lon: -122}, function: :linear, scale: "30mi", decay: 0.5} ``` -### Routing +### Routing [master] Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing). -**Note:** Routing is not yet supported for Elasticsearch 2.0. - ```ruby class Contact < ActiveRecord::Base - searchkick routing: :user_id + searchkick routing: true + + def searchkick_routing + user_id + end end ``` diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index f2b0841..1e88ae9 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -53,14 +53,24 @@ module Searchkick end def bulk_delete(records) - Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: {_index: name, _type: document_type(r), _id: search_id(r)}} }) + Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: record_data(r)} }) end def bulk_index(records) - Searchkick.queue_items(records.map { |r| {index: {_index: name, _type: document_type(r), _id: search_id(r), data: search_data(r)}} }) + Searchkick.queue_items(records.map { |r| {index: record_data(r).merge(data: search_data(r))} }) end alias_method :import, :bulk_index + def record_data(r) + data = { + _index: name, + _id: search_id(r), + _type: document_type(r) + } + data[:_routing] = r.search_routing if r.respond_to?(:search_routing) + data + end + def retrieve(record) client.get( index: name, @@ -463,7 +473,10 @@ module Searchkick routing = {} if options[:routing] - routing = {required: true, path: options[:routing].to_s} + routing = {required: true} + unless options[:routing] == true + routing[:path] = options[:routing].to_s + end end dynamic_fields = { diff --git a/test/routing_test.rb b/test/routing_test.rb index 3b40162..058ad10 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -2,14 +2,12 @@ require_relative "test_helper" class RoutingTest < Minitest::Test def test_routing_query - skip if elasticsearch2? query = Store.search("Dollar Tree", routing: "Dollar Tree", execute: false) assert_equal query.params[:routing], "Dollar Tree" end def test_routing_mappings - skip if elasticsearch2? index_options = Store.searchkick_index.index_options - assert_equal index_options[:mappings][:_default_][:_routing], required: true, path: "name" + assert_equal index_options[:mappings][:_default_][:_routing], required: true end end diff --git a/test/test_helper.rb b/test/test_helper.rb index de24db6..b4f1a86 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -244,7 +244,7 @@ end class Store searchkick \ - routing: elasticsearch2? ? false : "name", + routing: true, merge_mappings: true, mappings: { store: { @@ -253,6 +253,10 @@ class Store } } } + + def search_routing + name + end end class Animal -- libgit2 0.21.0