Highlight search terms in a result set and distill the text surrounding the search terms.
Ruby
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
benchmarking
lib
spec
.gitignore
.travis.yml
Changes.md
Gemfile
Gemfile.lock
README.md
Rakefile
fluorescent.gemspec

README.md

fluorescent

Gem Version Build Status Dependency StatusCode Climate

Highlight search terms in a result set and distill the text surrounding the search terms.

Example:

padding: 10

search terms: "this is a test"

search results: "what do you think this is a test"

padded+highlighted display: "...t do you think this is a test..."

Basic example:

Here is a search method using PgSearch to retrieve results in a Rails model:

  def self.search(params)
    order_options =  params.has_key?('order_by')     ?
      { params['order_by'] => params['order_type'] } :
      { :published_on => 'desc'}
    results = with_author.published.fast_search(params['q'])
                         .page(params['page'])
                         .order(order_options)

     ## this is something I created to simply partition
     ## the formatted results and raw activerecord object collections
     Search::Result.new(
       :results   => results,
       :terms     => params['q'],
       :columns   => [:title,:body],
       :to_filter => [:body]
     )
  end

The Search::Result listing:

  require 'fluorescent'
  module Search
    class Result
      attr_reader :formatter, :raw
      def initialize args
        @formatter = Fluorescent.new(args)
        @raw   = args[:results]
      end
    end
  end

An example controller:

class SearchController < ApplicationController
  def index
    @results = []
    if params[:q]
      @results = Post.search(params.to_h)
      respond_to do |format|
        format.html { render action: "index" }
        format.json { render json: @results }
      end
    end
  end
end

And an example of how to display the results in a view:

<% if params[:q] %>
  <% if @results.formatter.formatted_results.any? %>
  <div class="search-results">
    <% @results.formatter.formatted_results.each do |r| %>
      <div class="blog-post">
        <div><%= link_to r[:title].html_safe, post_path(r[:id]) %></div>
        <div class="blog-post-meta">
          by <%= link_to post_url User.find(r[:user_id]), r[:user_id] %>
          on <%= r[:published_on] %>
        </div>
        <div class="blog-post-body">
          <%= r[:body].html_safe %>
        </div>
      </div>
      <hr />
    <% end %>
  </div>
  <% elsif !@results.formatter.formatted_results.any? %>
  <div class="search-result">
    (No results)
  </div>
<% end %>

License

The MIT License (MIT)

Copyright (c) 2015 Devin Joel Austin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.