forked from ankane/searchkick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensearch_knn.rb
72 lines (64 loc) · 1.4 KB
/
opensearch_knn.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
serialize :embedding, 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)")
body = {
query: {
knn: {
embedding: {
vector: movie.embedding,
k: 6 # size + 1, since post_filter will remove one
}
}
},
size: 5,
post_filter: {bool: {must_not: {term: {_id: movie.id}}}}
}
pp Movie.search(body: body).map(&:name)