Class: Neo4j::Core::Index::LuceneQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/neo4j-core/index/lucene_query.rb

Overview

This object is returned when you call the #find method on the Node, Relationship. The actual query is not executed until the first item is requested.

You can perform a query in many different ways:

For more information about the syntax see lucene.apache.org/java/3_0_2/queryparsersyntax.html

@example: By Compound Queries

Vehicle.find(:weight).between(5.0, 100000.0).and(:name).between('a', 'd')

You can combine several queries by ANDing those together.

Examples:

By Hash


Person.find(:name => 'foo', :age => 3)

By Range


Person.find(:age).between(15,35)

By Lucene Query Syntax


Car.find('wheels:"4" AND colour: "blue")

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, index_config, query, params = {}) ⇒ LuceneQuery

Returns a new instance of LuceneQuery.



37
38
39
40
41
42
43
# File 'lib/neo4j-core/index/lucene_query.rb', line 37

def initialize(index, index_config, query, params={})
  @index = index
  @query = query
  @index_config = index_config
  @params = params
  @order = params[:sort] if params.include?(:sort)
end

Instance Attribute Details

#left_and_queryObject

Returns the value of attribute left_and_query.



35
36
37
# File 'lib/neo4j-core/index/lucene_query.rb', line 35

def left_and_query
  @left_and_query
end

#left_or_queryObject

Returns the value of attribute left_or_query.



35
36
37
# File 'lib/neo4j-core/index/lucene_query.rb', line 35

def left_or_query
  @left_or_query
end

#orderObject

Returns the value of attribute order.



35
36
37
# File 'lib/neo4j-core/index/lucene_query.rb', line 35

def order
  @order
end

#queryObject

Returns the value of attribute query.



35
36
37
# File 'lib/neo4j-core/index/lucene_query.rb', line 35

def query
  @query
end

#right_not_queryObject

Returns the value of attribute right_not_query.



35
36
37
# File 'lib/neo4j-core/index/lucene_query.rb', line 35

def right_not_query
  @right_not_query
end

Instance Method Details

#[](index) ⇒ Object

Does simply loop all search items till the n’th is found. @return[Neo4j::Node, Neo4j::Relationship] the n’th search item



70
71
72
73
74
# File 'lib/neo4j-core/index/lucene_query.rb', line 70

def [](index)
  i = 0
  each { |x| return x if i == index; i += 1 }
  nil # out of index
end

#and(query2) ⇒ Neo4j::Core::Index::LuceneQuery

Create a compound lucene query.

Examples:


Person.find(:name=>'kalle').and(:age => 3)

Parameters:

  • query2 (String)

    the query that should be AND together

Returns:



117
118
119
# File 'lib/neo4j-core/index/lucene_query.rb', line 117

def and(query2)
  LuceneQuery.new(@index, @index_config, query2).tap { |new_query| new_query.left_and_query = self }
end

#asc(*fields) ⇒ Object

Sort ascending the given fields.

Parameters:

  • fields (Symbol)

    it should sort



158
159
160
161
162
# File 'lib/neo4j-core/index/lucene_query.rb', line 158

def asc(*fields)
  @order ||= []
  @order += fields.map { |f| [f, :asc] }
  self
end

#between(lower, upper, lower_inclusive = false, upper_inclusive = false) ⇒ Object

Performs a range query Notice that if you don’t specify a type when declaring a property a String range query will be performed.



84
85
86
87
88
# File 'lib/neo4j-core/index/lucene_query.rb', line 84

def between(lower, upper, lower_inclusive=false, upper_inclusive=false)
  raise "Expected a symbol. Syntax for range queries example: index(:weight).between(a,b)" unless Symbol === @query
  @query = range_query(@query, lower, upper, lower_inclusive, upper_inclusive)
  self
end

#closeObject

Close hits

Closes the underlying search result. This method should be called whenever you’ve got what you wanted from the result and won’t use it anymore. It’s necessary to call it so that underlying indexes can dispose of allocated resources for this search result. You can however skip to call this method if you loop through the whole result, then close() will be called automatically. Even if you loop through the entire result and then call this method it will silently ignore any consequtive call (for convenience).

This must be done according to the Neo4j Java Documentation:



58
59
60
61
# File 'lib/neo4j-core/index/lucene_query.rb', line 58

def close
  @hits.close if @hits
  @hits = nil
end

#desc(*fields) ⇒ Object

Sort descending the given fields.

Parameters:

  • fields (Symbol)

    it should sort



150
151
152
153
154
# File 'lib/neo4j-core/index/lucene_query.rb', line 150

def desc(*fields)
  @order ||= []
  @order += fields.map { |f| [f, :desc] }
  self
end

#eachObject

Implements the Ruby Enumerable interface



46
47
48
# File 'lib/neo4j-core/index/lucene_query.rb', line 46

def each
  hits.each { |x| yield x.wrapper }
end

#empty?true, false

Returns True if there is no search hits.

Returns:

  • (true, false)

    True if there is no search hits.



64
65
66
# File 'lib/neo4j-core/index/lucene_query.rb', line 64

def empty?
  hits.size == 0
end

#not(query2) ⇒ Neo4j::Core::Index::LuceneQuery

Create a NOT lucene query.

Examples:


Person.find(:age => 3).not(:name=>'kalle')

Parameters:

  • query2 (String)

    the query that should exclude matching results

Returns:



143
144
145
# File 'lib/neo4j-core/index/lucene_query.rb', line 143

def not(query2)
  LuceneQuery.new(@index, @index_config, query2).tap { |new_query| new_query.right_not_query = self }
end

#or(query2) ⇒ Neo4j::Core::Index::LuceneQuery

Create an OR lucene query.

Examples:


Person.find(:name=>'kalle').or(:age => 3)

Parameters:

  • query2 (String)

    the query that should be OR together

Returns:



130
131
132
# File 'lib/neo4j-core/index/lucene_query.rb', line 130

def or(query2)
  LuceneQuery.new(@index, @index_config, query2).tap { |new_query| new_query.left_or_query = self }
end

#range_query(field, lower, upper, lower_inclusive, upper_inclusive) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/neo4j-core/index/lucene_query.rb', line 90

def range_query(field, lower, upper, lower_inclusive, upper_inclusive)
  type = @index_config.field_type(field)
  raise "no index on field #{field}" unless type
  # check that we perform a range query on the same values as we have declared with the property :key, :type => ...
  raise "find(#{field}).between(#{lower}, #{upper}): #{lower} not a #{type}" unless type == lower.class
  raise "find(#{field}).between(#{lower}, #{upper}): #{upper} not a #{type}" unless type == upper.class

  case lower
    when Fixnum
      Java::OrgApacheLuceneSearch::NumericRangeQuery.new_long_range(field.to_s, lower, upper, lower_inclusive, upper_inclusive)
    when Float
      Java::OrgApacheLuceneSearch::NumericRangeQuery.new_double_range(field.to_s, lower, upper, lower_inclusive, upper_inclusive)
    else
      Java::OrgApacheLuceneSearch::TermRangeQuery.new(field.to_s, lower, upper, lower_inclusive, upper_inclusive)
  end
end

#sizeObject

@return the number of search hits



77
78
79
# File 'lib/neo4j-core/index/lucene_query.rb', line 77

def size
  hits.size
end