Class: Awesome::Search

Inherits:
Object
  • Object
show all
Includes:
Definitions::Bits, Definitions::Filters, Definitions::Stopwords, Definitions::Types
Defined in:
lib/awesome/search.rb

Direct Known Subclasses

SuperSearch

Constant Summary

Constants included from Definitions::Stopwords

Definitions::Stopwords::BEG_OPERATORS, Definitions::Stopwords::END_OPERATORS, Definitions::Stopwords::EXCLUSION_OPERATORS, Definitions::Stopwords::INCLUSION_OPERATORS, Definitions::Stopwords::QUOTED_REGEX, Definitions::Stopwords::RM_QUOTED_REGEX, Definitions::Stopwords::UNQUOTED_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Definitions::Stopwords

#array_with_stopwords, #clean_token, #gowords_array, #highlight_token_array, included, #process_stopwords, #query_wo_exact_phrases, #quoted_exact_phrases_array, #remove_exclusions, #search_token_array, #set_clean_search_query, #tokenize_quot, #tokenize_unquot, #tokenize_without_quot, #unquoted_exact_phrases_array

Methods included from Definitions::Filters

included

Methods included from Definitions::Types

included, #valid_search_type_inception?

Methods included from Definitions::Bits

included

Constructor Details

#initialize(*args) ⇒ Search

CLASS METHODS Main focus of class methods is determining which sort of AwesomeSearch subclass we need to instantiate for the search



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/awesome/search.rb', line 71

def initialize(*args)
  @multiple_types_as_one = args.first[:multiple_types_as_one]
  @stopwords =  !args.first[:stopwords].blank? ?
                  args.first[:stopwords].is_a?(Array) ?
                    args.first[:stopwords] :
                    args.first[:stopwords].is_a?(Symbol) ?
                      self.class.stopwords(args.first[:stopwords]) :
                      self.class.stopwords(:both) :
                  args.first[:stopwords] == true ?
                    self.class.stopwords(:standard) :
                    self.class.stopwords(:none)
  @page     = args.first[:page]
  @per_page = args.first[:per_page]
  @search_text =    args.first[:search_text]    # a string
  @search_filters = args.first[:search_filters] # a ruby object (string, array, hash) to be used by subclasss search classes as a filter
  #Chicken-egg problem: search_tokens and highlight_tokens both need to be set within clean_search_text (and they are!) because they must work with the text cleaned by the clean_search_text methods
  @search_tokens =  args.first[:search_tokens]  # an array of the query terms as tokens after being cleaned, unless passed in as param (not sure why this would ever be desired, but why not allow it jic?) When not set in args, will be set by clean_search_text methods
  @highlight_tokens =  args.first[:highlight_tokens]  # an array of the query terms as unquoted tokens after being cleaned, unless passed in as param (not sure why this would ever be desired, but why not allow it jic?) When not set in args, will be set by clean_search_text methods
  @search_query =   self.clean_search_text      # a string to be set based on the search text by removing the search modifiers from the search text
  @clean_search_query = self.set_clean_search_query # Removed search tokens and modifiers (+) so Regexes can be run on this string
  @search_type =    args.first[:search_type]    # a symring (symring methods are in the Bits mixin)
  @search_locale =  args.first[:search_locale]  # a symring (symring methods are in the Bits mixin)
  @found = nil
  @tally = nil
  @success = true
  @redirect_url = nil
  @replacement_for = nil # stores the name of, or explanatory text about, the primary search if this search is a secondary (replacement) search for a primary search that returned no results
  if self.class.check_inception && !self.valid_search_type_inception?
    puts "search type inception is invalid (no type regex matches query string)" if Awesome::Search.verbose
    @invalid_inception = true
  end
end

Instance Attribute Details

#clean_search_queryObject

Returns the value of attribute clean_search_query.



39
40
41
# File 'lib/awesome/search.rb', line 39

def clean_search_query
  @clean_search_query
end

#endpointObject

Returns the value of attribute endpoint.



39
40
41
# File 'lib/awesome/search.rb', line 39

def endpoint
  @endpoint
end

#foundObject

Returns the value of attribute found.



39
40
41
# File 'lib/awesome/search.rb', line 39

def found
  @found
end

#gowordsObject

Returns the value of attribute gowords.



39
40
41
# File 'lib/awesome/search.rb', line 39

def gowords
  @gowords
end

#highlight_tokensObject

Returns the value of attribute highlight_tokens.



39
40
41
# File 'lib/awesome/search.rb', line 39

def highlight_tokens
  @highlight_tokens
end

#invalid_inceptionObject

Returns the value of attribute invalid_inception.



39
40
41
# File 'lib/awesome/search.rb', line 39

def invalid_inception
  @invalid_inception
end

#multiple_types_as_oneObject

Returns the value of attribute multiple_types_as_one.



39
40
41
# File 'lib/awesome/search.rb', line 39

def multiple_types_as_one
  @multiple_types_as_one
end

#pageObject

Returns the value of attribute page.



39
40
41
# File 'lib/awesome/search.rb', line 39

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.



39
40
41
# File 'lib/awesome/search.rb', line 39

def per_page
  @per_page
end

#query_without_exact_phrasesObject

Returns the value of attribute query_without_exact_phrases.



39
40
41
# File 'lib/awesome/search.rb', line 39

def query_without_exact_phrases
  @query_without_exact_phrases
end

#quoted_exact_phrasesObject

Returns the value of attribute quoted_exact_phrases.



39
40
41
# File 'lib/awesome/search.rb', line 39

def quoted_exact_phrases
  @quoted_exact_phrases
end

#redirect_urlObject

