Class: BlacklightAdvancedSearch::QueryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/blacklight_advanced_search/advanced_query_parser.rb

Overview

Can extract query elements from rails #params query params, and then parse them and convert them into a solr query with #to_solr

#keyword_queries and #filters, which just return extracted elements of query params, may also be useful in display etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_state, config) ⇒ QueryParser

Returns a new instance of QueryParser.



12
13
14
15
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 12

def initialize(search_state, config)
  @search_state = search_state
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 10

def config
  @config
end

#search_stateObject (readonly)

Returns the value of attribute search_state.



10
11
12
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 10

def search_state
  @search_state
end

Instance Method Details

#keyword_opObject

Returns “AND” or “OR”, how #keyword_queries will be combined



26
27
28
29
30
31
32
33
34
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 26

def keyword_op
  op = search_state.params[:op]&.to_sym || :must

  if op == :should
    'OR'
  else
    'AND'
  end
end

#keyword_queriesObject



36
37
38
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 36

def keyword_queries
  search_state.clause_params.values.select { |clause| clause[:query].present? }
end

#local_param_hash(key, config) ⇒ Object



50
51
52
53
54
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 50

def local_param_hash(key, config)
  field_def = config.search_fields[key] || {}

  (field_def[:solr_adv_parameters] || field_def[:solr_parameters] || {}).merge(field_def[:solr_local_parameters] || {})
end

#process_query(config) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 40

def process_query(config)
  queries = keyword_queries.map do |clause|
    field = clause[:field]
    query = clause[:query]

    ParsingNesting::Tree.parse(query, config.advanced_search[:query_parser]).to_query(local_param_hash(field, config))
  end
  queries.join(" #{keyword_op} ")
end

#to_solrObject



17
18
19
20
21
22
23
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 17

def to_solr
  @to_solr ||= begin
    {
      q: process_query(config)
    }
  end
end