0.0
No commit activity in last 3 years
No release in over 3 years
There's a lot of open issues
Highlight search terms in a result set and distill the result text down to the pertinent parts
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 5.5.1
= 10.4.2

Runtime

 Project Readme

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.