Scopes
The Flex::Scopes
module is included in your Flex::ActiveModel
models, so you can just use them without the include Flex::ActiveModel
statement. On the other hand, you can include the Flex::Scopes
with any class that includes any Flex module, and with a simple trick, also with your ActiveRecord
models (that defines their own scope
method) (see Using flex-scopes with ActiveRecord Models) .
Filter Scopes
The flex-scope
gems implements only a few of the most commonly used elasticsearch filters, plus a generic filters
scope that allows to add any other filter not explicitly defined as a scope. All the Filter Scopes return a Flex::Scope
object.
missing
|
elasticsearch missing-filter API Accepts a single key hash or a multiple keys hash (that will be translated in a sequence of
|
term
|
Accepts a single key hash or a multiple keys hash (that will be translated in a sequence of
|
terms
|
An extended version of the elasticsearch terms-filter API In its basic usage it conforms to the API, but it allows also to represent the
|
range
|
Implements the elasticsearch range-filter
|
filters
|
This scope is a generic scope that allows to add any elasticsearch filter structure not explicitly available as a scope. Accepts a single, or an array, or a list of filter structures. For example:
|
and and or wrappers
|
You can wrap groups of filters in
|
Variable Scopes
These scopes are a direct interface for the variables used to query. All the Variable Scopes return a Flex::Scope
object.
query_string
|
This scope accepts a query string or an hash to parse as documented in elasticsearch query-string-query API. If omitted it uses
|
query
|
Alias for |
sort
|
This scope accepts one or an array or a list of sort structures documented as elasticsearch sort API.
|
fields
|
The fields that you want to retrieve in order to limit the size of the response
|
script_fields
|
Implements the elasticsearch script-fields API. Adds one or more script fields to the scope.
|
size
|
Limits the size of the retrieved hits
|
page
|
The page of the pagination. By setting this scope you get automatically set the
|
params
|
The params are a hash of key/value pairs. Flex will transform them in the query string part of the path (all what comes after the
|
index
|
The
|
type
|
The
|
variables
|
A generic scope usable to add variables
|
facets
|
Implements a generic elasticsearch facets API usable with all the facets (just pass the facet hash structure).
|
highlights
|
Implements the elasticsearch highlighting API.
|
metrics
|
Adds the elasticsearch search_type API of type
|
Query Scopes
All the Query Scopes methods actually perform the query generated by the scope and return some kind of result but not a scope object, so they are used as the last scope in the chain. They all accept a list of variables (which will be deep-merged with the variables contained in the scope itself).
If they are called on your class directly, they first generate an empty scope without restrictions, internally using the scoped
method (see scoped). Depending on the method, the result may be a single document, a collection of documents, or the actual instance(s) of your class when used with Flex::ActiveModel
models. Others allow you to count or iterate over some retrieved collection.
find
|
Retrieves a single or multiple documents using their ids
|
first
|
Retrieves the first document of the scope
|
last
|
Retrieves the last document of the scope
|
all
|
Retrieves all the documents of the scope
|
delete
|
Deletes the documents of the scope
|
each
|
Iterates over all the documents in the scope
|
scan_all
|
Iterates in batches, over all the documents in the scope by using the elasticsearch search_type API of type
|
each_batch , find_in_batches
|
Aliases for |
count
|
Counts the documents in the scope
|
Methods
scoped
|
This method creates a new |
scope
|
This method generates a named scope in your class. It requires a symbol name as the first argument, and a scope, or a block (or
|
scope_methods
|
The array of all the named scopes you defined. Mostly used internally. |
method_missing
|
Used to chain the scopes with other scopes, or eventually with templates defined by
|
Using flex-scopes with ActiveRecord Models
You must not include Flex::Scopes
right in your ActiveRecord
models: instead you should namespace your scopes and set the context
to your model. For example:
class MyModel < ActiveRecord::Base
include Flex::ModelIndexer
module Flex
include Flex::Scopes
flex.context = MyModel
scope :red, terms(:color => 'red')
...
end
end
total_red = MyModel::Flex.red.count
first_red = MyModel::Flex.red.first
Notice: When you namespace the scopes, you must set the
flex.context
with your model class.