Skip to content

Commit

Permalink
Finished impl indexing of Date and DateTime. [#2 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Dec 18, 2008
1 parent 899925e commit c600ad9
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 6 deletions.
16 changes: 14 additions & 2 deletions README.rdoc
Expand Up @@ -517,7 +517,7 @@ Example, to set a property to any type
Neo4j.load(node.neo_node_id).foo.class # => Array


=== Property of type Date
=== Property of type Date and DateTime

Example of using Date queries:

Expand All @@ -527,9 +527,21 @@ Example of using Date queries:
index :since, :type => Date
end

node.since = Date.new 2008,05,06
MyNode.find("born:[20080427 TO 20100203]")[0].since # => Date 2008,05,06

TODO MyNode.find
Example of using DateTime queries:

class MyNode
include Neo4j::NodeMixin
property :since, :type => DateTime
index :since, :type => DateTime
end

node.since = DateTime.civil 2008,04,27,15,25,59
MyNode.find("since:[200804271504 TO 201002031534]")[0].since # => DateTime ...

Only UTC timezone is allowed.

=== Finding all nodes

Expand Down
17 changes: 16 additions & 1 deletion lib/lucene/field_info.rb
Expand Up @@ -57,6 +57,16 @@ def convert_to_ruby(value)
date = org.apache.lucene.document.DateTools.stringToDate(value)
seconds_since_1970 = date.getTime / 1000
Date.new(*Time.at(seconds_since_1970).to_a[3 .. 5].reverse)
when DateTime.to_s
return value if value.kind_of? DateTime
return nil if value.nil?
year = value[0..3].to_i
month = value[4..5].to_i
day = value[6..7].to_i
hour = value[8..9].to_i
min = value[10..11].to_i
sec = value[12..13].to_i
DateTime.civil(year,month,day,hour,min,sec)
else
raise ConversionNotSupportedException.new("Can't convert key '#{value}' of with type '#{@info[:type].class.to_s}'")
end
Expand All @@ -74,9 +84,14 @@ def convert_to_lucene(value)
when Float.to_s then sprintf('%024.12f', value) # TODO: configurable
when Bignum.to_s then sprintf('%024d, value')
when Date.to_s
t = Time.gm(value.year, value.month, value.day)
t = Time.utc(value.year, value.month, value.day)
d = t.to_i * 1000
org.apache.lucene.document.DateTools.timeToString(d,org.apache.lucene.document.DateTools::Resolution::DAY )
when DateTime.to_s
# only utc times are supported
t = Time.utc(value.year, value.month, value.day, value.hour, value.min, value.sec)
d = t.to_i * 1000
org.apache.lucene.document.DateTools.timeToString(d,org.apache.lucene.document.DateTools::Resolution::SECOND )
else value.to_s
end
end
Expand Down
51 changes: 49 additions & 2 deletions test/lucene/index_spec.rb
Expand Up @@ -418,7 +418,7 @@
end


it "can be used to convert and store Date properties" do
it "can be used to convert and store Date field" do
#given
@index.field_infos[:since][:store] = true
@index.field_infos[:since][:type] = Date
Expand All @@ -437,6 +437,29 @@
hits[0][:since].day.should == 26
end


it "can be used to convert and store DateTime field" do
#given
@index.field_infos[:since][:store] = true
@index.field_infos[:since][:type] = DateTime
date = DateTime.new(2008,12,18,11,4,59)
@index << {:id => 1, :since => date}
@index.commit

# when
hits = @index.find(:id => "1")

# then
hits.size.should == 1
hits[0][:since].should be_instance_of(DateTime)
hits[0][:since].year.should == 2008
hits[0][:since].month.should == 12
hits[0][:since].day.should == 18
hits[0][:since].hour.should == 11
hits[0][:since].min.should == 4
hits[0][:since].sec.should == 59
end

end

describe Index, " when updating a document" do
Expand Down Expand Up @@ -505,6 +528,9 @@
@index = Index.new('myindex')
@index.field_infos[:since][:store] = false
@index.field_infos[:since][:type] = Date

@index.field_infos[:born][:store] = false
@index.field_infos[:born][:type] = DateTime
end


Expand All @@ -521,7 +547,7 @@
hits[0][:id].should == '1'
end

it "can find an index using a date range" do
it "can find an index using a Date range" do
#given
@index << {:id => 1, :since => Date.new(2008,4,26)}
@index.commit
Expand All @@ -535,6 +561,27 @@
@index.find("since:[20000425 TO 20200426]")[0][:id].should == '1'
end

it "can find an index using a DateTime range" do
#given
# only UTC times are supported
@index << {:id => 1, :born => DateTime.civil(2008,4,26,15,58,02)}
@index.commit

# then
@index.find("born:[20080427 TO 20100203]").size.should == 0
@index.find("born:[20080422 TO 20080425]").size.should == 0
@index.find("born:[20080426 TO 20090203]")[0][:id].should == '1'
@index.find("born:[20080425 TO 20080427]")[0][:id].should == '1'
@index.find("born:[20000425 TO 20200426]")[0][:id].should == '1'

@index.find("born:[200804260000 TO 200804262359]")[0][:id].should == '1'
@index.find("born:[200804261500 TO 200804261600]")[0][:id].should == '1'
@index.find("born:[200804261557 TO 200804261559]")[0][:id].should == '1'
@index.find("born:[20080426155759 TO 20080426155804]")[0][:id].should == '1'
@index.find("born:[200804261559 TO 200804261601]").size.should == 0
@index.find("born:[200804261557 TO 200804261500]").size.should == 0
end

end
end

38 changes: 37 additions & 1 deletion test/neo4j/node_lucene_spec.rb
Expand Up @@ -335,7 +335,7 @@ class TestNode
found.size.should == 0
end

describe "Find Nodes using Lucene date index" do
describe "Find Nodes using Lucene Date index" do
before(:each) do
undefine_class :PersonNode
start
Expand Down Expand Up @@ -365,5 +365,41 @@ class PersonNode
result.size.should == 1
end
end

describe "Find Nodes using Lucene DateTime index" do
before(:each) do
undefine_class :PersonNode
start
class PersonNode
include Neo4j::NodeMixin
property :name
property :since, :type => DateTime
index :name
index :since, :type => DateTime
end
end

after(:each) do
stop
end

it "should find using a date query" do
result = PersonNode.find("since:[200804271504 TO 201002031534]")
result.size.should == 0
node = PersonNode.new
node.since.should be_nil
node.name = 'kalle'
# only UTC Times are supported
node.since = DateTime.civil 2008,04,27,15,25,59

# when
result = PersonNode.find("since:[200804271504 TO 201002031534]")
result.size.should == 1
result[0].since.class.should == DateTime
result[0].since.year.should == 2008
result[0].since.min.should == 25
end
end

end

0 comments on commit c600ad9

Please sign in to comment.