From 223848df5067cbcc8bad345a33592f29c63af377 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 14 Nov 2017 20:53:47 -0800 Subject: [PATCH] ActiveRecord::Base -> ApplicationRecord [skip ci] --- README.md | 76 ++++++++++++++++++++++++++++++++++++++-------------------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index f9b1835..f61b1df 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The latest version works with Elasticsearch 2, 5, and 6. For Elasticsearch 1, us Add searchkick to models you want to search. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick end ``` @@ -238,7 +238,7 @@ Product.search "fresh honey", operator: "or" # fresh OR honey By default, results must match the entire word - `back` will not match `backpack`. You can change this behavior with: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick word_start: [:name] end ``` @@ -282,7 +282,7 @@ User.search "fresh honey", match: :phrase Searchkick defaults to English for stemming. To change this, use: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick language: "german" end ``` @@ -292,7 +292,7 @@ end ### Synonyms ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick synonyms: [["scallion", "green onion"], ["qtip", "cotton swab"]] end ``` @@ -316,7 +316,7 @@ synonyms: ["lightbulb => halogenlamp"] The above approach works well when your synonym list is static, but in practice, this is often not the case. When you analyze search conversions, you often want to add new synonyms or tags without a full reindex. You can use a library like [ActsAsTaggableOn](https://github.com/mbleigh/acts-as-taggable-on) and do: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord acts_as_taggable scope :search_import, -> { includes(:tags) } @@ -350,7 +350,7 @@ mv prolog/wn_s.pl /var/lib Tell each model to use it: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick wordnet: true end ``` @@ -419,7 +419,7 @@ Product.search "🍨🍰", emoji: true Control what data is indexed with the `search_data` method. Call `Product.reindex` after changing this method. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord belongs_to :department def search_data @@ -435,7 +435,7 @@ end Searchkick uses `find_in_batches` to import documents. To eager load associations, use the `search_import` scope. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord scope :search_import, -> { includes(:department) } end ``` @@ -443,7 +443,7 @@ end By default, all records are indexed. To control which records are indexed, use the `should_index?` method together with the `search_import` scope. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord scope :search_import, -> { where(active: true) } def should_index? @@ -485,7 +485,7 @@ There are four strategies for keeping the index synced with your database. Use background jobs for better performance ```ruby - class Product < ActiveRecord::Base + class Product < ApplicationRecord searchkick callbacks: :async end ``` @@ -501,7 +501,7 @@ There are four strategies for keeping the index synced with your database. Turn off automatic syncing ```ruby - class Product < ActiveRecord::Base + class Product < ApplicationRecord searchkick callbacks: false end ``` @@ -527,7 +527,7 @@ end Data is **not** automatically synced when an association is updated. If this is desired, add a callback to reindex: ```ruby -class Image < ActiveRecord::Base +class Image < ApplicationRecord belongs_to :product after_commit :reindex_product @@ -566,7 +566,7 @@ You do **not** need to clean up the search queries. Searchkick automatically tre Next, add conversions to the index. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord has_many :searches, class_name: "Searchjoy::Search", as: :convertable searchkick conversions: ["conversions"] # name of field @@ -594,7 +594,7 @@ rake searchkick:reindex CLASS=Product Order results differently for each user. For example, show a user’s previously purchased products before other results. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord def search_data { name: name, @@ -623,7 +623,7 @@ Autocomplete predicts what a user will type, making the search experience faster First, specify which fields use this feature. This is necessary since autocomplete can increase the index size significantly, but don’t worry - this gives you blazing faster queries. ```ruby -class Movie < ActiveRecord::Base +class Movie < ApplicationRecord searchkick word_start: [:title, :director] end ``` @@ -683,7 +683,7 @@ Then add the search box and JavaScript code to a view. ![Suggest](https://raw.githubusercontent.com/ankane/searchkick/gh-pages/recursion.png) ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick suggest: [:name] # fields to generate suggestions end ``` @@ -821,7 +821,7 @@ Product.search "pear", aggs: {products_per_year: {date_histogram: {field: :creat Specify which fields to index with highlighting. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick highlight: [:name] end ``` @@ -874,7 +874,7 @@ product.similar(fields: [:name], where: {size: "12 oz"}) ### Geospatial Searches ```ruby -class Restaurant < ActiveRecord::Base +class Restaurant < ApplicationRecord searchkick locations: [:location] def search_data @@ -920,7 +920,7 @@ Restaurant.search "wings", boost_by_distance: {location: {origin: {lat: 37, lon: You can also index and search geo shapes. ```ruby -class Restaurant < ActiveRecord::Base +class Restaurant < ApplicationRecord searchkick geo_shape: { bounds: {tree: "geohash", precision: "1km"} } @@ -1172,7 +1172,7 @@ If you run into issues on Windows, check out [this post](https://www.rastating.c By default, all string fields are searchable (can be used in `fields` option). Speed up indexing and reduce index size by only making some fields searchable. This disables the `_all` field unless it’s listed. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick searchable: [:name] end ``` @@ -1182,7 +1182,7 @@ end By default, all string fields are filterable (can be used in `where` option). Speed up indexing and reduce index size by only making some fields filterable. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick filterable: [:brand] end ``` @@ -1267,7 +1267,7 @@ Searchkick.redis = ConnectionPool.new { Redis.new } And ask your models to queue updates. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick callbacks: :queue end ``` @@ -1291,7 +1291,7 @@ For more tips, check out [Keeping Elasticsearch in Sync](https://www.elastic.co/ Searchkick supports [Elasticsearch’s routing feature](https://www.elastic.co/blog/customizing-your-document-routing), which can significantly speed up searches. ```ruby -class Business < ActiveRecord::Base +class Business < ApplicationRecord searchkick routing: true def search_routing @@ -1311,7 +1311,7 @@ Business.search "ice cream", routing: params[:city_id] Reindex a subset of attributes to reduce time spent generating search data and cut down on network traffic. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord def search_data { name: name @@ -1338,7 +1338,7 @@ Product.reindex(:search_prices) Split out conversions into a separate method so you can use partial reindexing, and cache conversions to prevent N+1 queries. Be sure to use a centralized cache store like Memcached or Redis. ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord def search_data { name: name @@ -1403,7 +1403,7 @@ Searchkick makes it easy to use the Elasticsearch DSL on its own. Create a custom mapping: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick mappings: { product: { properties: { @@ -1418,7 +1418,7 @@ end To keep the mappings and settings generated by Searchkick, use: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick merge_mappings: true, mappings: {...} end ``` @@ -1544,7 +1544,7 @@ Product.search_index.clean_indices Use custom settings ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick settings: {number_of_shards: 3} end ``` @@ -1552,7 +1552,7 @@ end Use a different index name ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick index_name: "products_v2" end ``` @@ -1560,7 +1560,7 @@ end Use a dynamic index name ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick index_name: -> { "#{name.tableize}-#{I18n.locale}" } end ``` @@ -1568,7 +1568,7 @@ end Prefix the index name ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick index_prefix: "datakick" end ``` @@ -1588,7 +1588,7 @@ Product.search("banana", conversions_term: "organic banana") Multiple conversion fields ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord has_many :searches, class_name: "Searchjoy::Search" # searchkick also supports multiple "conversions" fields @@ -1653,7 +1653,7 @@ Searchkick.search("*", index_name: [Product, Store], model_includes: {Product = Specify default fields to search ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick default_fields: [:name] end ``` @@ -1661,7 +1661,7 @@ end Turn off special characters ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord # A will not match Ä searchkick special_characters: false end @@ -1670,7 +1670,7 @@ end Use a different [similarity algorithm](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-similarity.html) for scoring ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick similarity: "classic" end ``` @@ -1678,7 +1678,7 @@ end Change import batch size ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick batch_size: 200 # defaults to 1000 end ``` @@ -1705,7 +1705,7 @@ Product.search("carrots", request_params: {search_type: "dfs_query_then_fetch"}) Reindex conditionally ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick callbacks: false # add the callbacks manually @@ -1907,7 +1907,7 @@ Product.search_index.refresh Due to the distributed nature of Elasticsearch, you can get incorrect results when the number of documents in the index is low. You can [read more about it here](https://www.elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-fetch). To fix this, do: ```ruby -class Product < ActiveRecord::Base +class Product < ApplicationRecord searchkick settings: {number_of_shards: 1} end ``` -- libgit2 0.21.0