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