Class: QueryParser::Set
- Inherits:
-
Object
- Object
- QueryParser::Set
- Defined in:
- lib/queryparser.rb
Overview
The base class for the and
and or
sets.
Generally you should not need to handle this class unless you are changing the parser works.
Instance Method Summary collapse
-
#add(*data) ⇒ Object
Add a list of
terms
,nots
and othersets
to the list of things that are in this part of the query. -
#boostable(negative = false) ⇒ Object
Return all the boostable terms that are held in the set.
-
#contents ⇒ Object
Returns all the data held by this set.
-
#initialize ⇒ Set
constructor
A new instance of Set.
-
#inspect ⇒ Object
Display the set, useful for debugging and testing.
-
#lucene(field, similarity = nil) ⇒ Object
Convert a set into string usable in a Lucene query with an optional similarity that needs to be passed to the Terms.
-
#reduce ⇒ Object
A Set contained within a Set should fold the contents of the inner set into itself.
-
#reduced? ⇒ Boolean
Did calling #reduce on this set actually reduce it.
-
#set_reduced ⇒ Object
Force the reduced flag to true.
Constructor Details
#initialize ⇒ Set
Returns a new instance of Set.
513 514 515 516 |
# File 'lib/queryparser.rb', line 513 def initialize @data = Array.new @was_reduced = false end |
Instance Method Details
#add(*data) ⇒ Object
Add a list of terms
, nots
and other sets
to the list of things that are in this part of the query.
Can handle a list of items or just a single one.
523 524 525 526 527 528 529 530 531 |
# File 'lib/queryparser.rb', line 523 def add(*data) data.each do |i| if i.class == Array then i.each {|j| add(j)} else @data << i end end end |
#boostable(negative = false) ⇒ Object
Return all the boostable terms that are held in the set. Thus for
tom and dick and harry
The terms tom
, dick
and harry
are all considered boostable. However in
tom and dick and not harry
Only the terms tom
and dick
are considered boostable
619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/queryparser.rb', line 619 def boostable(negative = false) r = Array.new @data.each do |i| x = i.boostable(negative) if x != nil then r << x end end return r.flatten end |
#contents ⇒ Object
Returns all the data held by this set
534 535 536 |
# File 'lib/queryparser.rb', line 534 def contents @data end |
#inspect ⇒ Object
Display the set, useful for debugging and testing
539 540 541 542 543 |
# File 'lib/queryparser.rb', line 539 def inspect r = Array.new @data.each {|i|r << i.inspect} "<#{self.inspect_class} #{r.join(' ')}>" end |
#lucene(field, similarity = nil) ⇒ Object
Convert a set into string usable in a Lucene query with an optional similarity that needs to be passed to the Terms
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/queryparser.rb', line 548 def lucene(field, similarity = nil) r = Array.new @data.each do |i| x = '' if self.class == QueryParser::And and i.class != QueryParser::Not then x = '+' end x << i.lucene(field, similarity) r << x end if r.size == 1 then "#{r[0]}" else "(#{r.join(' ')})" end end |
#reduce ⇒ Object
A Set contained within a Set should fold the contents of the inner set into itself. Otherwise reduce the contents of the Set individually and set the flag if the contents reduced
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/queryparser.rb', line 569 def reduce r = Array.new @was_reduced = false @data.each do |i| if self.class == i.class then @was_reduced = true i.contents.each do |c| r << c.reduce end else x = i.reduce if x.reduced? then @was_reduced = true end r << x end end if r.size == 1 then @was_reduced = true r[0].set_reduced return r.first else @data = r return self end end |
#reduced? ⇒ Boolean
Did calling #reduce on this set actually reduce it
599 600 601 |
# File 'lib/queryparser.rb', line 599 def reduced? @was_reduced end |
#set_reduced ⇒ Object
Force the reduced flag to true
604 605 606 |
# File 'lib/queryparser.rb', line 604 def set_reduced @was_reduced = true end |