Skip to content

Commit a09751f

Browse files
Supporting additional parameters on the client.query.get() endpoints
1 parent 9d3f109 commit a09751f

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,17 @@ response.data
139139
### Querying
140140
```ruby
141141
near_text = { "concepts": ["biology"] }
142+
sort_obj = { path: ["category"] } # Not supporting the `order:` param currently
143+
where_obj = { path: ["id"], operator: :equal, valueString: "..." }
142144

143145
client.query.get(
144146
class_name: 'Question',
145147
fields: ['question', 'answer', 'category'],
146148
limit: 1,
149+
offset: 1,
150+
after: 'id',
151+
sort: sort_obj,
152+
where_obj: where_obj,
147153

148154
# To use this parameter you must have created your schema by setting the `vectorizer:` property to
149155
# either 'text2vec-transformers', 'text2vec-contextionary', 'text2vec-openai', 'multi2vec-clip', 'text2vec-huggingface' or 'text2vec-cohere'

lib/weaviate/query.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,65 @@
22

33
module Weaviate
44
class Query < Base
5+
# Meta-programming hack to dynamically create appropriate Weaviate GraphQL query. Expected query:
6+
#
7+
# Article(where: {
8+
# ...
9+
# operator: GreaterThan, # operator
10+
#
11+
# Since `operator:` parameter expects a literal value, the only way to pass it in Ruby is by defining a class and returning it
12+
# inside of a lambda function when it's called. This is why we have the following code:
13+
# standard:disable all
14+
WHERE_OPERANDS = {
15+
and: -> { class ::And; end; And },
16+
or: -> { class ::Or; end; Or },
17+
equal: -> { class ::Equal; end; Equal },
18+
not_equal: -> { class ::NotEqual; end; NotEqual },
19+
greater_than: -> { class ::GreaterThan; end; GreaterThan },
20+
greater_than_equal: -> { class ::GreaterThanEqual; end; GreaterThanEqual },
21+
less_than: -> { class ::LessThan; end; LessThan },
22+
less_than_equal: -> { class ::LessThanEqual; end; LessThanEqual },
23+
like: -> { class ::Like; end; Like },
24+
within_geo_range: -> { class ::WithinGeoRange; end; WithinGeoRange },
25+
is_null: -> { class ::IsNull; end; IsNull }
26+
}.freeze
27+
# standard:enable all
28+
529
def get(
630
class_name:,
731
fields:,
832
limit: nil,
33+
offset: nil,
34+
after: nil,
35+
sort: nil,
36+
where: nil,
937
near_text: nil
1038
)
1139
params = {}
1240
params["nearText"] = near_text unless near_text.nil?
1341
params["limit"] = limit unless limit.nil?
42+
params["offset"] = offset unless offset.nil?
43+
params["after"] = after unless after.nil?
44+
45+
if sort.present?
46+
# TODO: Implement the `order:` param
47+
# Unable to currently support the `order:` param. Weaviate GraphQL API expects a literal value, but we can't pass it in Ruby
48+
# Article(sort: [{
49+
# order: asc # <--- this is the problem.
50+
# https://weaviate.io/developers/weaviate/api/graphql/filters#sorting-api
51+
#
52+
sort.delete("order")
53+
params["sort"] = sort
54+
end
55+
56+
if where.present?
57+
params["where"] = where
58+
if where[:operator].present?
59+
params["where"][:operator] = WHERE_OPERANDS[where[:operator].to_sym].call
60+
end
61+
# TODO: Transform the multiple operands: [{}] case
62+
end
63+
1464
# TODO implement the rest of the API params
1565

1666
response = client.graphql.execute(get_query(class_name, params, fields), near_text: near_text)

spec/weaviate/query_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,25 @@
5454
limit: 1
5555
)
5656

57+
expect(data.count).to eq(1)
5758
expect(data.first.category).to eq("SCIENCE")
5859
expect(data.first.question).to eq("In 1953 Watson & Crick built a model of the molecular structure of this, the gene-carrying substance")
5960
end
6061
end
62+
63+
describe "WHERE_OPERANDS" do
64+
it "returns the correct class name" do
65+
expect(Weaviate::Query::WHERE_OPERANDS[:and].call).to eq(And)
66+
expect(Weaviate::Query::WHERE_OPERANDS[:or].call).to eq(Or)
67+
expect(Weaviate::Query::WHERE_OPERANDS[:equal].call).to eq(Equal)
68+
expect(Weaviate::Query::WHERE_OPERANDS[:not_equal].call).to eq(NotEqual)
69+
expect(Weaviate::Query::WHERE_OPERANDS[:greater_than].call).to eq(GreaterThan)
70+
expect(Weaviate::Query::WHERE_OPERANDS[:greater_than_equal].call).to eq(GreaterThanEqual)
71+
expect(Weaviate::Query::WHERE_OPERANDS[:less_than].call).to eq(LessThan)
72+
expect(Weaviate::Query::WHERE_OPERANDS[:less_than_equal].call).to eq(LessThanEqual)
73+
expect(Weaviate::Query::WHERE_OPERANDS[:like].call).to eq(Like)
74+
expect(Weaviate::Query::WHERE_OPERANDS[:within_geo_range].call).to eq(WithinGeoRange)
75+
expect(Weaviate::Query::WHERE_OPERANDS[:is_null].call).to eq(IsNull)
76+
end
77+
end
6178
end

0 commit comments

Comments
 (0)