Commit 4fb582e914772368b956baa6c20e0304da5a58bf
1 parent
b89a92ee
Exists in
master
and in
21 other branches
Changed book to product
Showing
1 changed file
with
17 additions
and
17 deletions
Show diff stats
README.md
... | ... | @@ -17,7 +17,7 @@ Runs on Elasticsearch |
17 | 17 | ## Usage |
18 | 18 | |
19 | 19 | ```ruby |
20 | -class Book < ActiveRecord::Base | |
20 | +class Product < ActiveRecord::Base | |
21 | 21 | searchkick :name |
22 | 22 | end |
23 | 23 | ``` |
... | ... | @@ -25,18 +25,18 @@ end |
25 | 25 | And to query, use: |
26 | 26 | |
27 | 27 | ```ruby |
28 | -Book.search("Nobody Listens to Andrew") | |
28 | +Product.search("2% Milk") | |
29 | 29 | ``` |
30 | 30 | |
31 | 31 | ### Synonyms |
32 | 32 | |
33 | 33 | ```ruby |
34 | -class Book < ActiveRecord::Base | |
34 | +class Product < ActiveRecord::Base | |
35 | 35 | searchkick :name, synonyms: ["scallion => green onion"] # TODO Ruby syntax |
36 | 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 | 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 | 48 | |
49 | 49 | ```ruby |
50 | 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 | 53 | end |
54 | 54 | ``` |
55 | 55 | |
56 | 56 | Add the conversions to the index. |
57 | 57 | |
58 | 58 | ```ruby |
59 | -class Book < ActiveRecord::Base | |
59 | +class Product < ActiveRecord::Base | |
60 | 60 | has_many :searches |
61 | 61 | |
62 | 62 | searchkick :name, conversions: true |
63 | 63 | |
64 | 64 | def to_indexed_json |
65 | 65 | { |
66 | - title: title, | |
66 | + name: name, | |
67 | 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 | 70 | end |
71 | 71 | end |
... | ... | @@ -74,7 +74,7 @@ end |
74 | 74 | After the reindex is complete (to prevent errors), tell the search query to use conversions. |
75 | 75 | |
76 | 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 | 80 | ### Zero Downtime Changes |
... | ... | @@ -82,25 +82,25 @@ Book.search("Nobody Listens to Andrew", conversions: true) |
82 | 82 | Elasticsearch has a feature called aliases that allows you to change mappings with no downtime. |
83 | 83 | |
84 | 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 | 92 | Searchkick uses `find_in_batches` to import documents. To filter documents or eagar load associations, use the `searchkick_import` scope. |
93 | 93 | |
94 | 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 | 97 | end |
98 | 98 | ``` |
99 | 99 | |
100 | 100 | There is also a rake task. |
101 | 101 | |
102 | 102 | ```sh |
103 | -rake searchkick:reindex CLASS=Book | |
103 | +rake searchkick:reindex CLASS=Product | |
104 | 104 | ``` |
105 | 105 | |
106 | 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 | 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 | 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 | 118 | ### Inconsistent Scores | ... | ... |