Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

map semantic search fields

  • Loading branch information...
commit 217eb8af94f89fbb10d3a1bbbd099d380e04db80 1 parent 6d30688
@jrochkind authored
View
37 app/models/bento_search/search_engine.rb
@@ -99,22 +99,47 @@ def parse_search_arguments(*orig_arguments)
arguments[:start] = (arguments[:page] - 1) * arguments[:per_page]
arguments.delete(:page)
end
+
+ # translate semantic_search_field to search_field, or raise if
+ # can't.
+ if semantic = arguments.delete(:semantic_search_field)
+ mapped = self.class.semantic_search_map[semantic]
+ unless mapped
+ raise ArgumentError.new("#{self.class.name} does not know about :semantic_search_field #{semantic}")
+ end
+ arguments[:search_field] = mapped
+ end
return arguments
end
module ClassMethods
- def search_field_keys
+ # Returns list of string internal search_field's that can
+ # be supplied to search(:search_field => x)
+ def search_keys
return [] unless respond_to? :search_field_definitions
return search_field_definitions.keys
end
- def semantic_search_fields
- return [] unless respond_to? :search_field_definitions
- search_field_definitions.collect do |k, v|
- v[:semantic] if v
- end.compact
+ # Returns list of symbol semantic_search_field that can be
+ # supplied to search(:semantic_search_field => x)
+ def semantic_search_keys
+ semantic_search_map.keys
end
+
+ # returns a hash keyed by semantic search field symbol,
+ # value string internal search field key.
+ def semantic_search_map
+ return {} unless respond_to? :search_field_definitions
+
+ # Hash[] conveniently takes an array of k-v pairs.
+ return Hash[
+ search_field_definitions.collect do |field, defn|
+ [ defn[:semantic], field ] if defn && defn[:semantic]
+ end.compact
+ ]
+ end
+
end
end
View
23 test/unit/search_engine_base_test.rb
@@ -98,10 +98,29 @@ def test_enforce_max_per_page
end
def test_search_field_keys
- assert_equal ["my_title", "my_author", "my_other"], Dummy.search_field_keys
- assert_equal [:title, :author], Dummy.semantic_search_fields
+ assert_equal ["my_title", "my_author", "my_other"], Dummy.search_keys
+ assert_equal [:title, :author], Dummy.semantic_search_keys
end
+ def test_semantic_search_map
+ assert_equal( {:title => "my_title", :author => "my_author"},
+ Dummy.semantic_search_map)
+ end
+
+ def test_translate_search_field_semantics
+ d = Dummy.new
+
+ args = d.test_parse(:query => "query", :semantic_search_field => :title)
+
+ assert ! (args.has_key? :semantic_search_field), "translates semantic_search_field to search_field"
+ assert_equal "my_title", args[:search_field]
+
+ assert_raise(ArgumentError, "Raises for undefined semantic_search_field") do
+ d.test_parse(:query => "query", :semantic_search_field => :subject)
+ end
+ end
+
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.