full_search 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5343bec22958a4f873a86fa226a66e925606f1eb01743e70993b6f2a4d598786
4
- data.tar.gz: 3cb1d8b6d8cc8577901c4f6eb79e3340d5899da4ae1bb378ae752e4c89ed7d28
3
+ metadata.gz: 90ca9a454b8cacf2619c28ced3b07420fbf26ab34ddb55b79f93ce82ce69c06e
4
+ data.tar.gz: e37851d2e693989a7585fe40460af0bab1ae91c9b171faca76114394b3c97799
5
5
  SHA512:
6
- metadata.gz: 792eee7f1854aeac5b038f8e9496bf353637ffa85e72117e69ef6ae085c14f6849e43b7fd9c3c9edec5457ac94eaadc1bf6ea6b121294474d43ed135617d70b1
7
- data.tar.gz: 3221491574ddbeca5e5299fc38b2959fb31b679c89244bdaaba68ffba8da2a06f3419d61cb857f754bb66a0a55dc80770a816e35ed396e46c3b7a53834f85b5a
6
+ metadata.gz: 76f1883ee4d70669bd85bfac929b2dfaf820efb40400cfaabe98fd0d7dc68ad867e551cb0cb7d292f51694e74fa0349080497adb6a6dd52154b1e80b259540e1
7
+ data.tar.gz: f5e1fa063bcf8ff247ff52506f98fcacd98d47de827601a48d2070f8da58d380dc72d628089d69f63a89bc0a88dacec123c68566ec82e502defeaa76a5546958
@@ -4,12 +4,18 @@ module FullSearch
4
4
  class Highlighter
5
5
  def self.apply!(records, model, query)
6
6
  snippets = build_snippets(model, query)
7
+ if snippets.values.all?(&:nil?) && records.any?
8
+ snippets = manual_snippets(records, model, query)
9
+ end
7
10
  records.each { |record| record.full_search_snippet = snippets[record.id] }
8
11
  records
9
12
  end
10
13
 
11
14
  def self.apply_fields!(records, model, query)
12
15
  fields = build_field_snippets(model, query)
16
+ if fields.empty? && records.any?
17
+ fields = manual_field_snippets(records, model, query)
18
+ end
13
19
  records.each { |record| record.full_search_highlight_fields = fields[record.id] || {} }
14
20
  records
15
21
  end
@@ -41,6 +47,38 @@ module FullSearch
41
47
  end
42
48
  end
43
49
 
50
+ def self.manual_snippets(records, model, query)
51
+ dsl = model.full_search_dsl
52
+ config = dsl.highlight_config || { open_tag: "<mark>", close_tag: "</mark>" }
53
+ cols = dsl.fields.map(&:name)
54
+
55
+ records.to_h do |record|
56
+ text = cols.map { |col| record.public_send(col).to_s }.join(" ").strip
57
+ highlighted = manual_highlight(text, query, config)
58
+ [record.id, highlighted.presence]
59
+ end
60
+ end
61
+
62
+ def self.manual_field_snippets(records, model, query)
63
+ dsl = model.full_search_dsl
64
+ config = dsl.highlight_config || { open_tag: "<mark>", close_tag: "</mark>" }
65
+ cols = dsl.fields.map(&:name)
66
+
67
+ records.to_h do |record|
68
+ snippets = cols.each_with_object({}) do |col, hash|
69
+ value = record.public_send(col).to_s
70
+ highlighted = manual_highlight(value, query, config)
71
+ hash[col.to_s] = highlighted if highlighted.include?(config[:open_tag])
72
+ end
73
+ [record.id, snippets]
74
+ end
75
+ end
76
+
77
+ def self.manual_highlight(text, query, config)
78
+ return text if text.empty? || query.empty?
79
+ text.gsub(/#{Regexp.escape(query)}/i, "#{config[:open_tag]}\\0#{config[:close_tag]}")
80
+ end
81
+
44
82
  def self.highlight_rows(model, query)
45
83
  dsl = model.full_search_dsl
46
84
  config = dsl.highlight_config || { open_tag: "<mark>", close_tag: "</mark>" }
@@ -82,7 +82,11 @@ module FullSearch
82
82
  return [] if match_expr.empty?
83
83
 
84
84
  term = parsed.last rescue nil
85
- return [] if term.nil? || term.length < dsl.typo_tolerance_min_term_length.to_i
85
+ return [] if term.nil?
86
+
87
+ if term.length < dsl.typo_tolerance_min_term_length.to_i
88
+ return like_prefix_ids(term)
89
+ end
86
90
 
87
91
  trigram_table = FullSearch::Index.trigram_table_name(model)
88
92
 
@@ -100,6 +104,27 @@ module FullSearch
100
104
  connection.execute("#{sql} #{filter_conditions}").map { |r| r["id"] }
101
105
  end
102
106
 
107
+ def like_prefix_ids(term)
108
+ column_fields = dsl.fields.select { |f| f.source.nil? }
109
+ return [] if column_fields.empty?
110
+
111
+ like_conditions = column_fields.map do |field|
112
+ "#{connection.quote_table_name(model.table_name)}.#{connection.quote_column_name(field.name)} LIKE #{connection.quote("#{term}%")}"
113
+ end.join(" OR ")
114
+
115
+ sql = <<~SQL
116
+ SELECT #{model.table_name}.id
117
+ FROM #{model.table_name}
118
+ WHERE (#{like_conditions})
119
+ SQL
120
+
121
+ filter_conditions = filters.map do |name, value|
122
+ "AND #{model.table_name}.#{name} = #{connection.quote(value)}"
123
+ end.join(" ")
124
+
125
+ connection.execute("#{sql} #{filter_conditions}").map { |r| r["id"] }
126
+ end
127
+
103
128
  def apply_ranking(rel, all_ids, exact_ids)
104
129
  return rel if all_ids.empty?
105
130
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FullSearch
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: full_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben D'Angelo