elasticsearch to be turned off as a default in dev and test

This commit is contained in:
Shiho Takagi
2015-01-21 16:44:59 +11:00
parent f541261e43
commit 018b2b4711
9 changed files with 70 additions and 28 deletions

View File

@@ -40,7 +40,7 @@ class Crop < ActiveRecord::Base
tokenizer: {
gs_edgeNGram_tokenizer: {
type: "edgeNGram",
min_gram: 4,
min_gram: 3,
max_gram: 10,
token_chars: [ "letter", "digit" ]
}
@@ -243,17 +243,21 @@ class Crop < ActiveRecord::Base
# Crop.search(string)
def self.search(query)
search_str = query.nil? ? "" : query.downcase
response = __elasticsearch__.search( {
query: {
multi_match: {
query: "#{search_str}",
fields: ["name", "scientific_names.scientific_name", "alternate_names.name"]
}
},
size: 50
}
)
return response.records.to_a
if ENV['GROWSTUFF_ELASTICSEARCH'] == "true"
search_str = query.nil? ? "" : query.downcase
response = __elasticsearch__.search( {
query: {
multi_match: {
query: "#{search_str}",
fields: ["name", "scientific_names.scientific_name", "alternate_names.name"]
}
},
size: 50
}
)
return response.records.to_a
else
where("name ILIKE ?", "%#{query}%")
end
end
end

View File

@@ -62,6 +62,13 @@ GROWSTUFF_PAYPAL_USERNAME: "dummy"
GROWSTUFF_PAYPAL_PASSWORD: "dummy"
GROWSTUFF_PAYPAL_SIGNATURE: "dummy"
# Elasticsearch is used for flexible search and it requires another component
# to be installed. To make it easy for people who don't need to test this feature
# it's been turned off for test and development environment as a default.
# If you want to test this functionality, install elasticsearch and
# set this flag to "true".
GROWSTUFF_ELASTICSEARCH: "false"
##############################################################################
# Other environments
# You can override the above for staging, production, etc.
@@ -78,6 +85,8 @@ test:
staging:
GROWSTUFF_SITE_NAME: Growstuff (staging)
GROWSTUFF_ELASTICSEARCH: "true"
production:
GROWSTUFF_SITE_NAME: Growstuff
GROWSTUFF_ELASTICSEARCH: "true"

View File

@@ -7,12 +7,13 @@ feature "Harvesting a crop", :js => true do
background do
login_as(member)
visit new_harvest_path
sync_elasticsearch([maize])
end
it_behaves_like "crop suggest", "harvest", "crop"
scenario "Creating a new harvest", :js => true do
fill_autocomplete "crop", :with => "m"
fill_autocomplete "crop", :with => "mai"
select_from_autocomplete "maize"
within "form#new_harvest" do
fill_in "When?", :with => "2014-06-15"

View File

@@ -9,12 +9,13 @@ feature "Planting a crop", :js => true do
background do
login_as(member)
visit new_planting_path
sync_elasticsearch([maize])
end
it_behaves_like "crop suggest", "planting"
scenario "Creating a new planting" do
fill_autocomplete "crop", :with => "m"
fill_autocomplete "crop", :with => "mai"
select_from_autocomplete "maize"
within "form#new_planting" do
fill_in "When", :with => "2014-06-15"
@@ -41,7 +42,7 @@ feature "Planting a crop", :js => true do
end
scenario "Marking a planting as finished" do
fill_autocomplete "crop", :with => "m"
fill_autocomplete "crop", :with => "mai"
select_from_autocomplete "maize"
within "form#new_planting" do
fill_in "When?", :with => "2014-07-01"
@@ -77,7 +78,7 @@ feature "Planting a crop", :js => true do
end
scenario "Marking a planting as finished without a date" do
fill_autocomplete "crop", :with => "m"
fill_autocomplete "crop", :with => "mai"
select_from_autocomplete "maize"
within "form#new_planting" do
check "Mark as finished"

View File

@@ -7,12 +7,13 @@ feature "Harvesting a crop", :js => true do
background do
login_as(member)
visit new_seed_path
sync_elasticsearch([maize])
end
it_behaves_like "crop suggest", "seed", "crop"
scenario "Adding a new seed", :js => true do
fill_autocomplete "crop", :with => "m"
fill_autocomplete "crop", :with => "mai"
select_from_autocomplete "maize"
within "form#new_seed" do
fill_in "Quantity:", :with => 42

View File

@@ -1,29 +1,39 @@
require 'rails_helper'
shared_examples "crop suggest" do |resource|
let!(:popcorn) { FactoryGirl.create(:popcorn) }
let!(:pea) { FactoryGirl.create(:crop, :name => 'pea') }
let!(:pear) { FactoryGirl.create(:pear) }
let!(:tomato) { FactoryGirl.create(:tomato) }
let!(:roma) { FactoryGirl.create(:roma) }
background do
sync_elasticsearch([pea, pear, maize, tomato])
end
scenario "See text in crop auto suggest field" do
expect(page).to have_selector("input[placeholder='e.g. lettuce']")
end
scenario "Typing in the crop name displays suggestions" do
within "form#new_#{resource}" do
fill_autocomplete "crop", :with => "p"
fill_autocomplete "crop", :with => "pe"
end
expect(page).to_not have_content("pear")
expect(page).to_not have_content("pea")
within "form#new_#{resource}" do
fill_autocomplete "crop", :with => "pea"
end
expect(page).to have_content("pear")
expect(page).to have_content("popcorn")
expect(page).to have_content("pea")
within "form#new_#{resource}" do
fill_autocomplete "crop", :with => "pear"
end
expect(page).to have_content("pear")
expect(page).to_not have_content("popcorn")
select_from_autocomplete("pear")
@@ -32,16 +42,16 @@ shared_examples "crop suggest" do |resource|
scenario "Typing and pausing does not affect input" do
within "form#new_#{resource}" do
fill_autocomplete "crop", :with => "p"
fill_autocomplete "crop", :with => "pea"
end
expect(page).to have_content("pear")
expect(find_field("crop").value).to eq("p")
expect(find_field("crop").value).to eq("pea")
end
scenario "Searching for a crop casts a wide net on results" do
within "form#new_#{resource}" do
fill_autocomplete "crop", :with => "to"
fill_autocomplete "crop", :with => "tom"
end
expect(page).to have_content("tomato")

View File

@@ -328,8 +328,7 @@ describe Crop do
context "search" do
before :each do
@mushroom = FactoryGirl.create(:crop, :name => 'mushroom')
@mushroom.__elasticsearch__.index_document
Crop.__elasticsearch__.refresh_index!
sync_elasticsearch([@mushroom])
end
it "finds exact matches" do
Crop.search('mushroom').should eq [@mushroom]

View File

@@ -14,7 +14,6 @@ RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.start
Crop.__elasticsearch__.create_index! force: true
end
config.after(:each) do

View File

@@ -0,0 +1,18 @@
module ElasticsearchHelpers
def sync_elasticsearch(crops)
if ENV['GROWSTUFF_ELASTICSEARCH'] == "true"
crops.each {|crop| crop.__elasticsearch__.index_document}
Crop.__elasticsearch__.refresh_index!
end
end
end
RSpec.configure do |config|
config.include ElasticsearchHelpers
config.before(:each) do
if ENV['GROWSTUFF_ELASTICSEARCH'] == "true"
Crop.__elasticsearch__.create_index! force: true
end
end
end