Changeset 283

Show
Ignore:
Timestamp:
04/27/08 10:27:52 (7 months ago)
Author:
eric.dumin..@gmail.com
Message:

possibility to sort results by relevance / by date

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/History.txt

    r281 r283  
    11== 0.1.7  2008-04- 
    22 
    3 * 1 minor enhancement: 
     3* 4 minor enhancement: 
    44  * added cache highlighting à la Google 
    55  * rake index:update implemented as described in Ferret book by David Balmain 
    66  * rake index:prune removes missing files from indexer. 
     7  * possibility to sort results by relevance / by date 
    78 
    89== 0.1.6  2008-04-25 
  • trunk/lib/picolena/templates/app/controllers/documents_controller.rb

    r277 r283  
    2323    start=Time.now 
    2424      @query=[params[:id],params.delete(:format)].compact.join('.') 
     25      sort=params[:sort] 
    2526      page=params[:page]||1 
    26       finder=Finder.new(@query,page) 
     27      finder=Finder.new(@query,sort,page) 
    2728      finder.execute! 
    2829      pager=::Paginator.new(finder.total_hits, Picolena::ResultsPerPage) do 
  • trunk/lib/picolena/templates/app/helpers/documents_helper.rb

    r277 r283  
    44    @matching_documents.nil? or @matching_documents.entries.empty? 
    55  end 
    6  
     6   
    77  # Very basic pagination. 
    88  # Provides liks to Next, Prev and FirstPage when needed. 
     
    1212     (link_to("→", :action => :show, :id => query, :page => page.next.number) if page.next?)].compact.join(" | ") 
    1313  end 
    14  
     14   
    1515  # Returns a localized sentence like "Results 1-10 of 12 for Zimbabwe (0.472s)" or 
    1616  # "Résultats 1-2 parmi 2 pour whatever (0.012s)" 
     
    2525    ].join(' ') 
    2626  end 
    27  
     27   
    2828  # Returns the time needed to treat the query and launch the search, with a ms precision : (0.472s) 
    2929  def show_time_needed(dt) 
     
    8080    h(document.highlighted_cache(query)).gsub(/\n/,'<br/>').gsub(/&lt;&lt;(.*?)&gt;&gt;/,content_tag(:span, '\1', :class=>"matching_content")) 
    8181  end 
     82   
     83  def sort_by_date_or_relevance(query) 
     84    [link_to_unless_current('By date', document_path(query, :sort=>'by_date')), 
     85     link_to_unless_current('By relevance', document_path(query))].join("&nbsp;") 
     86  end 
    8287end 
  • trunk/lib/picolena/templates/app/models/finder.rb

    r276 r283  
    66  end 
    77 
    8   def initialize(raw_query,page=1,results_per_page=Picolena::ResultsPerPage) 
     8  def initialize(raw_query,by_date=false, page=1,results_per_page=Picolena::ResultsPerPage) 
    99    @query = Query.extract_from(raw_query) 
    1010    @raw_query= raw_query 
     
    1212    @per_page=results_per_page 
    1313    @offset=(page.to_i-1)*results_per_page 
     14    @by_date=by_date 
    1415    index_should_have_documents 
    1516  end 
     
    1819    @matching_documents=[] 
    1920    start=Time.now 
    20       @total_hits = index.search_each(query, :limit => @per_page, :offset=>@offset){|index_id, score| 
     21      @total_hits = index.search_each(query, :limit => @per_page, :offset=>@offset, :sort => (sort_by_date if @by_date)){|index_id, score| 
    2122        begin 
    2223          found_doc=Document.new(index[index_id][:complete_path]) 
     
    5758 
    5859  private 
     60   
     61  def sort_by_date 
     62    Ferret::Search::SortField.new(:modified, :type => :byte, :reverse => true) 
     63  end 
    5964 
    6065  def index_should_have_documents 
  • trunk/lib/picolena/templates/app/models/indexer.rb

    r282 r283  
    147147        field_infos.add_field(:modified,           :store => :yes, :index => :untokenized) 
    148148        field_infos.add_field(:probably_unique_id, :store => :no,  :index => :untokenized) 
    149         field_infos.add_field(:language,           :store => :yes, :index => :yes
     149        field_infos.add_field(:language,           :store => :yes, :index => :untokenized
    150150      end 
    151151    end 
  • trunk/lib/picolena/templates/app/views/documents/show.html.haml

    r277 r283  
    1010        %span{:class=>'pagination'}=should_paginate(@matching_documents, @query) 
    1111        =describe_results(@matching_documents, @total_hits, @time_needed, h(@query)) 
     12-unless nothing_found? 
     13  %p 
     14    %span{:class=>'sort_by'}=sort_by_date_or_relevance(@query) 
    1215= render :partial =>'document', :collection => @matching_documents, :locals => { :query => @query} 
  • trunk/lib/picolena/templates/public/stylesheets/style.css

    r277 r283  
    8080        color: #fff; 
    8181        font-weight: bold; 
     82        text-decoration:none; 
     83} 
     84 
     85.sort_by { 
     86        float:right; 
     87        font-size: 13px; 
     88        color:#000; 
     89} 
     90 
     91.sort_by a{ 
     92        color: #EE8907; 
    8293        text-decoration:none; 
    8394} 
     
    117128#results { 
    118129        width:778px; 
    119         padding-top: 25px; 
     130        padding-top: 15px; 
    120131} 
    121132 
  • trunk/lib/picolena/templates/spec/models/basic_finder_spec.rb

    r263 r283  
    5151  end 
    5252 
    53   it "should accept one parameter as query, and 2 optionals for paginating" do 
     53  it "should accept one parameter as query, 1 optional for sorting results and 2 optionals for paginating" do 
    5454    lambda {Finder.new}.should raise_error(ArgumentError, "wrong number of arguments (0 for 1)") 
    5555    # show first page with 10 results per page 
    5656    lambda {Finder.new("a b")}.should_not raise_error 
    5757    # show second page 
    58     lambda {Finder.new("a", 2)}.should_not raise_error 
     58    lambda {Finder.new("a", "by_date")}.should_not raise_error 
    5959    # show first page with 15 results 
    60     lambda {Finder.new("a", 1, 15)}.should_not raise_error 
     60    lambda {Finder.new("a", "by_date", 1, 15)}.should_not raise_error 
    6161    # Too many parameters 
    62     lambda {Finder.new("a", 10, 20, 30)}.should raise_error(ArgumentError, "wrong number of arguments (4 for 3)") 
     62    lambda {Finder.new("a", "by_date", 10, 20, 30)}.should raise_error(ArgumentError, "wrong number of arguments (5 for 4)") 
    6363  end 
    6464 
  • trunk/lib/picolena/templates/spec/models/finder_spec.rb

    r270 r283  
    99 
    1010def matching_document_for(query) 
    11   # Returns matching document for any given query only if 
    12   # exactly one document is found. 
    13   # Specs don't pass otherwise. 
     11  # Returns matching document for any given query only if 
     12  # exactly one document is found. 
     13  # Specs don't pass otherwise. 
    1414  matching_documents=Finder.new(query).matching_documents 
    1515  matching_documents.size.should == 1