-
Notifications
You must be signed in to change notification settings - Fork 18
/
queries.js
152 lines (138 loc) · 4.39 KB
/
queries.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { default: weaviate } = require('weaviate-ts-client');
require('dotenv').config();
const client = weaviate.client({
scheme: 'https',
host: process.env.WEAVIATE_URL, // Replace with your endpoint
apiKey: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY),// Replace w/ your Weaviate instance API key
headers: { 'X-OpenAI-Api-Key': process.env.OPENAI_API_KEY },
});
let num_movies = 20;
//Query to fetch keyword results
async function get_keyword_results(text) {
let _text = text
if (!text) {
_text = "Movie"
}
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withBm25({
query: _text,
properties: ['title^3', 'director', 'genres', 'actors', 'keywords', 'description', 'plot'],
})
.withFields(['title', 'poster_link', 'genres', 'year', 'director', 'movie_id'])
.withLimit(num_movies)
.do()
.then(info => {
return info
})
.catch(err => {
console.error(err)
})
return data;
}
//Query to fetch results by sematic searching
async function get_semantic_results(text) {
if (text.length === 0) {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'poster_link', 'genres', 'year', 'director', 'movie_id'])
.withLimit(num_movies)
.do()
.then(info => {
return info
})
.catch(err => {
console.error(err)
});
return data;
} else {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'poster_link', 'genres', 'year', 'director', 'movie_id'])
.withNearText({ concepts: [text] })
.withLimit(num_movies)
.do()
.then(info => {
return info
})
.catch(err => {
console.error(err)
});
return data;
}
}
//Query to fetch results by sematic searching
async function get_hybrid_results(text) {
if (text.length === 0) {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'poster_link', 'genres', 'year', 'director', 'movie_id'])
.withLimit(num_movies)
.do()
.then(info => {
return info
})
.catch(err => {
console.error(err)
});
return data;
} else {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'poster_link', 'genres', 'year', 'director', 'movie_id'])
.withHybrid({ query: text, alpha: 0.5 })
.withLimit(num_movies)
.do()
.then(info => {
return info
})
.catch(err => {
console.error(err)
});
return data;
}
}
//Query to fetch movie details
async function get_movie_details(id) {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'poster_link', 'description', 'year', 'director', 'actors', 'genres', 'keywords', 'movie_id', '_additional { id certainty }'])
.withWhere({
path: ["movie_id"],
operator: "Equal",
valueNumber: parseInt(id)
})
.do()
.then(info => {
return info;
})
.catch(err => {
console.error(err)
})
return data;
}
//Query to fetch recommended movies
async function get_recommended_movies(mov_id) {
let data = await client.graphql
.get()
.withClassName('Awesome_moviate_movies')
.withFields(['title', 'genres', 'year', 'poster_link', 'movie_id'])
.withNearObject({ id: mov_id })
.withLimit(num_movies + 1)
.do()
.then(info => {
return info;
})
.catch(err => {
console.error(err)
});
return data;
}
//Exporting these function as they need to be used in index.js
module.exports = { get_keyword_results, get_semantic_results, get_hybrid_results, get_movie_details, get_recommended_movies }