module Elastic::SiteSearch::Client::Search
Methods wrapping the Elastic::SiteSearch
private search and API endpoints. Using these methods, you can perform full-text and prefix searches over the Documents in your Engine
, in a specific DocumentType
, or any subset of DocumentTypes. You can also filter results and get faceted counts for results.
For more information, visit the REST API documentation on searching.
Public Instance Methods
Perform a full-text search over all the DocumentTypes of the provided engine.
results = client.search("site-search-api-example", "glass") results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
@param [String] engine_id the Engine
slug or ID @param [String] query the search terms (may be nil) @param [Hash] options search options (see the REST API docs for a complete list) @option options [Integer] :page page number of results to fetch (server defaults to 1) @option options [Integer] :per_page number of results per page (server defaults to 20) @option options [Array] :document_types an array of DocumentType
slugs to search.
The server defaults to searching all DocumentTypes in the engine. To search a single document type, the +search_document_type+ method is more convenient.
@option options [Hash] :fetch_fields a Hash of DocumentType
slug to array of the fields to return with results
(example: <code>{'videos' => ['title', 'channel_id']}</code>)
@option options [Hash] :search_fields a Hash of DocumentType
slug to array of the fields to search.
May contain {field weight boosts}[https://swiftype.com/documentation/site-search/searching/field-weights] (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>). The server defaults to searching all +string+ and +text+ fields for search queries.
@option options [Hash] :filters a Hash of DocumentType
slug to filter definition Hash.
See {filters in the REST API documentation}[https://swiftype.com/documentation/site-search/searching/filtering] for more details (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
@option options [Hash] :functional_boosts a Hash of DocumentType
slug to functional boost definition
(example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
@option options [Hash] :facets a Hash of DocumentType
slug to an Array of field names to provide facetted counts for
(example: <code>{'videos' => ['category_id', 'channel_id']}</code>)
@option options [Hash] :sort_field a Hash of DocumentType
slug to field name to sort on
(example: <code>{'videos' => 'view_count'}</code>)
@option options [Hash] :sort_direction a Hash of DocumentType
slug to direction to sort
(example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
@return [Elastic::SiteSearch::ResultSet]
# File lib/elastic/site-search/client.rb, line 128 def search(engine_id, query, options={}) search_params = { :q => query }.merge(options) response = post("engines/#{engine_id}/search.json", search_params) ResultSet.new(response) end
Perform a full-text search over a single DocumentType
in an Engine
.
results = client.search_document_type("site-search-api-example", "videos", "glass") results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
@param [String] engine_id the Engine
slug or ID @param [String] document_type_id the DocumentType
slug or ID @param [String] query the search terms (may be nil) @param [Hash] options search options (see the REST API docs for a complete list) @option options [Integer] :page page number of results to fetch (server defaults to 1) @option options [Integer] :per_page number of results per page (server defaults to 20) @option options [Hash] :fetch_fields a Hash of DocumentType
slug to array of the fields to return with results
(example: <code>{'videos' => ['title', 'channel_id']}</code>)
@option options [Hash] :search_fields a Hash of DocumentType
slug to array of the fields to search.
May contain {field weight boosts}[https://swiftype.com/documentation/site-search/searching/field-weights] (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>). The server defaults to searching all +string+ and +text+ fields for search queries.
@option options [Hash] :filters a Hash of DocumentType
slug to filter definition Hash.
See {filters in the REST API documentation}[https://swiftype.com/documentation/site-search/searching/filtering] for more details (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
@option options [Hash] :functional_boosts a Hash of DocumentType
slug to functional boost definition
(example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
@option options [Hash] :facets a Hash of DocumentType
slug to an Array of field names to provide facetted counts for
(example: <code>{'videos' => ['category_id', 'channel_id']}</code>)
@option options [Hash] :sort_field a Hash of DocumentType
slug to field name to sort on
(example: <code>{'videos' => 'view_count'}</code>)
@option options [Hash] :sort_direction a Hash of DocumentType
slug to direction to sort
(example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
@return [Elastic::SiteSearch::ResultSet]
# File lib/elastic/site-search/client.rb, line 203 def search_document_type(engine_id, document_type_id, query, options={}) search_params = { :q => query }.merge(options) response = post("engines/#{engine_id}/document_types/#{document_type_id}/search.json", search_params) ResultSet.new(response) end
Perform an autocomplete (prefix) search over all the DocumentTypes of the provided engine. This can be used to implement type-ahead autocompletion. However, if your data is not sensitive, you should consider using the Site Search public JSONP API in the user's web browser for suggest queries.
results = client.suggest("site-search-api-example", "gla") results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
@param [String] engine_id the Engine
slug or ID @param [String] query the search terms @param [Hash] options search options (see the REST API docs for a complete list) @option options [Integer] :page page number of results to fetch (server defaults to 1) @option options [Integer] :per_page number of results per page (server defaults to 20) @option options [Array] :document_types an array of DocumentType
slugs to search.
The server defaults to searching all DocumentTypes in the engine. To search a single document type, the +suggest_document_type+ method is more convenient.
@option options [Hash] :fetch_fields a Hash of DocumentType
slug to array of the fields to return with results
(example: <code>{'videos' => ['title', 'channel_id']}</code>)
@option options [Hash] :search_fields a Hash of DocumentType
slug to array of the fields to search.
May contain {field weight boosts}[https://swiftype.com/documentation/site-search/searching/field-weights] (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>). The server defaults to searching all +string+ fields for suggest queries.
@option options [Hash] :filters a Hash of DocumentType
slug to filter definition Hash.
See {filters in the REST API documentation}[https://swiftype.com/documentation/site-search/searching/filtering] for more details (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
@option options [Hash] :functional_boosts a Hash of DocumentType
slug to functional boost definition
(example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
@option options [Hash] :sort_field a Hash of DocumentType
slug to field name to sort on
(example: <code>{'videos' => 'view_count'}</code>)
@option options [Hash] :sort_direction a Hash of DocumentType
slug to direction to sort
(example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
@return [Elastic::SiteSearch::ResultSet]
# File lib/elastic/site-search/client.rb, line 90 def suggest(engine_id, query, options={}) search_params = { :q => query }.merge(options) response = post("engines/#{engine_id}/suggest.json", search_params) ResultSet.new(response) end
Perform an autocomplete (prefix) search over a single DocumentType
in an Engine
. This can be used to implement type-ahead autocompletion. However, if your data is not sensitive, you should consider using the Site Search public JSONP API in the user's web browser for suggest queries.
results = client.suggest_document_type("site-search-api-example", "videos", "gla") results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
@param [String] engine_id the Engine
slug or ID @param [String] query the search terms @param [Hash] options search options (see the REST API docs for a complete list) @option options [Integer] :page page number of results to fetch (server defaults to 1) @option options [Integer] :per_page number of results per page (server defaults to 20) @option options [Array] :document_types an array of DocumentType
slugs to search.
The server defaults to searching all DocumentTypes in the engine. To search a single document type, the +suggest_document_type+ method is more convenient.
@option options [Hash] :fetch_fields a Hash of DocumentType
slug to array of the fields to return with results
(example: <code>{'videos' => ['title', 'channel_id']}</code>)
@option options [Hash] :search_fields a Hash of DocumentType
slug to array of the fields to search.
May contain {field weight boosts}[https://swiftype.com/documentation/site-search/searching/field-weights] (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>). The server defaults to searching all +string+ fields for suggest queries.
@option options [Hash] :filters a Hash of DocumentType
slug to filter definition Hash.
See {filters in the REST API documentation}[https://swiftype.com/documentation/site-search/searching/filtering] for more details (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
@option options [Hash] :functional_boosts a Hash of DocumentType
slug to functional boost definition
(example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
@option options [Hash] :sort_field a Hash of DocumentType
slug to field name to sort on
(example: <code>{'videos' => 'view_count'}</code>)
@option options [Hash] :sort_direction a Hash of DocumentType
slug to direction to sort
(example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
@return [Elastic::SiteSearch::ResultSet]
# File lib/elastic/site-search/client.rb, line 167 def suggest_document_type(engine_id, document_type_id, query, options={}) search_params = { :q => query }.merge(options) response = post("engines/#{engine_id}/document_types/#{document_type_id}/suggest.json", search_params) ResultSet.new(response) end