exclude unapproved crops from search

This commit is contained in:
Shiho Takagi
2015-02-17 23:33:39 +11:00
parent ac6aa730c1
commit 2241a760c2
2 changed files with 15 additions and 1 deletions

View File

@@ -76,6 +76,7 @@ class Crop < ActiveRecord::Base
mappings dynamic: 'false' do
indexes :id, type: 'long'
indexes :name, type: 'string', analyzer: 'gs_edgeNGram_analyzer'
indexes :approval_status, type: 'string'
indexes :scientific_names do
indexes :scientific_name,
type: 'string',
@@ -92,7 +93,7 @@ class Crop < ActiveRecord::Base
def as_indexed_json(options={})
self.as_json(
only: [:id, :name],
only: [:id, :name, :approval_status],
include: {
scientific_names: { only: :scientific_name },
alternate_names: { only: :name }
@@ -311,6 +312,9 @@ class Crop < ActiveRecord::Base
fields: ["name", "scientific_names.scientific_name", "alternate_names.name"]
}
},
filter: {
term: {approval_status: "approved"}
},
size: 50
}
)

View File

@@ -342,6 +342,16 @@ describe Crop do
it "searches case insensitively" do
Crop.search('mUsH').should include @mushroom
end
it "doesn't find 'rejected' crop" do
@mushroom.approval_status = "rejected"
sync_elasticsearch([@mushroom])
Crop.search('mushroom').should_not include @mushroom
end
it "doesn't find 'pending' crop" do
@mushroom.approval_status = "pending"
sync_elasticsearch([@mushroom])
Crop.search('mushroom').should_not include @mushroom
end
end
context "csv loading" do