Skip to content

Commit

Permalink
Adding specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibondarev committed Apr 21, 2023
1 parent d2a82d9 commit b8a44ef
Show file tree
Hide file tree
Showing 27 changed files with 728 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

/volumes
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
milvus (0.1.0)
milvus (0.9.0)
faraday (~> 2.7.0)

GEM
Expand Down Expand Up @@ -39,6 +39,7 @@ GEM

PLATFORMS
x86_64-darwin-19
x86_64-darwin-22

DEPENDENCIES
milvus!
Expand Down
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ client = Milvus::Client.new(
### Using the Collections endpoints

```ruby
# Data types: "boolean", "int8", "int16", "int32", "int64", "float", "double", "string", "varchar", "binary_vector", "float_vector"

# Creating a new collection schema
client.collections.create(
collection_name: "book",
Expand Down Expand Up @@ -71,19 +73,19 @@ client.collections.create(
```
```ruby
# Get the collection info
client.collections.get(collection_name: "recipes")
client.collections.get(collection_name: "book")
```
```ruby
# Delete the collection
client.collections.delete(collection_name: "recipes")
client.collections.delete(collection_name: "book")
```
```ruby
# Load the collection to memory before a search or a query
client.collections.load(collection_name: "recipes")
client.collections.load(collection_name: "book")
```
```ruby
# Release a collection from memory after a search or a query to reduce memory usage
client.collections.release(collection_name: "recipes")
client.collections.release(collection_name: "book")
```

### Inserting Data
Expand Down Expand Up @@ -159,7 +161,22 @@ client.indices.delete(
### Search & Querying
```ruby
client.search(

collection_name: "book",
output_fields: ["book_id"], # optional
anns_field: "book_intro",
top_k: "2",
params: "{\"nprobe\": 10}",
metric_type: "L2",
round_decimal: "-1",
vectors: [ [0.1,0.2] ],
dsl_type: 1
)
```
```ruby
client.query(
collection_name: "book",
output_fields: ["book_id", "book_intro"],
expr: "book_id in [2,4,6,8]"
)
```

Expand Down Expand Up @@ -215,4 +232,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/andrei

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
`milvus` is licensed under the Apache License, Version 2.0. View a copy of the License file.

2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require "milvus"
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
require "pry"
# require "pry"
# Pry.start

client = Milvus::Client.new(
Expand Down
2 changes: 2 additions & 0 deletions lib/milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ module Milvus
autoload :Health, "milvus/health"
autoload :Indices, "milvus/indices"
autoload :Partitions, "milvus/partitions"
autoload :Search, "milvus/search"
autoload :Query, "milvus/query"
end
8 changes: 4 additions & 4 deletions lib/milvus/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def indices
@indices ||= Milvus::Indices.new(client: self)
end

def search
@search ||= Milvus::Search.new(client: self).post
def search(...)
@search ||= Milvus::Search.new(client: self).post(...)
end

def query
@query ||= Milvus::Query.new(client: self).post
def query(...)
@query ||= Milvus::Query.new(client: self).post(...)
end

def connection
Expand Down
8 changes: 4 additions & 4 deletions lib/milvus/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def delete(collection_name:)
collection_name: collection_name
}.to_json
end
response.body || true
response.body.empty? ? true : response.body
end

# Load the collection to memory before a search or a query
Expand All @@ -52,17 +52,17 @@ def load(collection_name:)
collection_name: collection_name
}.to_json
end
response.body
response.body.empty? ? true : response.body
end

# Release a collection from memory after a search or a query to reduce memory usage
def release(collection_name:)
response = client.connection.delete("#{PATH}/release") do |req|
response = client.connection.delete("#{PATH}/load") do |req|
req.body = {
collection_name: collection_name
}.to_json
end
response.body
response.body.empty? ? true : response.body
end
end
end
8 changes: 4 additions & 4 deletions lib/milvus/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def insert(

req.body['partition_name'] = partition_name if partition_name
end
response.body
response.body.empty? ? true : response.body
end

# Delete the entities with the boolean expression you created
Expand All @@ -34,7 +34,7 @@ def delete(
expr: expression
}.to_json
end
response.body
response.body.empty? ? true : response.body
end

# Compact data manually
Expand All @@ -46,7 +46,7 @@ def compact!(
collectionID: collection_id
}.to_json
end
response.body
response.body.empty? ? true : response.body
end

# Check compaction status
Expand All @@ -58,7 +58,7 @@ def compact_status(
compactionID: compaction_id
}.to_json
end
response.body
response.body.empty? ? true : response.body
end
end
end
14 changes: 8 additions & 6 deletions lib/milvus/partitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class Partitions < Base

# Create a partition
def create(collection_name:, partition_name:)
response = client.connection.post(PATH, {
collection_name: collection_name,
partition_name: partition_name
})
response = client.connection.post(PATH) do |req|
req.body = {
collection_name: collection_name,
partition_name: partition_name
}
end
response.body.empty? ? true : response.body
end

Expand Down Expand Up @@ -53,13 +55,13 @@ def load(

def release(
collection_name:,
partition_name:,
partition_names:,
replica_number: nil
)
response = client.connection.delete("#{PATH}s/load") do |req|
req.body = {
collection_name: collection_name,
partition_name: partition_name
partition_names: partition_names
}
req.body[:replica_number] = replica_number if replica_number
end
Expand Down
17 changes: 17 additions & 0 deletions lib/milvus/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@
module Milvus
class Query < Base
PATH = "query".freeze

def post(
collection_name:,
output_fields:,
expr:
)
response = client.connection.post(PATH) do |req|
req.body = {
collection_name: collection_name,
expr: expr
}
if output_fields
req.body[:output_fields] = output_fields
end
end
response.body.empty? ? true : response.body
end
end
end
30 changes: 25 additions & 5 deletions lib/milvus/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ class Search < Base

def post(
collection_name:,
output_fileds:,
search_params:
output_fields: nil,
anns_field:,
top_k:,
params:,
metric_type:,
round_decimal: nil,
vectors:,
dsl_type:
)
response = client.connection.post(PATH) do |req|
req.body = {
collection_name: collection_name,
output_fileds: output_fileds,
search_params: search_params
}.to_json
search_params: [
{ key: "anns_field", value: anns_field },
{ key: "topk", value: top_k },
{ key: "params", value: params },
{ key: "metric_type", value: metric_type },
],
dsl_type: dsl_type,
vectors: vectors
}
if round_decimal
req.body[:search_params].push(
{ key: "round_decimal", value: round_decimal }
)
end
if output_fields
req.body[:output_fields] = output_fields
end
end
response.body.empty? ? true : response.body
end
Expand Down
2 changes: 1 addition & 1 deletion lib/milvus/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Milvus
VERSION = "0.1.0".freeze
VERSION = "0.9.0".freeze
end
50 changes: 50 additions & 0 deletions spec/fixtures/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"status": {},
"schema": {
"name": "book",
"description": "Test book search",
"fields": [
{
"fieldID": 100,
"name": "book_id",
"is_primary_key": true,
"description": "book id",
"data_type": 5
},
{
"fieldID": 101,
"name": "word_count",
"description": "count of words",
"data_type": 5
},
{
"fieldID": 102,
"name": "book_intro",
"description": "embedded vector of book introduction",
"data_type": 101,
"type_params": [
{
"key": "dim",
"value": "2"
}
]
}
]
},
"collectionID": 440928616022607200,
"virtual_channel_names": [
"by-dev-rootcoord-dml_14_440928616022607200v0",
"by-dev-rootcoord-dml_15_440928616022607200v1"
],
"physical_channel_names": [
"by-dev-rootcoord-dml_14",
"by-dev-rootcoord-dml_15"
],
"created_timestamp": 440932111134752770,
"created_utc_timestamp": 1682022518672,
"shards_num": 2,
"collection_name": "book"
}



4 changes: 4 additions & 0 deletions spec/fixtures/compaction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": {},
"compactionID": 434262132129005569
}
25 changes: 25 additions & 0 deletions spec/fixtures/entities_created.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"status": {},
"IDs": {
"IdField": {
"IntId": {
"data": [
1,
2,
3,
4,
5
]
}
}
},
"succ_index": [
0,
1,
2,
3,
4
],
"insert_cnt": 5,
"timestamp": 440950902901702658
}
15 changes: 15 additions & 0 deletions spec/fixtures/entities_deleted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"status": {},
"IDs": {
"IdField": {
"IntId": {
"data": [
0,
1
]
}
}
},
"delete_cnt": 2,
"timestamp": 440950991152218116
}
Loading

0 comments on commit b8a44ef

Please sign in to comment.