Returns the value of attribute redirect_url.



39
40
41
# File 'lib/awesome/search.rb', line 39

def redirect_url
  @redirect_url
end

#replacement_forObject

Returns the value of attribute replacement_for.



39
40
41
# File 'lib/awesome/search.rb', line 39

def replacement_for
  @replacement_for
end

#search_filtersObject

Returns the value of attribute search_filters.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_filters
  @search_filters
end

#search_localeObject

Returns the value of attribute search_locale.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_locale
  @search_locale
end

#search_queryObject Also known as: processed_query

Returns the value of attribute search_query.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_query
  @search_query
end

#search_textObject Also known as: query

Returns the value of attribute search_text.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_text
  @search_text
end

#search_tokensObject

Returns the value of attribute search_tokens.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_tokens
  @search_tokens
end

#search_typeObject

Returns the value of attribute search_type.



39
40
41
# File 'lib/awesome/search.rb', line 39

def search_type
  @search_type
end

#stopwordsObject

Returns the value of attribute stopwords.



39
40
41
# File 'lib/awesome/search.rb', line 39

def stopwords
  @stopwords
end

#successObject

Returns the value of attribute success.



39
40
41
# File 'lib/awesome/search.rb', line 39

def success
  @success
end

#tallyObject

Returns the value of attribute tally.



39
40
41
# File 'lib/awesome/search.rb', line 39

def tally
  @tally
end

#tokenize_quotedObject

Returns the value of attribute tokenize_quoted.



39
40
41
# File 'lib/awesome/search.rb', line 39

def tokenize_quoted
  @tokenize_quoted
end

#tokenize_unquotedObject

Returns the value of attribute tokenize_unquoted.



39
40
41
# File 'lib/awesome/search.rb', line 39

def tokenize_unquoted
  @tokenize_unquoted
end

#tokenize_without_quotedObject

Returns the value of attribute tokenize_without_quoted.



39
40
41
# File 'lib/awesome/search.rb', line 39

def tokenize_without_quoted
  @tokenize_without_quoted
end

#unquoted_exact_phrasesObject

Returns the value of attribute unquoted_exact_phrases.



39
40
41
# File 'lib/awesome/search.rb', line 39

def unquoted_exact_phrases
  @unquoted_exact_phrases
end

Class Method Details

.clean_search_text(text, multiple_types_as_one = false) ⇒ Object



123
124
125
126
# File 'lib/awesome/search.rb', line 123

def self.clean_search_text(text, multiple_types_as_one = false)
  txt = text.gsub(self.search_type_modifiers_regex(true, multiple_types_as_one), "")
  txt.gsub(self.search_filter_modifiers_regex(true), "")
end

.configure_search_filters {|@@search_filters| ... } ⇒ Object

Yields:



25
26
27
# File 'lib/awesome/search.rb', line 25

def self.configure_search_filters(&block)
  yield @@search_filters
end

.configure_search_stopwords {|@@search_stopwords| ... } ⇒ Object

Yields:

  • (@@search_stopwords)


29
30
31
# File 'lib/awesome/search.rb', line 29

def self.configure_search_stopwords(&block)
  yield @@search_stopwords
end

.configure_search_types {|@@search_types| ... } ⇒ Object

Yields:

  • (@@search_types)


21
22
23
# File 'lib/awesome/search.rb', line 21

def self.configure_search_types(&block)
  yield @@search_types
end

.results_for(anytext, types, locales, filters = nil, multiple_types_as_one = false, page = nil, per_page = nil) ⇒ Object

Main method used by the app to search Instantiates an Awesome::Triage which will handle setting up the search



130
131
132
133
134
135
# File 'lib/awesome/search.rb', line 130

def self.results_for(anytext, types, locales, filters = nil, multiple_types_as_one = false, page = nil, per_page = nil)
  # 1. if the search is blank return nil
  puts "anytext is blank" if Awesome::Search.verbose && anytext.blank?
  return nil if anytext.blank?
  Triage.new(:text => anytext, :types => types, :locales => locales, :filters => filters, :multiple_types_as_one => multiple_types_as_one, :page => page, :per_page => per_page)
end

Instance Method Details

#clean_search_textObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/awesome/search.rb', line 112

def clean_search_text
  #puts "search_text: #{self.search_text}" if Awesome::Search.verbose
  txt = Awesome::Triage.clean_search_text(self.search_text)
  #puts "cleaning1: #{txt}" if Awesome::Search.verbose
  txt = Awesome::Search.clean_search_text(txt, self.multiple_types_as_one)
  #puts "cleaning2: #{txt}" if Awesome::Search.verbose
  txt = self.process_stopwords(txt)
  #puts "cleaning3: #{txt}" if Awesome::Search.verbose
  txt
end

#get_resultsObject

get_results is called on the instances of the subclasses. The subclasses override the methods called within it to return their customized stuff The subclasses call super to ensure that the data is valid before sending out the search.



142
143
144
145
# File 'lib/awesome/search.rb', line 142

def get_results
  # 1. if the search is blank do NOT run the search (handled in subclasses)
  !self.search_text.blank? && !self.search_query.blank? && !self.search_type.blank? && !self.search_locale.blank?
end

#to_sObject



104
105
106
107
108
109
110
# File 'lib/awesome/search.rb', line 104

def to_s
  "search_query: #{self.search_query} \
\n\rsearch_tokens: #{self.search_tokens.inspect} \
\n\rhighlight_tokens: #{self.highlight_tokens.inspect} \
\n\rtally: #{self.tally} \
\n\rsuccess? #{self.success ? 'Yes' : 'No'}"
end