Module: ElasticsearchQueryParser

Defined in:
lib/elasticsearch_query_parser.rb,
lib/elasticsearch_query_parser/sentence.rb,
lib/elasticsearch_query_parser/grammar/parser.rb,
lib/elasticsearch_query_parser/grammar/transformer.rb,
lib/elasticsearch_query_parser/grammar/presenters/term.rb,
lib/elasticsearch_query_parser/grammar/presenters/query.rb,
lib/elasticsearch_query_parser/grammar/presenters/operator.rb

Overview

Mail ElasticsearchQueryParser interface

Defined Under Namespace

Modules: Grammar Classes: ParseFailedException, Sentence

Class Method Summary collapse

Class Method Details

.configurationObject

Gem configuration object For now only contain 1 attribute: ‘elastic_field_name` option (by default :text) Responds with OpenStruct entity which responds_to `elastic_field_name` Example:

>> ElasticsearchQueryParser.configuration
=> #<OpenStruct elastic_field_name=:text>


15
16
17
18
19
# File 'lib/elasticsearch_query_parser.rb', line 15

def self.configuration
  @configuration ||= OpenStruct.new(
    elastic_field_name: :text
  )
end

.configure {|configuration| ... } ⇒ Object

For gem configuration For now only ‘elastic_field_name` can be modified. See README.md for details

Yields:



23
24
25
# File 'lib/elasticsearch_query_parser.rb', line 23

def self.configure
  yield(configuration)
end

.parse_query(user_query) ⇒ Object

Parse given string into Elastic query object (see README.md for more examples) Example:

>> ElasticsearchQueryParser.parse_query("London")
=> { query: { bool: { should: [{ match: { text: { query: "London", operator: "and" } } }] } } }


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elasticsearch_query_parser.rb', line 31

def self.parse_query(user_query)
  query = Sentence.new(user_query).to_s

  return {} if query.to_s.empty?

  parse_tree = Grammar::Parser.new.parse(query)
  transformed_query = Grammar::Transformer.new.apply(parse_tree)
  { query: transformed_query.to_elasticsearch }
rescue Parslet::ParseFailed => e
  raise ParseFailedException, e.parse_failure_cause
end