summaryrefslogtreecommitdiff
path: root/lib/chef_zero/solr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef_zero/solr')
-rw-r--r--lib/chef_zero/solr/query/binary_operator.rb16
-rw-r--r--lib/chef_zero/solr/query/phrase.rb2
-rw-r--r--lib/chef_zero/solr/query/range_query.rb4
-rw-r--r--lib/chef_zero/solr/query/regexpable_query.rb1
-rw-r--r--lib/chef_zero/solr/query/term.rb10
-rw-r--r--lib/chef_zero/solr/query/unary_operator.rb8
-rw-r--r--lib/chef_zero/solr/solr_doc.rb8
-rw-r--r--lib/chef_zero/solr/solr_parser.rb67
8 files changed, 59 insertions, 57 deletions
diff --git a/lib/chef_zero/solr/query/binary_operator.rb b/lib/chef_zero/solr/query/binary_operator.rb
index 6d9b219..e733102 100644
--- a/lib/chef_zero/solr/query/binary_operator.rb
+++ b/lib/chef_zero/solr/query/binary_operator.rb
@@ -18,13 +18,13 @@ module ChefZero
def matches_doc?(doc)
case @operator
- when 'AND'
+ when "AND"
left.matches_doc?(doc) && right.matches_doc?(doc)
- when 'OR'
+ when "OR"
left.matches_doc?(doc) || right.matches_doc?(doc)
- when '^'
+ when "^"
left.matches_doc?(doc)
- when ':'
+ when ":"
if left.respond_to?(:literal_string) && left.literal_string
values = doc[left.literal_string]
else
@@ -36,13 +36,13 @@ module ChefZero
def matches_values?(values)
case @operator
- when 'AND'
+ when "AND"
left.matches_values?(values) && right.matches_values?(values)
- when 'OR'
+ when "OR"
left.matches_values?(values) || right.matches_values?(values)
- when '^'
+ when "^"
left.matches_values?(values)
- when ':'
+ when ":"
raise ": does not work inside a : or term"
end
end
diff --git a/lib/chef_zero/solr/query/phrase.rb b/lib/chef_zero/solr/query/phrase.rb
index f229da9..d345a8e 100644
--- a/lib/chef_zero/solr/query/phrase.rb
+++ b/lib/chef_zero/solr/query/phrase.rb
@@ -1,4 +1,4 @@
-require 'chef_zero/solr/query/regexpable_query'
+require "chef_zero/solr/query/regexpable_query"
module ChefZero
module Solr
diff --git a/lib/chef_zero/solr/query/range_query.rb b/lib/chef_zero/solr/query/range_query.rb
index 625c0bb..76ec828 100644
--- a/lib/chef_zero/solr/query/range_query.rb
+++ b/lib/chef_zero/solr/query/range_query.rb
@@ -15,7 +15,7 @@ module ChefZero
def matches_values?(values)
values.any? do |value|
- unless @from == '*'
+ unless @from == "*"
case @from <=> value
when -1
return false
@@ -23,7 +23,7 @@ module ChefZero
return false if !@from_inclusive
end
end
- unless @to == '*'
+ unless @to == "*"
case value <=> @to
when 1
return false
diff --git a/lib/chef_zero/solr/query/regexpable_query.rb b/lib/chef_zero/solr/query/regexpable_query.rb
index cebc011..62d7fc7 100644
--- a/lib/chef_zero/solr/query/regexpable_query.rb
+++ b/lib/chef_zero/solr/query/regexpable_query.rb
@@ -16,6 +16,7 @@ module ChefZero
def matches_doc?(doc)
matches_values?(doc[DEFAULT_FIELD])
end
+
def matches_values?(values)
values.any? { |value| !@regexp.match(value).nil? }
end
diff --git a/lib/chef_zero/solr/query/term.rb b/lib/chef_zero/solr/query/term.rb
index 23f4a72..e106519 100644
--- a/lib/chef_zero/solr/query/term.rb
+++ b/lib/chef_zero/solr/query/term.rb
@@ -1,4 +1,4 @@
-require 'chef_zero/solr/query/regexpable_query'
+require "chef_zero/solr/query/regexpable_query"
module ChefZero
module Solr
@@ -11,19 +11,19 @@ module ChefZero
regexp_string = ""
index = 0
while index < term.length
- if term[index] == '*'
+ if term[index] == "*"
regexp_string << "#{WORD_CHARACTER}*"
literal_string = nil
index += 1
- elsif term[index] == '?'
+ elsif term[index] == "?"
regexp_string << WORD_CHARACTER
literal_string = nil
index += 1
- elsif term[index] == '~'
+ elsif term[index] == "~"
raise "~ unsupported"
else
if term[index] == '\\'
- index = index+1
+ index = index + 1
if index >= term.length
raise "Backslash at end of string '#{term}'"
end
diff --git a/lib/chef_zero/solr/query/unary_operator.rb b/lib/chef_zero/solr/query/unary_operator.rb
index a873932..e83683c 100644
--- a/lib/chef_zero/solr/query/unary_operator.rb
+++ b/lib/chef_zero/solr/query/unary_operator.rb
@@ -16,9 +16,9 @@ module ChefZero
def matches_doc?(doc)
case @operator
- when '-', 'NOT'
+ when "-", "NOT"
!operand.matches_doc?(doc)
- when '+'
+ when "+"
# TODO This operator uses relevance to eliminate other, unrelated
# expressions. +a OR b means "if it has b but not a, don't return it"
raise "+ not supported yet, because it is hard."
@@ -27,9 +27,9 @@ module ChefZero
def matches_values?(values)
case @operator
- when '-', 'NOT'
+ when "-", "NOT"
!operand.matches_values?(values)
- when '+'
+ when "+"
# TODO This operator uses relevance to eliminate other, unrelated
# expressions. +a OR b means "if it has b but not a, don't return it"
raise "+ not supported yet, because it is hard."
diff --git a/lib/chef_zero/solr/solr_doc.rb b/lib/chef_zero/solr/solr_doc.rb
index 6476d48..b25ea15 100644
--- a/lib/chef_zero/solr/solr_doc.rb
+++ b/lib/chef_zero/solr/solr_doc.rb
@@ -15,12 +15,12 @@ module ChefZero
def matching_values(&block)
result = []
key_values(nil, @json) do |key, value|
- if block.call(key)
+ if yield(key)
result << value.to_s
end
end
# Handle manufactured value(s)
- if block.call('X_CHEF_id_CHEF_X')
+ if yield("X_CHEF_id_CHEF_X")
result << @id.to_s
end
@@ -32,7 +32,7 @@ module ChefZero
def key_values(key_so_far, value, &block)
if value.is_a?(Hash)
value.each_pair do |child_key, child_value|
- block.call(child_key, child_value.to_s)
+ yield(child_key, child_value.to_s)
if key_so_far
new_key = "#{key_so_far}_#{child_key}"
key_values(new_key, child_value, &block)
@@ -45,7 +45,7 @@ module ChefZero
key_values(key_so_far, child_value, &block)
end
else
- block.call(key_so_far || 'text', value.to_s)
+ yield(key_so_far || "text", value.to_s)
end
end
end
diff --git a/lib/chef_zero/solr/solr_parser.rb b/lib/chef_zero/solr/solr_parser.rb
index 99da5fe..2bf2125 100644
--- a/lib/chef_zero/solr/solr_parser.rb
+++ b/lib/chef_zero/solr/solr_parser.rb
@@ -1,9 +1,9 @@
-require 'chef_zero/solr/query/binary_operator'
-require 'chef_zero/solr/query/unary_operator'
-require 'chef_zero/solr/query/term'
-require 'chef_zero/solr/query/phrase'
-require 'chef_zero/solr/query/range_query'
-require 'chef_zero/solr/query/subquery'
+require "chef_zero/solr/query/binary_operator"
+require "chef_zero/solr/query/unary_operator"
+require "chef_zero/solr/query/term"
+require "chef_zero/solr/query/phrase"
+require "chef_zero/solr/query/range_query"
+require "chef_zero/solr/query/subquery"
module ChefZero
module Solr
@@ -40,19 +40,20 @@ module ChefZero
# Operators
operator = peek_operator_token
if operator
- @index+=operator.length
+ @index += operator.length
operator
else
# Everything that isn't whitespace or an operator, is part of a term
# (characters plus backslashed escaped characters)
start_index = @index
- begin
+ loop do
if @query_string[@index] == '\\'
- @index+=1
+ @index += 1
end
- @index+=1 if !eof?
- end while !eof? && peek_term_token
- @query_string[start_index..@index-1]
+ @index += 1 if !eof?
+ break if eof? || !peek_term_token
+ end
+ @query_string[start_index..@index - 1]
end
end
@@ -66,15 +67,15 @@ module ChefZero
def peek_term_token
return nil if @query_string[@index] =~ /\s/
op = peek_operator_token
- return !op || op == '-'
+ return !op || op == "-"
end
def peek_operator_token
- if ['"', '+', '-', '!', '(', ')', '{', '}', '[', ']', '^', ':'].include?(@query_string[@index])
+ if ['"', "+", "-", "!", "(", ")", "{", "}", "[", "]", "^", ":"].include?(@query_string[@index])
return @query_string[@index]
else
- result = @query_string[@index..@index+1]
- if ['&&', '||'].include?(result)
+ result = @query_string[@index..@index + 1]
+ if ["&&", "||"].include?(result)
return result
end
end
@@ -91,19 +92,19 @@ module ChefZero
# Expression is over when we hit a close paren or eof
# (peek_token has the side effect of skipping whitespace for us, so we
# really know if we're at eof or not)
- until peek_token == ')' || eof?
+ until peek_token == ")" || eof?
operator = peek_token
if binary_operator?(operator)
next_token
else
# If 2 terms are next to each other, the default operator is OR
- operator = 'OR'
+ operator = "OR"
end
next_expression = read_single_expression
# Build the operator, taking precedence into account
if result.is_a?(Query::BinaryOperator) &&
- binary_operator_precedence(operator) > binary_operator_precedence(result.operator)
+ binary_operator_precedence(operator) > binary_operator_precedence(result.operator)
# a+b*c -> a+(b*c)
new_right = Query::BinaryOperator.new(result.right, operator, next_expression)
result = Query::BinaryOperator.new(result.left, result.operator, new_right)
@@ -142,7 +143,7 @@ module ChefZero
Query::Phrase.new(phrase_terms)
# If it's the start of a range query, build that
- elsif token == '{' || token == '['
+ elsif token == "{" || token == "["
left = next_token
parse_error(left, "Expected left term in range query") if !left
to = next_token
@@ -150,17 +151,17 @@ module ChefZero
right = next_token
parse_error(right, "Expected left term in range query") if !right
end_range = next_token
- parse_error(right, "Expected end range '#{end_range}") if !['}', ']'].include?(end_range)
- Query::RangeQuery.new(left, right, token == '[', end_range == ']')
+ parse_error(right, "Expected end range '#{end_range}") if !["}", "]"].include?(end_range)
+ Query::RangeQuery.new(left, right, token == "[", end_range == "]")
- elsif token == '('
+ elsif token == "("
subquery = read_expression
close_paren = next_token
- parse_error(close_paren, "Expected ')'") if close_paren != ')'
+ parse_error(close_paren, "Expected ')'") if close_paren != ")"
Query::Subquery.new(subquery)
# If it's the end of a closure, raise an exception
- elsif ['}',']',')'].include?(token)
+ elsif ["}", "]", ")"].include?(token)
parse_error(token, "Unexpected end paren")
# If it's a binary operator, raise an exception
@@ -170,7 +171,7 @@ module ChefZero
# Otherwise it's a term.
else
term = Query::Term.new(token)
- if peek_token == ':'
+ if peek_token == ":"
Query::BinaryOperator.new(term, next_token, read_single_expression)
else
term
@@ -179,27 +180,27 @@ module ChefZero
end
def unary_operator?(token)
- [ 'NOT', '+', '-' ].include?(token)
+ [ "NOT", "+", "-" ].include?(token)
end
def binary_operator?(token)
- [ 'AND', 'OR', '^', ':'].include?(token)
+ [ "AND", "OR", "^", ":"].include?(token)
end
def binary_operator_precedence(token)
case token
- when '^'
+ when "^"
4
- when ':'
+ when ":"
3
- when 'AND'
+ when "AND"
2
- when 'OR'
+ when "OR"
1
end
end
- DEFAULT_FIELD = 'text'
+ DEFAULT_FIELD = "text"
end
end
end