Class: Neo4j::Core::Index::LuceneQuery
- Inherits:
-
Object
- Object
- Neo4j::Core::Index::LuceneQuery
- 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 AND
ing those together.
Instance Attribute Summary collapse
-
#left_and_query ⇒ Object
Returns the value of attribute left_and_query.
-
#left_or_query ⇒ Object
Returns the value of attribute left_or_query.
-
#order ⇒ Object
Returns the value of attribute order.
-
#query ⇒ Object
Returns the value of attribute query.
-
#right_not_query ⇒ Object
Returns the value of attribute right_not_query.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Does simply loop all search items till the n’th is found.
-
#and(query2) ⇒ Neo4j::Core::Index::LuceneQuery
Create a compound lucene query.
-
#asc(*fields) ⇒ Object
Sort ascending the given fields.
-
#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.
-
#close ⇒ Object
Close hits.
-
#desc(*fields) ⇒ Object
Sort descending the given fields.
-
#each ⇒ Object
Implements the Ruby
Enumerable
interface. -
#empty? ⇒ true, false
True if there is no search hits.
-
#initialize(index, index_config, query, params = {}) ⇒ LuceneQuery
constructor
A new instance of LuceneQuery.
-
#not(query2) ⇒ Neo4j::Core::Index::LuceneQuery
Create a NOT lucene query.
-
#or(query2) ⇒ Neo4j::Core::Index::LuceneQuery
Create an OR lucene query.
- #range_query(field, lower, upper, lower_inclusive, upper_inclusive) ⇒ Object
-
#size ⇒ Object
@return the number of search hits.
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_query ⇒ Object
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_query ⇒ Object
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 |
#order ⇒ Object
Returns the value of attribute order.
35 36 37 |
# File 'lib/neo4j-core/index/lucene_query.rb', line 35 def order @order end |
#query ⇒ Object
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_query ⇒ Object
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.
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.
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 |
#close ⇒ Object
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.
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 |
#each ⇒ Object
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.
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.
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.
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 |
#size ⇒ Object
@return the number of search hits
77 78 79 |
# File 'lib/neo4j-core/index/lucene_query.rb', line 77 def size hits.size end |