Class: Appydave::Tools::Jump::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/jump/search.rb

Overview

Search provides fuzzy search across location metadata

Scoring algorithm based on match type:

  • Exact key match: 100 points

  • Key contains term: 50 points

  • Brand/client alias match: 40 points

  • Tag match: 30 points

  • Type match: 20 points

  • Description contains: 10 points

  • Path contains: 5 points

Examples:

Basic search

search = Search.new(config)
results = search.search('appydave ruby')
results[:success]  # => true
results[:results]  # => Array of scored location hashes

Constant Summary collapse

SCORE_EXACT_KEY =
100
SCORE_KEY_CONTAINS =
50
SCORE_BRAND_CLIENT_ALIAS =
40
SCORE_TAG_MATCH =
30
SCORE_TYPE_MATCH =
20
SCORE_DESCRIPTION_CONTAINS =
10
SCORE_PATH_CONTAINS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Search



33
34
35
# File 'lib/appydave/tools/jump/search.rb', line 33

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/appydave/tools/jump/search.rb', line 31

def config
  @config
end

Instance Method Details

#get(key) ⇒ Hash

Get a location by exact key



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/appydave/tools/jump/search.rb', line 70

def get(key)
  location = config.find(key)

  if location
    result = location_to_result(location, SCORE_EXACT_KEY)
    result[:index] = 1
    {
      success: true,
      count: 1,
      results: [result]
    }
  else
    suggestions = find_suggestions(key)
    {
      success: false,
      error: 'Location not found',
      code: 'NOT_FOUND',
      suggestion: suggestions.empty? ? nil : "Did you mean: #{suggestions.join(', ')}?"
    }
  end
end

#listHash

List all locations



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/appydave/tools/jump/search.rb', line 95

def list
  results = config.locations.map.with_index(1) do |location, index|
    result = location_to_result(location, 0)
    result[:index] = index
    result
  end

  {
    success: true,
    count: results.size,
    results: results
  }
end

#search(query) ⇒ Hash

Search for locations matching query terms



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/appydave/tools/jump/search.rb', line 41

def search(query)
  terms = parse_query(query)

  return empty_result if terms.empty?

  scored_locations = config.locations.map do |location|
    score = calculate_score(location, terms)
    next if score.zero?

    location_to_result(location, score)
  end.compact

  # Sort by score descending, then by key alphabetically
  sorted = scored_locations.sort_by { |r| [-r[:score], r[:key]] }

  # Add index numbers
  sorted.each_with_index { |result, i| result[:index] = i + 1 }

  {
    success: true,
    count: sorted.size,
    results: sorted
  }
end