Sha256: 26d7eaecb6b40f1465126d6117df838f5cfff8f9c59ba87edc47b6499e20b022

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

class ThinkingSphinx::Search::Query
  if Regexp.instance_methods.include?(:encoding)
    DefaultToken = Regexp.new('\p{Word}+')
  else
    DefaultToken = Regexp.new('\w+', nil, 'u')
  end

  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|
      "@#{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')
      return keyword.to_s
    end

    token = star.is_a?(Regexp) ? star : DefaultToken
    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

4 entries across 4 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.1 lib/thinking_sphinx/search/query.rb
thinking-sphinx-3.0.0 lib/thinking_sphinx/search/query.rb
thinking-sphinx-3.0.0.rc lib/thinking_sphinx/search/query.rb
thinking-sphinx-3.0.0.pre lib/thinking_sphinx/search/query.rb