diff --git a/examples/Gemfile b/examples/Gemfile deleted file mode 100644 index d77ec765..00000000 --- a/examples/Gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source "https://rubygems.org" - -gemspec path: ".." - -gem "activerecord" -gem "disco" -gem "elasticsearch" -gem "opensearch-ruby" -gem "sqlite3" diff --git a/examples/elasticsearch_knn.rb b/examples/elasticsearch_knn.rb deleted file mode 100644 index a779c3b0..00000000 --- a/examples/elasticsearch_knn.rb +++ /dev/null @@ -1,62 +0,0 @@ -require "active_record" -require "disco" -require "elasticsearch" -require "searchkick" - -ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" -ActiveRecord::Schema.verbose = false -ActiveRecord::Schema.define do - create_table :movies do |t| - t.string :name - t.text :embedding - end -end - -class Movie < ActiveRecord::Base - # remove "coder: " for Active Record < 7.1 - serialize :embedding, coder: JSON - - searchkick \ - mappings: { - properties: { - embedding: { - type: "dense_vector", - dims: 20, - index: true, - similarity: "cosine" - } - } - }, - merge_mappings: true - - def search_data - { - name: name, - embedding: embedding - } - end -end - -data = Disco.load_movielens -recommender = Disco::Recommender.new(factors: 20) -recommender.fit(data) - -movies = [] -recommender.item_ids.each do |item_id| - movies << {name: item_id, embedding: recommender.item_factors(item_id).to_a} -end -Movie.insert_all!(movies) - -Movie.reindex - -movie = Movie.find_by!(name: "Star Wars (1977)") -body = { - knn: { - filter: {bool: {must_not: {term: {_id: movie.id}}}}, - field: "embedding", - k: 5, - num_candidates: 5, - query_vector: movie.embedding - } -} -pp Movie.search(body: body).map(&:name) diff --git a/examples/opensearch_knn.rb b/examples/opensearch_knn.rb deleted file mode 100644 index dddc6d1a..00000000 --- a/examples/opensearch_knn.rb +++ /dev/null @@ -1,74 +0,0 @@ -require "active_record" -require "disco" -require "opensearch-ruby" -require "searchkick" - -ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" -ActiveRecord::Schema.verbose = false -ActiveRecord::Schema.define do - create_table :movies do |t| - t.string :name - t.text :embedding - end -end - -class Movie < ActiveRecord::Base - # remove "coder: " for Active Record < 7.1 - serialize :embedding, coder: JSON - - searchkick \ - settings: { - index: { - knn: true - } - }, - mappings: { - properties: { - embedding: { - type: "knn_vector", - dimension: 20, - method: { - name: "hnsw", - space_type: "cosinesimil", - engine: "lucene" - } - } - } - }, - merge_mappings: true - - def search_data - { - name: name, - embedding: embedding - } - end -end - -data = Disco.load_movielens -recommender = Disco::Recommender.new(factors: 20) -recommender.fit(data) - -movies = [] -recommender.item_ids.each do |item_id| - movies << {name: item_id, embedding: recommender.item_factors(item_id).to_a} -end -Movie.insert_all!(movies) - -Movie.reindex - -movie = Movie.find_by!(name: "Star Wars (1977)") -# uses efficient filtering available in OpenSearch 2.4+ -# https://opensearch.org/docs/latest/search-plugins/knn/filter-search-knn/ -body = { - query: { - knn: { - embedding: { - filter: {bool: {must_not: {term: {_id: movie.id}}}}, - vector: movie.embedding, - k: 5 - } - } - } -} -pp Movie.search(body: body).map(&:name)