Skip to content

Commit

Permalink
feat: metadata filtering (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossvogel committed Jun 7, 2023
1 parent 4cc13de commit 6bb1a8d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/pinecone.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ defmodule Pinecone do

@doc """
Deletes all vectors from the given Pinecone index.
If the filter option is passed, deletes only vectors that match the given metadata filter
## Options
Expand All @@ -294,16 +295,22 @@ defmodule Pinecone do
* `:config` - client configuration used to override application
level configuration. Defaults to `nil`
* `:filter` - metadata filter to apply to the deletion. See https://docs.pinecone.io/docs/metadata-filtering
"""
@spec delete_all_vectors(index :: index_type(), opts :: keyword()) ::
success_type(String.t()) | error_type()
def delete_all_vectors(%Index{name: name, project_name: project_name}, opts \\ []) do
opts = Keyword.validate!(opts, [:config, :namespace])
opts = Keyword.validate!(opts, [:config, :namespace, :filter])

params = [{"delete_all", true}]
params = if opts[:namespace], do: [{"namespace", opts[:namespace]} | params], else: params
body =
if is_map(opts[:filter]),
do: %{"filter" => opts[:filter]},
else: %{"deleteAll" => true}

delete({:vectors, "#{name}-#{project_name}"}, "vectors/delete", opts[:config], params: params)
body = if opts[:namespace], do: Map.put(body, "namespace", opts[:namespace]), else: body

post({:vectors, "#{name}-#{project_name}"}, "vectors/delete", body, opts[:config])
end

@doc """
Expand All @@ -323,6 +330,8 @@ defmodule Pinecone do
* `:namespace` - index namespace to query. Defaults to `nil`
which will query vectors in the default namespace
* `:filter` - metadata filter to apply to the query. See https://docs.pinecone.io/docs/metadata-filtering
* `:config` - client configuration used to override application
level configuration. Defaults to `nil`
"""
Expand All @@ -335,18 +344,21 @@ defmodule Pinecone do
:namespace,
top_k: 5,
include_values: false,
include_metadata: false
include_metadata: false,
filter: %{}
])

validate!("top_k", opts[:top_k], :non_negative_integer)
validate!("include_values", opts[:include_values], :boolean)
validate!("include_metadata", opts[:include_metadata], :boolean)
validate!("filter", opts[:filter], :map)

body = %{
"vector" => vector,
"topK" => opts[:top_k],
"includeValues" => opts[:include_values],
"includeMetadata" => opts[:include_metadata]
"includeMetadata" => opts[:include_metadata],
"filter" => opts[:filter]
}

body = if opts[:namespace], do: Map.put(body, "namespace", opts[:namespace]), else: body
Expand Down Expand Up @@ -434,6 +446,7 @@ defmodule Pinecone do
defp validate!(_key, value, :non_negative_integer) when is_integer(value) and value > 0, do: :ok
defp validate!(_key, value, :boolean) when is_boolean(value), do: :ok
defp validate!(_key, value, :binary) when is_binary(value), do: :ok
defp validate!(_key, value, :map) when is_map(value), do: :ok

defp validate!(key, value, type) do
raise ArgumentError, "expected #{key} to be type #{inspect(type)}, got #{inspect(value)}"
Expand Down

0 comments on commit 6bb1a8d

Please sign in to comment.