Search
To create a search experience for your app, you can choose between frontend or backend search.
Frontend search
Use Algolia’s UI libraries, InstantSearch and Autocomplete, for client-side searching. Search requests are made from the browser or app, and results are returned without going through your server. This ensures rapid searching.
Backend search
Sometimes, backend search can be more suitable. The search request goes to your server, from your server to Algolia. Algolia sends the response back to your server, where you can process it.
Backend search response
With backend search, you’ll get ORM-compliant Rails model objects.
1
2
hits = Contact.search("jon doe")
p hits
The search
method accepts a query and search parameters:
1
2
# Query and search parameters
p Contact.search('jon doe', { hitsPerPage: 5, page: 2 })
If you always pass the same parameters, set them as index settings.
1
2
3
4
5
6
7
8
9
10
class Contact < ActiveRecord::Base
include AlgoliaSearch
algoliasearch do
# Default search parameters stored in the index settings
minWordSizefor1Typo 4
minWordSizefor2Typos 8
hitsPerPage 42
end
end
Every ORM object of the response contains the highlighted text for the search query.
This corresponds to the highlight_result
attribute in Algolia’s raw response.
1
hits[0].highlight_result['first_name']['value']
Raw search response
To access attributes from the Algolia search response, use the raw_answer
array:
1
2
3
hits = Contact.search("jon doe")
p hits.raw_answer['nbHits']
p hits.raw_answer['nbPages']
If you don’t need models, you can avoid loading objects from the database by retrieving Algolia’s raw JSON response:
1
2
3
4
json_answer = Contact.raw_search("jon doe")
p json_answer
p json_answer['hits']
p json_answer['facets']
Searching in specific indices
To search in specific indices, specify the index with the index
key:
1
2
Book.search 'new america', index: 'Book_by_editor'
Book.raw_search 'new america', index: 'Book_by_editor'
To search in a replica index, use the replica
key:
1
2
Book.search 'new america', replica: 'Book_by_editor'
Book.raw_search 'new america', replica: 'Book_by_editor'
Backend pagination
To add pagination server-side, use one of the following pagination backends:
To use :will_paginate
, add :pagination_backend
to your global configuration:
1
2
3
4
5
AlgoliaSearch.configuration = {
application_id: 'YourApplicationID',
api_key: 'YourWriteAPIKey',
pagination_backend: :will_paginate
}
In your controller:
1
@results = MyModel.search('america', hitsPerPage: 10)
In your views:
1
2
3
4
5
# If using will_paginate
<%= will_paginate @results %>
# If using kaminari
<%= paginate @results %>
Faceting
To retrieve facets, you must first configure them.
To retrieve them, use the facets
method.
1
2
3
4
5
hits = Contact.search('jon doe', { facets: '*' })
p hits # ORM-compliant array of objects
p hits.facets # Extra method added to retrieve facets
p hits.facets['company'] # Facet values+count of facet 'company'
p hits.facets['zip_code'] # Facet values+count of facet 'zip_code'
Or perform a raw search and access the JSON response attributes:
1
2
raw_json = Contact.raw_search('jon doe', { facets: '*' })
p raw_json['facets']
Search for facet values
If you have more facet values than what can fit in your UI, it can be helpful to let users search for them.
If you want to support searching for facet values, configure the facet as
searchable
.
1
Product.search_for_facet_values('category', 'Headphones') # Array of {value, highlighted, count}
This method accepts search parameters, such as filters.
1
2
3
4
5
# Only sends back the categories containing red Apple products (and only counts those)
Product.search_for_facet_values('category', 'phone', {
query: 'red',
filters: 'brand:Apple'
}) # Array of phone categories linked to red Apple products