Class: QueryParser::Term

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

Overview

A basic search term. The input query is tokenised into terms which then cat manipulated to create the query tree.

Generally you should not need to handle this class unless you are changing the parser works.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Term

Takes the token from the user’s query and classify it:

open

The opening ( used to indicate the start of a parentisised part of the query.

close

The closing ) used to indicate the end of a parentisised part of the query.

and

The term indicating conjunction

or

The term indicating disjunction

not

The term indicating negation

term

None of the above. A term to find.



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/queryparser.rb', line 429

def initialize(data)
  @type = 'term'
  @data = data
  @was_reduced = false

  if @data == nil then
    @data = ''
  else
    case @data.downcase
      when "("
        @type = "open"
      when ")"
        @type = "close"
      when "and", "or", "not"
        @type = 'op'
        @data = @data.downcase
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



449
450
451
# File 'lib/queryparser.rb', line 449

def data
  @data
end

#typeObject (readonly)

Returns the value of attribute type.



449
450
451
# File 'lib/queryparser.rb', line 449

def type
  @type
end

Instance Method Details

#boostable(negative = false) ⇒ Object

The query can be traversed to return the terms that are considered boostable. In the following apple will be considered positive and returned but banana will not:

apple not banana

Terms that are boostable can be used to improve the documents relavance / position in the results list.



499
500
501
502
503
504
505
# File 'lib/queryparser.rb', line 499

def boostable(negative = false)
  if negative == true then
    return nil
  else
    return self
  end
end

#inspectObject

Display the Term, useful for debugging and testing the Term class in isolation



453
454
455
# File 'lib/queryparser.rb', line 453

def inspect
  "#{@type}:#{@data}"
end

#lucene(field, suffix = nil) ⇒ Object

Convert a term into string usable in a Lucene query with an optional similarity



459
460
461
# File 'lib/queryparser.rb', line 459

def lucene(field, suffix = nil)
  "#{field}:#{@data}#{suffix}"
end

#reduceObject

Even though a term cannot, itself, be reduced the process will call this method on everything that is in the query. So we need to have this.



466
467
468
469
# File 'lib/queryparser.rb', line 466

def reduce
  @was_reduced = false
  return self
end

#reduced?Boolean

Return true if the previous call to #reduce did actually reduce the term. Again this is a method universal to all parts of the query and so we have to have it. But see #set_reduced to see why it can actually return true.

Returns:

  • (Boolean)


476
477
478
# File 'lib/queryparser.rb', line 476

def reduced?
  @was_reduced
end

#set_reducedObject

If the term was the only member of an and, or or double (or any multiple of two) not then it will replace the and, or or not in the query and therefore the original term has reduced and this, the replacement term, needs to indicate that fact. This allows us to flag that.



486
487
488
# File 'lib/queryparser.rb', line 486

def set_reduced
  @was_reduced = true
end