Commit 4fb582e914772368b956baa6c20e0304da5a58bf

Authored by Andrew Kane
1 parent b89a92ee

Changed book to product

Showing 1 changed file with 17 additions and 17 deletions   Show diff stats
@@ -17,7 +17,7 @@ Runs on Elasticsearch @@ -17,7 +17,7 @@ Runs on Elasticsearch
17 ## Usage 17 ## Usage
18 18
19 ```ruby 19 ```ruby
20 -class Book < ActiveRecord::Base 20 +class Product < ActiveRecord::Base
21 searchkick :name 21 searchkick :name
22 end 22 end
23 ``` 23 ```
@@ -25,18 +25,18 @@ end @@ -25,18 +25,18 @@ end
25 And to query, use: 25 And to query, use:
26 26
27 ```ruby 27 ```ruby
28 -Book.search("Nobody Listens to Andrew") 28 +Product.search("2% Milk")
29 ``` 29 ```
30 30
31 ### Synonyms 31 ### Synonyms
32 32
33 ```ruby 33 ```ruby
34 -class Book < ActiveRecord::Base 34 +class Product < ActiveRecord::Base
35 searchkick :name, synonyms: ["scallion => green onion"] # TODO Ruby syntax 35 searchkick :name, synonyms: ["scallion => green onion"] # TODO Ruby syntax
36 end 36 end
37 ``` 37 ```
38 38
39 -You must call `Book.reindex` after changing synonyms. 39 +You must call `Product.reindex` after changing synonyms.
40 40
41 ### Make Searches Better Over Time 41 ### Make Searches Better Over Time
42 42
@@ -48,24 +48,24 @@ Keep track of searches. The database works well for low volume, but feel free t @@ -48,24 +48,24 @@ Keep track of searches. The database works well for low volume, but feel free t
48 48
49 ```ruby 49 ```ruby
50 class Search < ActiveRecord::Base 50 class Search < ActiveRecord::Base
51 - belongs_to :item  
52 - # fields: id, query, searched_at, converted_at, item_id 51 + belongs_to :product
  52 + # fields: id, query, searched_at, converted_at, product_id
53 end 53 end
54 ``` 54 ```
55 55
56 Add the conversions to the index. 56 Add the conversions to the index.
57 57
58 ```ruby 58 ```ruby
59 -class Book < ActiveRecord::Base 59 +class Product < ActiveRecord::Base
60 has_many :searches 60 has_many :searches
61 61
62 searchkick :name, conversions: true 62 searchkick :name, conversions: true
63 63
64 def to_indexed_json 64 def to_indexed_json
65 { 65 {
66 - title: title, 66 + name: name,
67 conversions: searches.group("query").count.map{|query, count| {query: query, count: count} }, # TODO fix 67 conversions: searches.group("query").count.map{|query, count| {query: query, count: count} }, # TODO fix
68 - _boost: Math.log(copies_sold_count) # boost more popular books a bit 68 + _boost: Math.log(orders_count) # boost more popular products a bit
69 } 69 }
70 end 70 end
71 end 71 end
@@ -74,7 +74,7 @@ end @@ -74,7 +74,7 @@ end
74 After the reindex is complete (to prevent errors), tell the search query to use conversions. 74 After the reindex is complete (to prevent errors), tell the search query to use conversions.
75 75
76 ```ruby 76 ```ruby
77 -Book.search("Nobody Listens to Andrew", conversions: true) 77 +Product.search("Nobody Listens to Andrew", conversions: true)
78 ``` 78 ```
79 79
80 ### Zero Downtime Changes 80 ### Zero Downtime Changes
@@ -82,25 +82,25 @@ Book.search(&quot;Nobody Listens to Andrew&quot;, conversions: true) @@ -82,25 +82,25 @@ Book.search(&quot;Nobody Listens to Andrew&quot;, conversions: true)
82 Elasticsearch has a feature called aliases that allows you to change mappings with no downtime. 82 Elasticsearch has a feature called aliases that allows you to change mappings with no downtime.
83 83
84 ```ruby 84 ```ruby
85 -Book.reindex 85 +Product.reindex
86 ``` 86 ```
87 87
88 -This creates a new index `books_20130714181054` and points the `books` alias to the new index when complete - an atomic operation :) 88 +This creates a new index `products_20130714181054` and points the `products` alias to the new index when complete - an atomic operation :)
89 89
90 -**First time:** If books is an existing index, it will be replaced by an alias. 90 +**First time:** If products is an existing index, it will be replaced by an alias.
91 91
92 Searchkick uses `find_in_batches` to import documents. To filter documents or eagar load associations, use the `searchkick_import` scope. 92 Searchkick uses `find_in_batches` to import documents. To filter documents or eagar load associations, use the `searchkick_import` scope.
93 93
94 ```ruby 94 ```ruby
95 -class Book < ActiveRecord::Base  
96 - scope :searchkick_import, where(active: true).includes(:author, :chapters) 95 +class Product < ActiveRecord::Base
  96 + scope :searchkick_import, where(active: true).includes(:searches)
97 end 97 end
98 ``` 98 ```
99 99
100 There is also a rake task. 100 There is also a rake task.
101 101
102 ```sh 102 ```sh
103 -rake searchkick:reindex CLASS=Book 103 +rake searchkick:reindex CLASS=Product
104 ``` 104 ```
105 105
106 Thanks to Jaroslav Kalistsuk for the [original implementation](https://gist.github.com/jarosan/3124884) and Clinton Gormley for a [good post](http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/) on this. 106 Thanks to Jaroslav Kalistsuk for the [original implementation](https://gist.github.com/jarosan/3124884) and Clinton Gormley for a [good post](http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/) on this.
@@ -112,7 +112,7 @@ Thanks to Jaroslav Kalistsuk for the [original implementation](https://gist.gith @@ -112,7 +112,7 @@ Thanks to Jaroslav Kalistsuk for the [original implementation](https://gist.gith
112 When changing the mapping in a model, you must create a new index for the changes to take place. Elasticsearch does not support updates to mappings. For zero downtime, use the `reindex` method above, which creates a new index and swaps it in after it's built. To view the current mapping, use: 112 When changing the mapping in a model, you must create a new index for the changes to take place. Elasticsearch does not support updates to mappings. For zero downtime, use the `reindex` method above, which creates a new index and swaps it in after it's built. To view the current mapping, use:
113 113
114 ```sh 114 ```sh
115 -curl "http://localhost:9200/books/_mapping?pretty=1" 115 +curl "http://localhost:9200/products/_mapping?pretty=1"
116 ``` 116 ```
117 117
118 ### Inconsistent Scores 118 ### Inconsistent Scores