Class: Litesearch::Schema::BasicAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/litestack/litesearch/schema_adapters/basic_adapter.rb

Direct Known Subclasses

ContentlessAdapter, StandaloneAdapter

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ BasicAdapter

Returns a new instance of BasicAdapter.



2
3
4
5
6
7
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 2

def initialize(schema)
  @schema = schema
  @sql = {}
  enrich_schema
  generate_sql
end

Instance Method Details

#active_cols_namesObject



33
34
35
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 33

def active_cols_names
  active_fields.collect { |k, v| v[:col] }
end

#active_field_namesObject



29
30
31
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 29

def active_field_names
  active_fields.keys
end

#active_fieldsObject



25
26
27
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 25

def active_fields
  @schema[:fields].select { |k, v| v[:weight] != 0 }
end

#active_weightsObject



41
42
43
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 41

def active_weights
  active_fields.values.collect { |v| v[:weight].to_f }
end

#field_namesObject



21
22
23
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 21

def field_names
  @schema[:fields].keys
end

#fieldsObject



17
18
19
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 17

def fields
  @schema[:fields]
end

#generate_sqlObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 78

def generate_sql
  @sql[:create_index] = :create_index_sql
  @sql[:create_vocab_tables] = :create_vocab_tables_sql
  @sql[:insert] = "INSERT OR REPLACE INTO #{name}(rowid, #{active_col_names_sql}) VALUES (:id, #{active_col_names_var_sql}) RETURNING rowid"
  @sql[:delete] = "DELETE FROM #{name} WHERE rowid = :id"
  @sql[:count] = "SELECT count(*) FROM #{name}(:term)"
  @sql[:count_all] = "SELECT count(*) FROM #{name}"
  @sql[:delete_all] = "DELETE FROM #{name}"
  @sql[:drop] = "DROP TABLE #{name}"
  @sql[:expand_data] = "UPDATE #{name}_data SET block = block || zeroblob(:length) WHERE id = 1"
  @sql[:expand_docsize] = "UPDATE #{name}_docsize SET sz = sz || zeroblob(:length)"
  @sql[:ranks] = :ranks_sql
  @sql[:set_config_value] = "INSERT OR REPLACE INTO #{name}_config(k, v) VALUES (:key, :value)"
  @sql[:get_config_value] = "SELECT v FROM #{name}_config WHERE k = :key"
  @sql[:search] = "SELECT rowid AS id, -rank AS search_rank FROM #{name}(:term) WHERE rank !=0 ORDER BY rank LIMIT :limit OFFSET :offset"
  @sql[:similarity_terms] = "SELECT DISTINCT term FROM #{name}_instance WHERE doc = :id AND FLOOR(term) IS NULL AND LENGTH(term) > 2 AND NOT instr(term, ' ') AND NOT instr(term, '-') AND NOT instr(term, ':') AND NOT instr(term, '#') AND NOT instr(term, '_') LIMIT 15"
  @sql[:similarity_query] = "SELECT group_concat('\"' || term || '\"', ' OR ') FROM #{name}_row WHERE term IN (#{@sql[:similarity_terms]})"
  @sql[:similarity_search] = "SELECT rowid AS id, -rank AS search_rank FROM #{name}(:term) WHERE rowid != :id ORDER BY rank LIMIT :limit"
  @sql[:similar] = "SELECT rowid AS id, -rank AS search_rank FROM #{name} WHERE #{name} = (#{@sql[:similarity_query]}) AND rowid != :id ORDER BY rank LIMIT :limit"
  @sql[:update_index] = "UPDATE sqlite_schema SET sql = :sql WHERE name = '#{name}'"
  @sql[:update_content_table] = "UPDATE sqlite_schema SET sql = :sql WHERE name = '#{name}_content'"
end

#nameObject



9
10
11
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 9

def name
  @schema[:name]
end

#order_fields(old_schema) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 49

def order_fields(old_schema)
  new_fields = {}
  old_field_names = old_schema.schema[:fields].keys
  old_field_names.each do |name|
    new_fields[name] = @schema[:fields].delete(name)
  end
  missing_field_names = field_names - old_field_names
  missing_field_names.each do |name|
    new_fields[name] = @schema[:fields].delete(name)
  end
  @schema[:fields] = new_fields # this should be in order now
  generate_sql
  enrich_schema
end

#sql_for(method, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 64

def sql_for(method, *args)
  if (sql = @sql[method])
    if sql.is_a? String
      sql
    elsif sql.is_a? Proc
      sql.call(*args)
    elsif sql.is_a? Symbol
      send(sql, *args)
    elsif sql.is_a? Litesearch::SchemaChangeException
      raise sql
    end
  end
end

#tableObject



13
14
15
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 13

def table
  @schema[:table]
end

#tokenizer_sqlObject



45
46
47
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 45

def tokenizer_sql
  Litesearch::Schema::TOKENIZERS[@schema[:tokenizer]]
end

#weightsObject



37
38
39
# File 'lib/litestack/litesearch/schema_adapters/basic_adapter.rb', line 37

def weights
  @schema[:fields].values.collect { |v| v[:weight].to_f }
end