full_search 0.3.4 → 0.3.5

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: ed7b0998cc6d4014ab815a6152ba07562da34c98b3cca3aa3886e63fb45acc5b
4
- data.tar.gz: f29430f7ac4d5a3175e1eb46c803aa3a9b9ce68e3e930f2f75ecfd8ad03242d7
3
+ metadata.gz: b3b466311e2305f90987a48ff58cee59f4c81af6e960b5fcd2fe09cf845b985b
4
+ data.tar.gz: 1d355e22aa06d280d984d7da1df90e57a8ca4a5f5676b7870424f2b048c051ba
5
5
  SHA512:
6
- metadata.gz: cce0189e77ec25883ad52dae321dea4f99cb8f1f245f331b5cdeb94f6c3554c675e84b0214e4036ca1c01fc53f22e41594324729d39688c8e48d0bcd791356f3
7
- data.tar.gz: a9d3f31295d082892dc5cf797f2d38a9cb8151629f9e6d2c0ac0ad1b8dcfeb5e732b740fb91d138d0b3a7f1db513c72c36aa61a1410d2b052bd57bda0790c5dd
6
+ metadata.gz: 9489ff8c9bf04c59b6380cd1810871a39a947421754379ac6fb1c1cb36cbf7850a753d0a7354e25f38b591aeae63cbb05cd7d8c0f187bb84ba6ef191aba19755
7
+ data.tar.gz: a23ce1270f3ac088b02774a43178fb277811e7df65a7d518f3fde4b8899cfb2db3f07164c72ea75ec26bb3b8ed9ef29b94d05aa53009ef1f82dd9d1bb4d14f79
data/README.md CHANGED
@@ -45,6 +45,27 @@ end
45
45
  Customer.search("sam", filters: { account_id: 1 }).page(params[:page])
