Skip to content
Snippets Groups Projects

Resolve #3403561 "Support pinecone starterfree"

@@ -48,7 +48,7 @@ class SearchApiPineconeBackend extends BackendPluginBase implements PluginFormIn
$form['namespace'] = [
'#type' => 'textfield',
'#title' => $this->t('Namespace'),
'#description' => $this->t('An optional override for the default namespace. The index ID will always be appended.'),
'#description' => $this->t('An optional override for the default namespace. The index ID will always be appended unless namespaces are disabled in the Pinecone configuration.'),
'#default_value' => $this->configuration['namespace'],
'#pattern' => '[a-zA-Z0-9_]*',
];
@@ -77,7 +77,13 @@ class SearchApiPineconeBackend extends BackendPluginBase implements PluginFormIn
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item) {
// Delete the item as we are actually inserting multiple.
$this->deleteItems($index, [$item->getId()]);
try {
$this->deleteItems($index, [$item->getId()]);
}
catch (\Exception $exception) {
// Do nothing, Starter Pinecone does not allow deletion
// with filters.
}
$itemBase = [
'metadata' => [
@@ -126,7 +132,7 @@ class SearchApiPineconeBackend extends BackendPluginBase implements PluginFormIn
$this->client->delete([
'collection' => $this->getNamespace($index),
'filter' => [
'item_id' => ['$in' => $item_ids],
'item_id' => ['$in' => array_values($item_ids)],
],
]);
}
@@ -135,10 +141,16 @@ class SearchApiPineconeBackend extends BackendPluginBase implements PluginFormIn
* {@inheritdoc}
*/
public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) {
$this->client->delete([
'deleteAll' => TRUE,
'collection' => $this->getNamespace($index),
]);
try {
$this->client->delete([
'deleteAll' => TRUE,
'collection' => $this->getNamespace($index),
]);
}
catch (\Exception $exception) {
// Do nothing, Starter Pinecone does not allow deletion.
// The OpenAI module will let the user know what to do.
}
}
/**
@@ -189,6 +201,12 @@ class SearchApiPineconeBackend extends BackendPluginBase implements PluginFormIn
* The pinecone namespace.
*/
public function getNamespace(IndexInterface $index): string {
// In the free version of pinecone, namespaces are not allowed.
if ($this->client->configuration['disable_namespace']) {
return '';
}
// In non-free version, set a default namespace if not set.
return ($this->configuration['namespace'] ?? "searchapi:{$this->server->id()}") . ":{$index->id()}";
}
Loading