Skip to content

Commit

Permalink
Merge pull request #28 from mlibrary/wanted_fix
Browse files Browse the repository at this point in the history
Wanted fix
  • Loading branch information
billdueber authored Mar 1, 2023
2 parents fb0c591 + a6ba9ac commit e9ad422
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/mlibrary_search_parser/node/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,18 @@ def select_type(type)
select { |x| x.is_type?(type.to_sym) }
end

# Get a list of all the tokens in left-to-right order
# @return [Array<String>]
def tokens
select_type(:tokens).map(&:to_s).reject { |str| str.empty? }
end

# Get a string joining all the tokens in the normal
# left-to-right -- essentially, the search without any
# booleans/fields/etc, and skipping any empty strings
# @return [String]
def tokens_string
select_type(:tokens).map(&:to_s).reject { |str| str.empty? }.join(" ")
tokens.join(" ")
end

# The tokens_string, but devoid of double-quotes and then wrapped
Expand All @@ -197,9 +203,22 @@ def tokens_phrase
%("#{tokens_string.delete('"')}")
end

# Get on the "wanted" tokens (those not encased in a "NOT" of some sort)
# @return [Array<String>]
def wanted_tokens
deep_dup.trim_not.tokens
end

# Get only the tokens the user actually wants
# @return [String]
def wanted_tokens_string
deep_dup.trim_not.tokens_string
wanted_tokens.join(" ")
end

# Only the wanted tokens enclosed in double-quotes
# @return [String]
def wanted_tokens_phrase
%("#{wanted_tokens_string.delete('"')}")
end

# Assign each node in this tree an arbitrary number, useful for
Expand Down
4 changes: 4 additions & 0 deletions lib/mlibrary_search_parser/node/fielded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def deep_dup(&blk)
end
end

def trim(&blk)
self.class.new(field, query.deep_dup.trim(&blk))
end

def ==(other)
other.is_type?(node_type) && other.field == field && other.query == query
end
Expand Down
7 changes: 5 additions & 2 deletions lib/mlibrary_search_parser/search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "mlibrary_search_parser/search_handler"
require "delegate"

module MLibrarySearchParser
class SearchBuilder
Expand All @@ -13,10 +14,10 @@ def build(original_input)
end
end

class Search
class Search < SimpleDelegator
attr_reader :original_input, :mini_search, :config, :errors, :warnings
# could come from search box, from adv search form, or from solr output

# could come from search box, from adv search form, or from solr output
def self.from_form(input, search_handler)
end

Expand All @@ -31,6 +32,8 @@ def initialize(original_input, config)
@mini_search = @search_handler.pre_process(MiniSearch.new(original_input))
@errors = Array(@mini_search.errors)
@warnings = Array(@mini_search.warnings)
@search_tree = @search_handler.parse(mini_search.to_s)
__setobj__(@search_tree)
end

def clean_string
Expand Down
5 changes: 5 additions & 0 deletions spec/node/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
expect(neg.wanted_tokens_string).to eq "one three"
end

it "finds wanted tokens in a more complex search" do
s = catalog_search("one AND author:(two NOT three) OR four NOT five")
expect(s.wanted_tokens_string).to eq("one two four")
end

it "produces a tree string" do
expect(@fielded.tree_string).to eq "FIELD: title\n ┝ two"
expect(@complex.tree_string).to eq "AND\n ┝ one\n ┝ two\nOR\n ┝ three\n ┝ four"
Expand Down

0 comments on commit e9ad422

Please sign in to comment.