Commit 435d9634e37d1663576035e937b48597455eaf49
1 parent
af6c676a
Exists in
master
and in
8 other branches
Moved testing section [skip ci]
Showing
1 changed file
with
136 additions
and
136 deletions
Show diff stats
README.md
... | ... | @@ -36,11 +36,11 @@ Plus: |
36 | 36 | - [Intelligent Search](#intelligent-search) |
37 | 37 | - [Instant Search / Autocomplete](#instant-search--autocomplete) |
38 | 38 | - [Aggregations](#aggregations) |
39 | +- [Testing](#testing) | |
39 | 40 | - [Deployment](#deployment) |
40 | 41 | - [Performance](#performance) |
41 | 42 | - [Elasticsearch DSL](#advanced) |
42 | 43 | - [Reference](#reference) |
43 | -- [Testing](#testing) | |
44 | 44 | |
45 | 45 | ## Getting Started |
46 | 46 | |
... | ... | @@ -1044,6 +1044,137 @@ Product.search_index.tokens("dieg", analyzer: "searchkick_word_search") |
1044 | 1044 | |
1045 | 1045 | See the [complete list of analyzers](https://github.com/ankane/searchkick/blob/31780ddac7a89eab1e0552a32b403f2040a37931/lib/searchkick/index_options.rb#L32). |
1046 | 1046 | |
1047 | +## Testing | |
1048 | + | |
1049 | +For performance, only enable Searchkick callbacks for the tests that need it. | |
1050 | + | |
1051 | +### Parallel Tests | |
1052 | + | |
1053 | +Rails 6 enables parallel tests by default. Add to your `test/test_helper.rb`: | |
1054 | + | |
1055 | +```ruby | |
1056 | +class ActiveSupport::TestCase | |
1057 | + parallelize_setup do |worker| | |
1058 | + Searchkick.index_suffix = worker | |
1059 | + | |
1060 | + # reindex models | |
1061 | + Product.reindex | |
1062 | + | |
1063 | + # and disable callbacks | |
1064 | + Searchkick.disable_callbacks | |
1065 | + end | |
1066 | +end | |
1067 | +``` | |
1068 | + | |
1069 | +And use: | |
1070 | + | |
1071 | +```ruby | |
1072 | +class ProductTest < ActiveSupport::TestCase | |
1073 | + def setup | |
1074 | + Searchkick.enable_callbacks | |
1075 | + end | |
1076 | + | |
1077 | + def teardown | |
1078 | + Searchkick.disable_callbacks | |
1079 | + end | |
1080 | + | |
1081 | + def test_search | |
1082 | + Product.create!(name: "Apple") | |
1083 | + Product.search_index.refresh | |
1084 | + assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1085 | + end | |
1086 | +end | |
1087 | +``` | |
1088 | + | |
1089 | +### Minitest | |
1090 | + | |
1091 | +Add to your `test/test_helper.rb`: | |
1092 | + | |
1093 | +```ruby | |
1094 | +# reindex models | |
1095 | +Product.reindex | |
1096 | + | |
1097 | +# and disable callbacks | |
1098 | +Searchkick.disable_callbacks | |
1099 | +``` | |
1100 | + | |
1101 | +And use: | |
1102 | + | |
1103 | +```ruby | |
1104 | +class ProductTest < Minitest::Test | |
1105 | + def setup | |
1106 | + Searchkick.enable_callbacks | |
1107 | + end | |
1108 | + | |
1109 | + def teardown | |
1110 | + Searchkick.disable_callbacks | |
1111 | + end | |
1112 | + | |
1113 | + def test_search | |
1114 | + Product.create!(name: "Apple") | |
1115 | + Product.search_index.refresh | |
1116 | + assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1117 | + end | |
1118 | +end | |
1119 | +``` | |
1120 | + | |
1121 | +### RSpec | |
1122 | + | |
1123 | +Add to your `spec/spec_helper.rb`: | |
1124 | + | |
1125 | +```ruby | |
1126 | +RSpec.configure do |config| | |
1127 | + config.before(:suite) do | |
1128 | + # reindex models | |
1129 | + Product.reindex | |
1130 | + | |
1131 | + # and disable callbacks | |
1132 | + Searchkick.disable_callbacks | |
1133 | + end | |
1134 | + | |
1135 | + config.around(:each, search: true) do |example| | |
1136 | + Searchkick.callbacks(nil) do | |
1137 | + example.run | |
1138 | + end | |
1139 | + end | |
1140 | +end | |
1141 | +``` | |
1142 | + | |
1143 | +And use: | |
1144 | + | |
1145 | +```ruby | |
1146 | +describe Product, search: true do | |
1147 | + it "searches" do | |
1148 | + Product.create!(name: "Apple") | |
1149 | + Product.search_index.refresh | |
1150 | + assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1151 | + end | |
1152 | +end | |
1153 | +``` | |
1154 | + | |
1155 | +### Factory Bot | |
1156 | + | |
1157 | +Use a trait and an after `create` hook for each indexed model: | |
1158 | + | |
1159 | +```ruby | |
1160 | +FactoryBot.define do | |
1161 | + factory :product do | |
1162 | + # ... | |
1163 | + | |
1164 | + # Note: This should be the last trait in the list so `reindex` is called | |
1165 | + # after all the other callbacks complete. | |
1166 | + trait :reindex do | |
1167 | + after(:create) do |product, _evaluator| | |
1168 | + product.reindex(refresh: true) | |
1169 | + end | |
1170 | + end | |
1171 | + end | |
1172 | +end | |
1173 | + | |
1174 | +# use it | |
1175 | +FactoryBot.create(:product, :some_trait, :reindex, some_attribute: "foo") | |
1176 | +``` | |
1177 | + | |
1047 | 1178 | ## Deployment |
1048 | 1179 | |
1049 | 1180 | Searchkick uses `ENV["ELASTICSEARCH_URL"]` for the Elasticsearch server. This defaults to `http://localhost:9200`. |
... | ... | @@ -1512,6 +1643,10 @@ Boost specific models with: |
1512 | 1643 | indices_boost: {Category => 2, Product => 1} |
1513 | 1644 | ``` |
1514 | 1645 | |
1646 | +## Multi-Tenancy | |
1647 | + | |
1648 | +Check out [this great post](https://www.tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/) on the [Apartment](https://github.com/influitive/apartment) gem. Follow a similar pattern if you use another gem. | |
1649 | + | |
1515 | 1650 | ## Scroll API |
1516 | 1651 | |
1517 | 1652 | Searchkick also supports the [scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html). Scrolling is not intended for real time user requests, but rather for processing large amounts of data. |
... | ... | @@ -1826,141 +1961,6 @@ Product.search "api", misspellings: {prefix_length: 2} # api, apt, no ahi |
1826 | 1961 | Product.search "ah", misspellings: {prefix_length: 2} # ah, no aha |
1827 | 1962 | ``` |
1828 | 1963 | |
1829 | -## Testing | |
1830 | - | |
1831 | -For performance, only enable Searchkick callbacks for the tests that need it. | |
1832 | - | |
1833 | -### Parallel Tests | |
1834 | - | |
1835 | -Rails 6 enables parallel tests by default. Add to your `test/test_helper.rb`: | |
1836 | - | |
1837 | -```ruby | |
1838 | -class ActiveSupport::TestCase | |
1839 | - parallelize_setup do |worker| | |
1840 | - Searchkick.index_suffix = worker | |
1841 | - | |
1842 | - # reindex models | |
1843 | - Product.reindex | |
1844 | - | |
1845 | - # and disable callbacks | |
1846 | - Searchkick.disable_callbacks | |
1847 | - end | |
1848 | -end | |
1849 | -``` | |
1850 | - | |
1851 | -And use: | |
1852 | - | |
1853 | -```ruby | |
1854 | -class ProductTest < ActiveSupport::TestCase | |
1855 | - def setup | |
1856 | - Searchkick.enable_callbacks | |
1857 | - end | |
1858 | - | |
1859 | - def teardown | |
1860 | - Searchkick.disable_callbacks | |
1861 | - end | |
1862 | - | |
1863 | - def test_search | |
1864 | - Product.create!(name: "Apple") | |
1865 | - Product.search_index.refresh | |
1866 | - assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1867 | - end | |
1868 | -end | |
1869 | -``` | |
1870 | - | |
1871 | -### Minitest | |
1872 | - | |
1873 | -Add to your `test/test_helper.rb`: | |
1874 | - | |
1875 | -```ruby | |
1876 | -# reindex models | |
1877 | -Product.reindex | |
1878 | - | |
1879 | -# and disable callbacks | |
1880 | -Searchkick.disable_callbacks | |
1881 | -``` | |
1882 | - | |
1883 | -And use: | |
1884 | - | |
1885 | -```ruby | |
1886 | -class ProductTest < Minitest::Test | |
1887 | - def setup | |
1888 | - Searchkick.enable_callbacks | |
1889 | - end | |
1890 | - | |
1891 | - def teardown | |
1892 | - Searchkick.disable_callbacks | |
1893 | - end | |
1894 | - | |
1895 | - def test_search | |
1896 | - Product.create!(name: "Apple") | |
1897 | - Product.search_index.refresh | |
1898 | - assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1899 | - end | |
1900 | -end | |
1901 | -``` | |
1902 | - | |
1903 | -### RSpec | |
1904 | - | |
1905 | -Add to your `spec/spec_helper.rb`: | |
1906 | - | |
1907 | -```ruby | |
1908 | -RSpec.configure do |config| | |
1909 | - config.before(:suite) do | |
1910 | - # reindex models | |
1911 | - Product.reindex | |
1912 | - | |
1913 | - # and disable callbacks | |
1914 | - Searchkick.disable_callbacks | |
1915 | - end | |
1916 | - | |
1917 | - config.around(:each, search: true) do |example| | |
1918 | - Searchkick.callbacks(nil) do | |
1919 | - example.run | |
1920 | - end | |
1921 | - end | |
1922 | -end | |
1923 | -``` | |
1924 | - | |
1925 | -And use: | |
1926 | - | |
1927 | -```ruby | |
1928 | -describe Product, search: true do | |
1929 | - it "searches" do | |
1930 | - Product.create!(name: "Apple") | |
1931 | - Product.search_index.refresh | |
1932 | - assert_equal ["Apple"], Product.search("apple").map(&:name) | |
1933 | - end | |
1934 | -end | |
1935 | -``` | |
1936 | - | |
1937 | -### Factory Bot | |
1938 | - | |
1939 | -Use a trait and an after `create` hook for each indexed model: | |
1940 | - | |
1941 | -```ruby | |
1942 | -FactoryBot.define do | |
1943 | - factory :product do | |
1944 | - # ... | |
1945 | - | |
1946 | - # Note: This should be the last trait in the list so `reindex` is called | |
1947 | - # after all the other callbacks complete. | |
1948 | - trait :reindex do | |
1949 | - after(:create) do |product, _evaluator| | |
1950 | - product.reindex(refresh: true) | |
1951 | - end | |
1952 | - end | |
1953 | - end | |
1954 | -end | |
1955 | - | |
1956 | -# use it | |
1957 | -FactoryBot.create(:product, :some_trait, :reindex, some_attribute: "foo") | |
1958 | -``` | |
1959 | - | |
1960 | -## Multi-Tenancy | |
1961 | - | |
1962 | -Check out [this great post](https://www.tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/) on the [Apartment](https://github.com/influitive/apartment) gem. Follow a similar pattern if you use another gem. | |
1963 | - | |
1964 | 1964 | ## Upgrading |
1965 | 1965 | |
1966 | 1966 | See [how to upgrade to Searchkick 3](docs/Searchkick-3-Upgrade.md) | ... | ... |