Sha256: 2fe8cf185fe89605cfd264815ace24c958bfcb7a7f2cdf59be94cdc76e23ffb7

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

class ThinkingSphinx::Search::Query
  DEFAULT_TOKEN = /[\p{Word}\\][\p{Word}\\@]+/

  attr_reader :keywords, :conditions, :star

  def initialize(keywords = '', conditions = {}, star = false)
    @keywords, @conditions, @star = keywords, conditions, star
  end

  def to_s
    (star_keyword(keywords || '') + ' ' + conditions.keys.collect { |key|
       next if conditions[key].blank?

      "@#{key} #{star_keyword conditions[key], key}"
    }.join(' ')).strip
  end

  private

  def star_keyword(keyword, key = nil)
    unless star && (key.nil? || key.to_s != 'sphinx_internal_class_name')
      return keyword.to_s
    end

    token = star.is_a?(Regexp) ? star : DEFAULT_TOKEN
    keyword.gsub(/("#{token}(.*?#{token})?"|(?![!-])#{token})/u) do
      pre, proper, post = $`, $&, $'
      # E.g. "@foo", "/2", "~3", but not as part of a token
      is_operator = pre.match(%r{(\W|^)[@~/]\Z}) ||
                    pre.match(%r{(\W|^)@\([^\)]*$})
      # E.g. "foo bar", with quotes
      is_quote    = proper[/^".*"$/]
      has_star    = post[/\*$/] || pre[/^\*/]
      if is_operator || is_quote || has_star
        proper
      else
        "*#{proper}*"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.4 lib/thinking_sphinx/search/query.rb