class EsQueryBuilder
The class has a responsibility for converting a query string into a corresponding query hash object for Elasticsearch.
Examples¶ ↑
builder = EsQueryBuilder.new( query_fields: ['query'], filter_fields: ['filter'] ) # => #<EsQueryBuilder> builder.build('term') # => { match: { '_all' => 'term' } } builder.build('query:term') # => { match: { 'query' => 'hello' } } builder.build('filter:term') # => { # filtered: { # query: { match_all: {} }, # filter: { term: { filter: 'hello' } } # } # } builder.build('query\:term') # => { match: { '_all' => 'query\:term' } } builder.build('unknown:term') # => { match: { '_all' => 'term' } }
Constants
- VERSION
Public
↑ topPublic Class Methods
new(query_fields: [], filter_fields: [], all_query_fields: '_all', hierarchy_fields: [])
click to toggle source
Construct the query builder object.
- query_fields
-
An Array of Strings for specifing allowed quering types (default: []).
- filter_fields
-
An Array of Strings for specifing allowed filtering types (default: []).
- all_query_fields
-
A String or an Array of Strings for searching usual query terms (default: '_all').
- hierarchy_fields
-
An Array of Strings which treats the trailing slash character as a hierarchy (default: []).
Returns¶ ↑
Returns nothing.
# File lib/es-query-builder.rb, line 49 def initialize(query_fields: [], filter_fields: [], all_query_fields: '_all', hierarchy_fields: []) @query_fields = query_fields @filter_fields = filter_fields @all_query_fields = all_query_fields @hierarchy_fields = hierarchy_fields end
Public Instance Methods
build(query_string)
click to toggle source
Convert the given query string into a query object.
- query_string
-
A query String for searching.
Examples¶ ↑
build('hello world') # => { # bool: { # must: [ # { match: { '_all' => 'hello' } }, # { match: { '_all' => 'world' } } # ] # } # }
Returns¶ ↑
Returns a Hash for Elasticsearch client or nil.
# File lib/es-query-builder.rb, line 74 def build(query_string) parser.parse(tokenizer.tokenize(query_string)) end
Internal
↑ topPrivate Instance Methods
parser()
click to toggle source