Class: Elasticquery::Filters::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/elasticquery/filters/search.rb

Constant Summary collapse

OPERATORS =
%w(and or)
TYPES =
%w(best_fields most_fields cross_fields phrase pharse_prefix)

Instance Method Summary collapse

Methods inherited from Base

#dup_with, #invalid?

Constructor Details

#initialize(query, fields: "_all", operator: "and", type: "best_fields") ⇒ Search

Create new search subquery



15
16
17
18
19
20
# File 'lib/elasticquery/filters/search.rb', line 15

def initialize(query, fields: "_all", operator: "and", type: "best_fields")
  @fields = fields
  @operator = operator
  @type = type
  @query = query
end

Instance Method Details

#to_hashHash

Hash presentation of query.

Examples:

r = Elasticquery::Filters::Search.new { fields: ['name', 'country'], query: 'belarus' }
r.to_hash #=> {query: {filtered: {query: {multi_match: {fields: ['name', 'country'], query: 'belarus'}}}}}

Returns:

  • (Hash)

    presentation of filter.



42
43
44
# File 'lib/elasticquery/filters/search.rb', line 42

def to_hash
  valid? ? {query: {filtered: {query: {multi_match: subquery}}}} : {}
end

#valid?Boolean

Is current query valid to exec

Examples:

filter = Elasticquery::Filters::Search.new 'hello'
filter.valid? #=> true

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/elasticquery/filters/search.rb', line 29

def valid?
  OPERATORS.include?(@operator) &&
    TYPES.include?(@type) &&
    ( Array === @fields || @fields == "_all" )
end