Class: ParsingNesting::Tree::List

Inherits:
Node show all
Defined in:
lib/parsing_nesting/tree.rb

Direct Known Subclasses

AndList, OrList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aList, query_parser) ⇒ List

Returns a new instance of List.



135
136
137
138
# File 'lib/parsing_nesting/tree.rb', line 135

def initialize(aList, query_parser)
  @query_parser = query_parser
  self.list = aList
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



133
134
135
# File 'lib/parsing_nesting/tree.rb', line 133

def list
  @list
end

#query_parserObject (readonly)

Returns the value of attribute query_parser.



134
135
136
# File 'lib/parsing_nesting/tree.rb', line 134

def query_parser
  @query_parser
end

Instance Method Details

#can_embed?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/parsing_nesting/tree.rb', line 140

def can_embed?
  false
end

#negateObject



198
199
200
# File 'lib/parsing_nesting/tree.rb', line 198

def negate
  List.new(list.collect { |i| i.negate })
end

#simple_pure_negative?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/parsing_nesting/tree.rb', line 144

def simple_pure_negative?
  list.find_all { |i| i.is_a? ExcludedClause }.length == list.length
end

#to_query(solr_params = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/parsing_nesting/tree.rb', line 148

def to_query(solr_params = {})
  queries = []

  (embeddable, gen_full_query) = list.partition { |i| i.respond_to?(:can_embed?) && i.can_embed? }

  unless embeddable.empty?
    queries << build_nested_query(embeddable, solr_params, force_deftype: query_parser)
  end

  gen_full_query.each do |node|
    queries << node.to_query(solr_params)
  end

  queries.join(" AND ")
end

#to_single_query_params(solr_local_params) ⇒ Object

Returns a Hash, assumes this will be the ONLY :q, used for parsing ‘simple search’ to Solr. Pass in params that need to be LOCAL solr params (using “foo=bar” embedded in query). Params that should be sent to Solr seperately are caller’s responsibility, merge em into the returned hash.

For very simple queries, this will produce an ordinary Solr q much like would be produced ordinarily. But for AND/OR/NOT, will sometimes include multiple nested queries instead.

This method will still sometimes return a single nested query, that could theoretically really be ordinary query possibly with localparams. It still works, but isn’t optimizing for a simpler query, because it’s using much of the same code used for combining multiple fields that need nested queries. Maybe we’ll optimize later, but the code gets tricky.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/parsing_nesting/tree.rb', line 180

def to_single_query_params(solr_local_params)
  # Can it be expressed in a single dismax?

  if list.find_all { |i| i.respond_to?(:can_embed?) && i.can_embed? }.length == list.length
    {
      # build_local_params(solr_local_params, nil) + list.collect {|n| n.to_embed}.join(" "),
      :q => build_nested_query(list, solr_local_params, :always_nested => false, :force_deftype => nil),
      :defType => query_parser
    }
  else
    # Can't be expressed in a single dismax, do it the normal way
    {
      :q => self.to_query(solr_local_params),
      :defType => "lucene"
    }
  end
end