On 7/13/06, Floyd Morgan <[EMAIL PROTECTED]> wrote:
> Here is the index snippet:
>
> doc = Ferret::Document::Document.new
> # insert the id
> doc << Ferret::Document::Field.new( "id", post.id,
> Ferret::Document::Field::Store::YES,
> Ferret::Document::Field::Index::UNTOKENIZED )
> # insert the date
> doc << Ferret::Document::Field.new( "created_at", post.created_at,
> Ferret::Document::Field::Store::NO,
> Ferret::Document::Field::Index::UNTOKENIZED )
> # add some other stuff ...
> # write to the index
> index << doc
>
> Here is the query snippet:
>
> sort_fields = []
> sort_fields << Ferret::Search::SortField.new
> ( "created_at", :sort_type =>
> Ferret::Search::SortField::SortType::INTEGER, :reverse => true )
> # search the index
> top_docs = index.search( query, { :first_doc =>
> first_doc , :num_docs => 5, :sort => sort_fields } )
I'm not exactly sure what post.created_at but if it's a Time object
then you need to convert it to a string that will sort correctly as a
string. ie use strftime("%Y%m%d") (use whatever precision you need.
Here is an example which adds 100 documents with 100 random dates in
the last 100 days;
require 'rubygems'
require 'ferret'
include Ferret::Index
include Ferret::Search
index = Index.new
t = Time.now
100.times do
index << {:id => "x",
:date => (t-24*60*60*rand(100)).strftime("%Y%m%d")}
end
sort_fields = [SortField.new(:date,
:sort_type => SortField::SortType::INTEGER,
:reverse => true)]
10.times do |start|
index.search_each("x",
:first_doc => start*10,
:num_docs => 10,
:sort => sort_fields) do |doc_id, score|
puts index[doc_id][:date]
end
end
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk