<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -24,7 +24,7 @@ class NewsArticleVersion &lt; ActiveRecord::Base
   validates_presence_of :news_article
   before_validation :setup_text
   after_save :update_text
- 
+
   # populate the object from a NewsPage object
   def populate_from_page(page)
     self.text_hash = page.hash
@@ -39,32 +39,82 @@ class NewsArticleVersion &lt; ActiveRecord::Base
       self.id &lt;=&gt; b.id
     end
   end
-  
+
   def text
     @text ||= news_article_version_text.to_s
   end
-  
+
+  def to_xapian_doc
+    XapianFu::XapianDoc.new(:id =&gt; id, :title =&gt; title, :text =&gt; text,
+                            :created_at =&gt; created_at)
+  end
+
   def text=(new_text)
     @text_changed = true if @text != new_text
     @text = new_text
   end
-  
+
+  def self.xapian_search(query, options = { })
+    xapian_db_ro.ro.reopen
+    docs = xapian_db_ro.search(query, options)
+    doc_hash = { }
+    docs.each { |d| doc_hash[d.id] = d }
+    versions = find(doc_hash.keys)
+    versions.sort_by do |v|
+      doc_hash[v.id].weight
+    end.reverse
+  end
+
+  def self.xapian_db_ro
+    @xapian_db_ro ||= XapianFu::XapianDb.new(:dir =&gt; File.join(RAILS_ROOT, 'xapian/news_article_versions'),
+                                             :sortable =&gt; :created_at)
+  end
+
   private
-  
+
+  def self.xapian_db
+    @xapian_db ||= XapianFu::XapianDb.new(:dir =&gt; File.join(RAILS_ROOT, 'xapian/news_article_versions'),
+                                          :create =&gt; true, :sortable =&gt; :created_at, :index_positions =&gt; false)
+  end
+
   def setup_text
     build_news_article_version_text unless news_article_version_text
     true
   end
-  
+
+  def self.xapian_rebuild(options = { })
+    options = { :batch_size =&gt; 1000 }.merge(options)
+    puts logger.info(&quot;starting xapian_rebuild for NewsArticleVersion with options #{options.inspect}&quot;)
+    find_in_batches(options) do |batch|
+      xapian_batch_index(batch)
+    end
+  end
+
   def update_text
     if @text_changed
       news_article_version_text.update_attributes(:text =&gt; @text)
     end
     true
   end
-  
+
+  def self.xapian_batch_index(records)
+    bm = Benchmark.measure do
+      records.each { |nv| xapian_db &lt;&lt; nv.to_xapian_doc }
+    end
+    puts logger.info(&quot;#{records.size} versions (#{records.first.id}..#{records.last.id}) indexed in %.2f seconds (#{(records.size/bm.total).round}/second)&quot; % bm.total)
+  end
+
+  def self.xapian_update
+    logger.info(&quot;starting xapian_update for NewsArticleVersion&quot;)
+    last = xapian_db.documents.max(:id)
+    xapian_rebuild(:conditions =&gt; ['news_article_versions.id &gt; ?', last.id])
+  rescue Exception =&gt; e
+    xapian_db.flush
+    raise e
+  end
+
   def set_new_version
     self.version = news_article.versions_count
   end
-  
+
 end</diff>
      <filename>app/models/news_article_version.rb</filename>
    </modified>
    <modified>
      <diff>@@ -17,16 +17,16 @@
 &lt;tr&gt;
 &lt;% if version.news_article.versions_count &gt; 1 -%&gt;
   &lt;% if version.version == 0 -%&gt;
-    &lt;td&gt;&lt;%= link_to h(truncate(version.title, 75)), { :action =&gt; :diff, :id =&gt; version.news_article.id,
+    &lt;td&gt;&lt;%= link_to h(truncate(version.title, :length =&gt; 95)), { :action =&gt; :diff, :id =&gt; version.news_article.id,
       :version_a =&gt; version.version+1,
       :version_b =&gt; version.version }, :title =&gt; &quot;#{h(version.title)}, first published #{version.news_article.created_at}&quot; %&gt;&lt;/td&gt;
   &lt;% else -%&gt;
-    &lt;td&gt;&lt;%= link_to h(truncate(version.title, 75)), { :action =&gt; :diff, :id =&gt; version.news_article.id,
+    &lt;td&gt;&lt;%= link_to h(truncate(version.title, :length =&gt; 75)), { :action =&gt; :diff, :id =&gt; version.news_article.id,
       :version_a =&gt; version.version,
       :version_b =&gt; version.version-1 }, :title =&gt; &quot;#{h(version.title)}, first published #{version.news_article.created_at}&quot; %&gt;&lt;/td&gt;
   &lt;% end -%&gt;
 &lt;% else -%&gt;
-&lt;td&gt;&lt;span title=&quot;&lt;%= &quot;#{h(version.title)}, first published #{version.news_article.created_at}&quot; %&gt;&quot;&gt;&lt;%= h(truncate(version.news_article.title, 75)) %&gt;&lt;/span&gt;&lt;/td&gt;
+&lt;td&gt;&lt;span title=&quot;&lt;%= &quot;#{h(version.title)}, first published #{version.news_article.created_at}&quot; %&gt;&quot;&gt;&lt;%= h(truncate(version.news_article.title, :length =&gt; 75)) %&gt;&lt;/span&gt;&lt;/td&gt;
 &lt;% end -%&gt;
 &lt;td&gt;&lt;%= version.created_at.strftime('%d %b %Y') %&gt;&lt;/td&gt;
 &lt;td class=&quot;version-and-total&quot;&gt;&lt;%= version.version %&gt; of &lt;%= version.news_article.versions_count %&gt;&lt;/td&gt;
@@ -35,5 +35,5 @@
 &lt;% end %&gt;
 &lt;/table&gt;
 &lt;hr/&gt;
-&lt;%= will_paginate @versions %&gt;
+&lt;%= will_paginate @versions if @versions.respond_to?(:total_pages) %&gt;
 &lt;%end -%&gt;</diff>
      <filename>app/views/news_articles/search.rhtml</filename>
    </modified>
    <modified>
      <diff>@@ -7,7 +7,7 @@
 &lt;th&gt;Source&lt;/th&gt;&lt;/tr&gt;
 &lt;% @versions.each do |version| %&gt;
 &lt;tr&gt;
-&lt;% if version.version &gt; 1 %&gt;
+&lt;% if version.version &gt; 0 %&gt;
 &lt;td&gt;
   &lt;%= link_to h(truncate(version.title, :length =&gt; 75)), 
 	  diff_url(version.news_article.id, version.version - 1, version.version), :title =&gt; h(version.title) %&gt;
@@ -24,7 +24,7 @@
   &lt;%= version.votes %&gt;
 &lt;/td&gt;
 &lt;td&gt;
-  &lt;small&gt;&lt;%= version.news_article.source %&gt;&lt;/small&gt;
+  &lt;small&gt;&lt;%#= version.source %&gt;&lt;/small&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;% end %&gt;</diff>
      <filename>app/views/versions/index.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -7,5 +7,10 @@
 &lt;%= @version.text %&gt;
 &lt;/td&gt;
 &lt;td&gt;
+&lt;ul&gt;
+&lt;% @xap_doc.terms.sort_by(&amp;:wdf)[0..40].each do |term| %&gt;
+&lt;li&gt;&lt;%= term.term %&gt;&lt;/li&gt;
+&lt;% end %&gt;
+&lt;/ul&gt;
 &lt;/td&gt;
 &lt;/table&gt;
\ No newline at end of file</diff>
      <filename>app/views/versions/show.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -34,6 +34,7 @@ Rails::Initializer.run do |config|
   
   config.gem 'web-page-parser'
   config.gem &quot;diff-lcs&quot;, :lib =&gt; &quot;diff/lcs&quot;
+  config.gem &quot;xapian-fu&quot;, :lib =&gt; 'xapian_fu'
   
 
   # Only load the plugins named here, in the order given. By default, all plugins 
@@ -88,3 +89,4 @@ require 'diff_html'
 require 'http'
 require 'zlib'
 include HTTP
+require 'xapian_fu'</diff>
      <filename>config/environment.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>cc72ffa2fa60e9af8cd184e30caaee341c3cfd0e</id>
    </parent>
    <parent>
      <id>07334c203c350cd294e9ac29249f2ace38bc70e6</id>
    </parent>
  </parents>
  <author>
    <name>John Leach</name>
    <email>john@johnleach.co.uk</email>
  </author>
  <url>http://github.com/johnl/news-sniffer/commit/4490393669013c340bd51efa94d0151ac3c025e9</url>
  <id>4490393669013c340bd51efa94d0151ac3c025e9</id>
  <committed-date>2009-07-06T13:41:51-07:00</committed-date>
  <authored-date>2009-07-06T13:35:49-07:00</authored-date>
  <message>Merge branch 'xapian'

Conflicts:
	app/controllers/news_articles_controller.rb
	app/models/news_article_version.rb
	app/views/news_articles/search.rhtml
	app/views/versions/index.html.erb
	config/routes.rb</message>
  <tree>30e4fcbc801e4305c17393ce0b78565e1c89e2d6</tree>
  <committer>
    <name>John Leach</name>
    <email>john@johnleach.co.uk</email>
  </committer>
</commit>
