Sha256: cbe97b87bed840bbc71d7be34f988d661158b0a6bc17fdfe1d355244607bdf06

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

class Query
  class << self
    # Returns a Ferret::Query from a raw String query.
    def extract_from(raw_query)
      parser.parse(convert_to_english(raw_query))
    end

    private

    # Converts query keywords to english so they can be parsed by Ferret.
    def convert_to_english(raw_query)
      to_en={
       /\b#{:AND.l}\b/=>'AND',
       /\b#{:OR.l}\b/=>'OR',
       /\b#{:NOT.l}\b/=>'NOT',
       /(#{:filename.l}):/=>'filename:',
       /(#{:filetype.l}):/=>'filetype:',
       /#{:content.l}:/ => 'content:',
       /(#{:modified.l}):/ => 'modified:',
       /(#{:language.l}):/ => 'language:',
       /\b#{:LIKE.l}\s+(\S+)/=>'\1~'
      }
      to_en.inject(raw_query){|mem,non_english_to_english_keyword|
        mem.gsub(*non_english_to_english_keyword)
      }
    end

    # Instantiates a QueryParser once, and keeps it in cache.
    def parser
      @@parser ||= Ferret::QueryParser.new(:fields => [:content, :filename, :basename, :filetype, :modified], :or_default => false, :analyzer=>Picolena::Analyzer)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
picolena-0.1.6 lib/picolena/templates/app/models/query.rb
picolena-0.1.7 lib/picolena/templates/app/models/query.rb