Class: BlacklightAdvancedSearch::QueryParser

Inherits:
Object
  • Object
show all
Includes:
FilterParser, ParsingNestingParser
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

Methods included from FilterParser

#generate_solr_fq

Methods included from ParsingNestingParser

#local_param_hash, #process_query

Constructor Details

#initialize(params, 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(params, config)
  @params = Blacklight::SearchState.new(params, config).to_h
  @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

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 66

def empty?
  filters.empty? && keyword_queries.empty?
end

#filtersObject

extracts advanced-type filters from query params, returned as a hash of field => [array of values]



51
52
53
54
55
56
57
58
59
60
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 51

def filters
  unless @filters
    @filters = {}
    return @filters unless @params[:f_inclusive] && @params[:f_inclusive].respond_to?(:each_pair)
    @params[:f_inclusive].each_pair do |field, value_array|
      @filters[field] ||= value_array.dup
    end
  end
  @filters
end

#filters_include_value?(field, value) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 62

def filters_include_value?(field, value)
  filters[field.to_s].try { |array| array.include? value }
end

#keyword_opObject

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



27
28
29
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 27

def keyword_op
  @params["op"] || "AND"
end

#keyword_queriesObject

extracts advanced-type keyword query elements from query params, returns as a kash of field => query. see also keyword_op



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/blacklight_advanced_search/advanced_query_parser.rb', line 34

def keyword_queries
  unless @keyword_queries
    @keyword_queries = {}

    return @keyword_queries unless @params[:search_field] == ::AdvancedController.blacklight_config.advanced_search[:url_key]

    config.search_fields.each do |key, _field_def|
      unless @params[key.to_sym].blank?
        @keyword_queries[key] = @params[key.to_sym]
      end
    end
  end
  @keyword_queries
end

#to_solrObject



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

def to_solr
  @to_solr ||= begin
    {
      :q => process_query(params, config),
      :fq => generate_solr_fq
    }
  end
end