Class: EsQueryBuilder
- Inherits:
-
Object
- Object
- EsQueryBuilder
- Defined in:
- lib/es-query-builder.rb,
lib/es-query-builder/token.rb,
lib/es-query-builder/parser.rb,
lib/es-query-builder/version.rb,
lib/es-query-builder/tokenizer.rb
Overview
Public: 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' } }
Defined Under Namespace
Classes: Parser, Token, Tokenizer
Constant Summary collapse
- VERSION =
'0.0.4'
Instance Method Summary collapse
-
#build(query_string) ⇒ Object
Public: Convert the given query string into a query object.
-
#initialize(query_fields: [], filter_fields: [], all_query_fields: '_all', hierarchy_fields: [], nested_fields: {}, child_fields: {}) ⇒ EsQueryBuilder
constructor
Public: Construct the query builder object.
Constructor Details
#initialize(query_fields: [], filter_fields: [], all_query_fields: '_all', hierarchy_fields: [], nested_fields: {}, child_fields: {}) ⇒ EsQueryBuilder
Public: 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 nothing.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/es-query-builder.rb', line 49 def initialize(query_fields: [], filter_fields: [], all_query_fields: '_all', hierarchy_fields: [], nested_fields: {}, child_fields: {}) @query_fields = query_fields @filter_fields = filter_fields @all_query_fields = all_query_fields @hierarchy_fields = hierarchy_fields @nested_fields = nested_fields @child_fields = child_fields end |
Instance Method Details
#build(query_string) ⇒ Object
Public: 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 a Hash for Elasticsearch client or nil.
77 78 79 |
# File 'lib/es-query-builder.rb', line 77 def build(query_string) parser.parse(tokenizer.tokenize(query_string)) end |