Class: Mebla::Search
- Inherits:
-
Object
- Object
- Mebla::Search
- Includes:
- Enumerable
- Defined in:
- lib/mebla/search.rb
Overview
Handles all searching functions and chains search to define filters, sorting or facets.
This example searches posts by tags, sorts and filters the results |
criteria = Post.search.terms(:tags, ['ruby', 'rails']).ascending(:publish_date).only(:author => ['cousine']) This will search the index for posts tagged 'ruby' or 'rails', arrange the results ascendingly according to their publish dates, and filter the results by the author named 'cousine'.
Mebla supports multiple methods of searching
|
Instance Attribute Summary (collapse)
-
- (Object) results
readonly
Returns the value of attribute results.
-
- (Object) slingshot_search
readonly
Returns the value of attribute slingshot_search.
Instance Method Summary (collapse)
-
- (Mebla::Search) ascending(field)
(also: #asc)
Sorts results ascendingly.
-
- (Mebla::Search) descending(field)
(also: #desc)
Sorts results descendingly.
-
- (Object) each(&block)
Iterates over the results collection.
-
- (Array) entries
Returns the internal results list.
-
- (Mebla::Search) facet(name, field, options = {})
Creates a new facet for the search Defining a global facet named "tags"::.
-
- (Hash) facets
Retrieves the facets Reading a facet named 'tags'::.
-
- (Mebla::ResultSet) hits
Performs the search and returns the results.
-
- (Search) initialize(query_string = "", type_names = [])
constructor
Creates a new Search object.
-
- (Mebla::Search) only(*fields)
Filters the results according to the criteria Get all indexed Posts and filter them by tags and authors::.
-
- (Object) query(query_string, options = {})
Creates a Lucene query string search criteria Match Posts with "Test Lucene query" as title::.
-
- (Mebla::Search) term(field, value)
Creates a term search criteria.
-
- (Mebla::Search) terms(field, values, options = {})
Creates a terms search criteria Match Posts tagged with either 'ruby' or 'rails'::.
-
- (Float) time
Retrieves the time taken to perform the search in ms.
-
- (Integer) total
Retrieves the total number of hits.
Constructor Details
- (Search) initialize(query_string = "", type_names = [])
Creates a new Search object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mebla/search.rb', line 42 def initialize(query_string = "", type_names = []) # Convert type names from string or symbol to array type_names = case true when type_names.is_a?(Symbol), type_names.is_a?(String) [type_names] when type_names.is_a?(Array) && !type_names.empty? type_names.collect{|name| name.to_s} else [] end @slingshot_search = Slingshot::Search::Search.new(Mebla.context.slingshot_index_name, {}) # Add a type filter to return only certain types unless type_names.empty? only(:_type => type_names) end unless query_string.blank? query(query_string) end end |
Instance Attribute Details
- (Object) results (readonly)
Returns the value of attribute results
37 38 39 |
# File 'lib/mebla/search.rb', line 37 def results @results end |
- (Object) slingshot_search (readonly)
Returns the value of attribute slingshot_search
37 38 39 |
# File 'lib/mebla/search.rb', line 37 def slingshot_search @slingshot_search end |
Instance Method Details
- (Mebla::Search) ascending(field) Also known as: asc
Sorts results ascendingly
115 116 117 118 119 |
# File 'lib/mebla/search.rb', line 115 def ascending(field) @slingshot_search = @slingshot_search.sort @slingshot_search.instance_variable_get(:@sort).send(field.to_sym, 'asc') self end |
- (Mebla::Search) descending(field) Also known as: desc
Sorts results descendingly
124 125 126 127 128 |
# File 'lib/mebla/search.rb', line 124 def descending(field) @slingshot_search = @slingshot_search.sort @slingshot_search.instance_variable_get(:@sort).send(field.to_sym, 'desc') self end |
- (Object) each(&block)
Iterates over the results collection
217 218 219 |
# File 'lib/mebla/search.rb', line 217 def each(&block) hits.each(&block) end |
- (Array) entries
Returns the internal results list
187 188 189 |
# File 'lib/mebla/search.rb', line 187 def entries hits.entries end |
- (Mebla::Search) facet(name, field, options = {})
check elasticsearch's facet reference for more information
Creates a new facet for the search
Defining a global facet named "tags" |
Post.search("*").facet("tags", :tag, :global => true) |
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/mebla/search.rb', line 141 def facet(name, field, ={}) # Get the hash facet_hash = @slingshot_search.instance_variable_get(:@facets) # Create a new Facet facet_obj = Slingshot::Search::Facet.new(name, ) facet_obj.terms(field) # Initialize the hash if its nil if facet_hash.nil? @slingshot_search.instance_variable_set(:@facets, {}) end # Add the facet to the hash @slingshot_search.instance_variable_get(:@facets).update facet_obj.to_hash self end |
- (Hash) facets
Retrieves the facets
Reading a facet named 'tags' |
facets = Post.search("*").facet("tags", :tag) facets.each do |term|
end |
212 213 214 |
# File 'lib/mebla/search.rb', line 212 def facets hits.facets end |
- (Mebla::ResultSet) hits
Performs the search and returns the results
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mebla/search.rb', line 173 def hits return @results if @results # Log search query Mebla.log("Searching:\n#{@slingshot_search.to_json.to_s}", :debug) response = @slingshot_search.perform.response Mebla.log("Response:\n#{response}", :info) @results = Mebla::ResultSet.new(response) # Log results statistics Mebla.log("Searched for:\n#{@slingshot_search.to_json.to_s}\ngot #{@results.total} documents in #{@results.time} ms", :debug) @results end |
- (Mebla::Search) only(*fields)
Filters the results according to the criteria
Get all indexed Posts and filter them by tags and authors |
Post.search("*").only(:tag => ["ruby", "rails"], :author => ["cousine"]) |
163 164 165 166 167 168 169 |
# File 'lib/mebla/search.rb', line 163 def only(*fields) return if fields.empty? fields.each do |field| @slingshot_search = @slingshot_search.filter(:terms, field) end self end |
- (Object) query(query_string, options = {})
For more information check Lucene's query syntax
Creates a Lucene query string search criteria
Match Posts with "Test Lucene query" as title |
Post.search.query("Test Lucene query", :default_field => "title") |
You can also instead |
Post.search.query("title: Test Lucene query") |
Or to search all fields |
Post.search.query("Test Lucene query") |
106 107 108 109 110 |
# File 'lib/mebla/search.rb', line 106 def query(query_string, = {}) @slingshot_search = @slingshot_search.query @slingshot_search.instance_variable_get(:@query).string(query_string, ) self end |
- (Mebla::Search) term(field, value)
Creates a term search criteria
83 84 85 86 87 |
# File 'lib/mebla/search.rb', line 83 def term(field, value) @slingshot_search = @slingshot_search.query @slingshot_search.instance_variable_get(:@query).term(field, value) self end |
- (Mebla::Search) terms(field, values, options = {})
Creates a terms search criteria
Match Posts tagged with either 'ruby' or 'rails' |
Post.search.terms(:tags, ['ruby', 'rails'], :minimum_match => 1) |
73 74 75 76 77 |
# File 'lib/mebla/search.rb', line 73 def terms(field, values, = {}) @slingshot_search = @slingshot_search.query @slingshot_search.instance_variable_get(:@query).terms(field, values, ) self end |
- (Float) time
Retrieves the time taken to perform the search in ms
199 200 201 |
# File 'lib/mebla/search.rb', line 199 def time hits.time end |
- (Integer) total
Retrieves the total number of hits
193 194 195 |
# File 'lib/mebla/search.rb', line 193 def total hits.total end |