Class: Solr::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/solr/util.rb

Overview

The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Constant Summary collapse

ESCAPES =
%w[+ - && || ! ( ) { } \[ \] " ~ * ? \\]

Class Method Summary collapse

Class Method Details

.paired_array_each(a, &block) ⇒ Object

paired_array_each() yields twice:

|key1,value1|  and |key2,value2|

16
17
18
19
20
21
# File 'lib/solr/util.rb', line 16

def self.paired_array_each(a, &block)
  0.upto(a.size / 2 - 1) do |i|
    n = i * 2
    yield(a[n], a[n+1])
  end
end

.paired_array_to_hash(a) ⇒ Object

paired_array_to_hash() => => value1, key2, value2


24
25
26
# File 'lib/solr/util.rb', line 24

def self.paired_array_to_hash(a)
  Hash[*a]
end

.query_parser_escape(string, fields = []) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/solr/util.rb', line 31

def self.query_parser_escape(string, fields = [])
  string = string.split(' ').map do |str| 
    if /(\w+):(.*)/ =~ str # escape : considering fields
      (!$2.empty? and fields.include?($1.to_sym)) ? "#{$1}:#{$2}" : "#{$1}\\:#{$2}"
    elsif /^\^(.*)/ =~ str # escape ^
      "\\^#{$1}"
    else
      str
    end
  end.join(' ')

  ESCAPES.each { |e| string.gsub! e, "\\#{e}" }
  string
end