46
46
  ```
47
47
 
48
+ ### Field aliasing with `as:`
49
+
50
+ Use `as:` to give a field a different column name in the FTS table. This is useful when
51
+ the model's accessor name differs from the desired search index key — for example, storing
52
+ a computed value (last 8 chars of VIN) in a shorter or semantically-named column:
53
+
54
+ ```ruby
55
+ class Vehicle < ApplicationRecord
56
+ full_search do
57
+ field :vin_last8, weight: 2, source: -> { vin&.last(8)&.upcase }, as: :vin
58
+ field :make, weight: 4
59
+ field :model, weight: 4
60
+ filter :account_id, required: true
61
+ highlight
62
+ end
63
+ end
64
+ ```
65
+
66
+ The FTS table stores the value under `vin` (not `vin_last8`). Search results highlight
67
+ keyed as `vin`, and the `full_search_text_for(:vin)` lookup works as expected.
68
+
48
69
  ## Features
49
70
 
50
71
  - Declarative `full_search` DSL with `search` / `full_search` query methods
@@ -66,7 +66,7 @@ module FullSearch
66
66
  table = qt(FullSearch::Index.fts_table_name(record.class))
67
67
  conn = ActiveRecord::Base.connection
68
68
  conn.execute(
69
- "UPDATE #{table} SET #{qc(field.name)} = #{q(value.to_s)} WHERE rowid = #{q(record.id)}"
69
+ "UPDATE #{table} SET #{qc(field.as || field.name)} = #{q(value.to_s)} WHERE rowid = #{q(record.id)}"
70
70
  )
71
71
  rescue => e
72
72
  raise unless e.message.include?("no such table")
@@ -3,8 +3,8 @@
3
3
  module FullSearch
4
4
  class Config
5
5
  attr_accessor :auto_rebuild_schema, :stale_query_behavior, :lock_rebuilds,
6
- :default_async_reindex, :default_async_source_reindex,
7
- :default_tokenizer, :auto_rebuild_on_stale_query
6
+ :default_async_reindex, :default_async_source_reindex,
7
+ :default_tokenizer, :auto_rebuild_on_stale_query
8
8
 
9
9
  def initialize
10
10
  @auto_rebuild_schema = false
@@ -20,9 +20,9 @@ module FullSearch
20
20
  end
21
21
 
22
22
  def field(name, weight: 1, source: nil, reindex_on: nil,
23
- async: FullSearch.config.default_async_reindex,
24
- async_source: FullSearch.config.default_async_source_reindex,
25
- as: nil, version: nil)
23
+ async: FullSearch.config.default_async_reindex,
24
+ async_source: FullSearch.config.default_async_source_reindex,
25
+ as: nil, version: nil)
26
26
  unless valid_name?(name)
27
27
  raise InvalidFieldError, "#{model_class.name}: invalid field name #{name.inspect}"
28
28
  end
@@ -46,9 +46,9 @@ module FullSearch
46
46
  conn.execute(backfill_sql(model)) if fts_was_created
47
47
  conn.execute(backfill_trigram_sql(model)) if trigram_was_created
48
48
  reindex_source_fields!(model) if dsl.fields.any?(&:source)
49
+ store_config_hash!(model)
49
50
  end
50
51
 
51
- store_config_hash!(model)
52
52
  ensure_triggers!(model) if model_table_exists?(model)
53
53
 
54
54
  verified_tables.add(model.table_name)
@@ -188,6 +188,10 @@ module FullSearch
188
188
  connection.quote_table_name(name)
189
189
  end
190
190
 
191
+ def column_name(col)
192
+ (col.respond_to?(:as) && col.as) ? col.as : col.name
193
+ end
194
+
191
195
  def qc(name)
192
196
  connection.quote_column_name(name)
193
197
  end
@@ -223,7 +227,7 @@ module FullSearch
223
227
  def create_virtual_table_sql(model)
224
228
  dsl = model.full_search_dsl
225
229
  columns = (dsl.fields + dsl.filters.map { |f| FilterColumnPlaceholder.new(name: f.name) } + extra_columns(model))
226
- column_list = columns.map { |c| (c.respond_to?(:unindexed?) && c.unindexed?) ? "#{qc(c.name)} UNINDEXED" : qc(c.name) }.join(", ")
230
+ column_list = columns.map { |c| (c.respond_to?(:unindexed?) && c.unindexed?) ? "#{qc(column_name(c))} UNINDEXED" : qc(column_name(c)) }.join(", ")
227
231
 
228
232
  "CREATE VIRTUAL TABLE #{qt(fts_table_name(model))} USING fts5(#{column_list}, tokenize='#{dsl.tokenize}');"
229
233
  end
@@ -244,7 +248,7 @@ module FullSearch
244
248
  where_clause = dsl.conditional_index? ? " WHERE (#{dsl.index_if_sql})" : ""
245
249
 
246
250
  <<~SQL
247
- INSERT INTO #{qt(fts_table_name(model))}(rowid, #{cols.map { |c| qc(c.name) }.join(", ")})
251
+ INSERT INTO #{qt(fts_table_name(model))}(rowid, #{cols.map { |c| qc(column_name(c)) }.join(", ")})
248
252
  SELECT #{qt(model.table_name)}.id, #{select} FROM #{qt(model.table_name)}#{where_clause};
249
253
  SQL
250
254
  end
@@ -369,7 +373,7 @@ module FullSearch
369
373
  def create_trigram_virtual_table_sql(model)
370
374
  dsl = model.full_search_dsl
371
375
  columns = (dsl.fields + dsl.filters.map { |f| FilterColumnPlaceholder.new(name: f.name) } + extra_columns(model))
372
- column_list = columns.map { |c| (c.respond_to?(:unindexed?) && c.unindexed?) ? "#{qc(c.name)} UNINDEXED" : qc(c.name) }.join(", ")
376
+ column_list = columns.map { |c| (c.respond_to?(:unindexed?) && c.unindexed?) ? "#{qc(column_name(c))} UNINDEXED" : qc(column_name(c)) }.join(", ")
373
377
 
374
378
  "CREATE VIRTUAL TABLE #{qt(trigram_table_name(model))} USING fts5(#{column_list}, tokenize='trigram');"
375
379
  end
@@ -390,7 +394,7 @@ module FullSearch
390
394
  where_clause = dsl.conditional_index? ? " WHERE (#{dsl.index_if_sql})" : ""
391
395
 
392
396
  <<~SQL
393
- INSERT INTO #{qt(trigram_table_name(model))}(rowid, #{cols.map { |c| qc(c.name) }.join(", ")})
397
+ INSERT INTO #{qt(trigram_table_name(model))}(rowid, #{cols.map { |c| qc(column_name(c)) }.join(", ")})
394
398
  SELECT #{qt(model.table_name)}.id, #{select} FROM #{qt(model.table_name)}#{where_clause};
395
399
  SQL
396
400
  end
@@ -469,7 +473,7 @@ module FullSearch
469
473
  end
470
474
 
471
475
  def col_names(cols)
472
- cols.map { |c| qc(c.name) }.join(", ")
476
+ cols.map { |c| qc(column_name(c)) }.join(", ")
473
477
  end
474
478
 
475
479
  def column_ref(col, prefix:)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FullSearch
4
- VERSION = "0.3.4"
4
+ VERSION = "0.3.5"
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.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben D'Angelo