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:
Example:
Person.find(:name => 'foo', :age => 3)
Example:
Person.find(:age).between(15,35)
Example
Car.find('wheels:"4" AND colour: "blue")
For more information about the syntax see lucene.apache.org/java/3_0_2/queryparsersyntax.html
You can combine several queries by ANDing those together.
Example:
Vehicle.find(:weight).between(5.0, 100000.0).and(:name).between('a', 'd')
Neo4j::Index::Indexer#find - which returns an LuceneQuery
# File lib/neo4j/index/lucene_query.rb, line 43 def initialize(index, decl_props, query, params={}) @index = index @query = query @decl_props = decl_props @params = params if params.include?(:sort) @order = {} params[:sort].each_pair { |k, v| @order[k] = (v == :desc) } end end
returns the n’th search item Does simply loop all search items till the n’th is found.
# File lib/neo4j/index/lucene_query.rb, line 102 def [](index) i = 0 each{|x| return x if i == index; i += 1} nil # out of index end
Create a compound lucene query.
the query that should be AND together
Person.find(:name=>'kalle').and(:age => 3)
# File lib/neo4j/index/lucene_query.rb, line 158 def and(query2) LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.left_and_query = self } end
Sort ascending the given fields.
# File lib/neo4j/index/lucene_query.rb, line 196 def asc(*fields) @order = fields.inject(@order || {}) { |memo, field| memo[field] = false; memo } self end
Performs a range query Notice that if you don’t specify a type when declaring a property a String range query will be performed.
# File lib/neo4j/index/lucene_query.rb, line 121 def between(lower, upper, lower_incusive=false, upper_inclusive=false) raise "Expected a symbol. Syntax for range queries example: index(:weight).between(a,b)" unless Symbol === @query raise "Can't only do range queries on Neo4j::NodeMixin, Neo4j::Model, Neo4j::RelationshipMixin" unless @decl_props # check that we perform a range query on the same values as we have declared with the property :key, :type => ... type = @decl_props[@query] && @decl_props[@query][:type] raise "find(#{@query}).between(#{lower}, #{upper}): #{lower} not a #{type}" if type && !type === lower.class raise "find(#{@query}).between(#{lower}, #{upper}): #{upper} not a #{type}" if type && !type === upper.class # Make it possible to convert those values @query = range_query(@query, lower, upper, lower_incusive, upper_inclusive) self end
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:
# File lib/neo4j/index/lucene_query.rb, line 89 def close @hits.close if @hits @hits = nil end
Sort descending the given fields.
# File lib/neo4j/index/lucene_query.rb, line 190 def desc(*fields) @order = fields.inject(@order || {}) { |memo, field| memo[field] = true; memo } self end
Since we include the Ruby Enumerable mixin we need this method.
# File lib/neo4j/index/lucene_query.rb, line 63 def each if @params.include?(:per_page) # paginate the result, used by the will_paginate gem page = @params[:page] || 1 per_page = @params[:per_page] to = per_page * page from = to - per_page i = 0 hits.each do |node| yield node.wrapper if i >= from i += 1 break if i >= to end else hits.each { |n| yield n.wrapper } end end
True if there is no search hits.
# File lib/neo4j/index/lucene_query.rb, line 95 def empty? hits.size == 0 end
Create a NOT lucene query.
the query that should exclude matching results
Person.find(:age => 3).not(:name=>'kalle')
# File lib/neo4j/index/lucene_query.rb, line 184 def not(query2) LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.right_not_query = self } end
Create an OR lucene query.
the query that should be OR together
Person.find(:name=>'kalle').or(:age => 3)
# File lib/neo4j/index/lucene_query.rb, line 171 def or(query2) LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.left_or_query = self } end
# File lib/neo4j/index/lucene_query.rb, line 134 def range_query(field, lower, upper, lower_incusive, upper_inclusive) lower = TypeConverters.convert(lower) upper = TypeConverters.convert(upper) case lower when Fixnum org.apache.lucene.search.NumericRangeQuery.new_long_range(field.to_s, lower, upper, lower_incusive, upper_inclusive) when Float org.apache.lucene.search.NumericRangeQuery.new_double_range(field.to_s, lower, upper, lower_incusive, upper_inclusive) else org.apache.lucene.search.TermRangeQuery.new(field.to_s, lower, upper, lower_incusive, upper_inclusive) end end
Returns the number of search hits
# File lib/neo4j/index/lucene_query.rb, line 109 def size hits.size end