|
2 | 2 |
|
3 | 3 | module Weaviate |
4 | 4 | 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 | + |
5 | 29 | def get( |
6 | 30 | class_name:, |
7 | 31 | fields:, |
8 | 32 | limit: nil, |
| 33 | + offset: nil, |
| 34 | + after: nil, |
| 35 | + sort: nil, |
| 36 | + where: nil, |
9 | 37 | near_text: nil |
10 | 38 | ) |
11 | 39 | params = {} |
12 | 40 | params["nearText"] = near_text unless near_text.nil? |
13 | 41 | 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 | + |
14 | 64 | # TODO implement the rest of the API params |
15 | 65 |
|
16 | 66 | response = client.graphql.execute(get_query(class_name, params, fields), near_text: near_text) |
|
0 commit comments