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.



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

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

Instance Attribute Details

#listObject

Returns the value of attribute list.



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

def list
  @list
end

#query_parserObject (readonly)

Returns the value of attribute query_parser.



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

def query_parser
  @query_parser
end

Instance Method Details

#can_embed?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/parsing_nesting/tree.rb', line 142

def can_embed?
  false
end

#negateObject



200
201
202
# File 'lib/parsing_nesting/tree.rb', line 200

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

#simple_pure_negative?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/parsing_nesting/tree.rb', line 146

def simple_pure_negative?      
  (list.find_all {|i|  i.kind_of? ExcludedClause }.length) == list.length      
end

#to_query(solr_params = {}) ⇒ Object



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

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.



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

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