Skip to content

Commit

Permalink
Create own configset for every collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jan 24, 2023
1 parent 00051c6 commit dcd5add
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 59 deletions.
17 changes: 15 additions & 2 deletions packages/seal-solr-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,23 @@ The following code shows how to create an Engine using this Adapter:
<?php

use Solr\Client;
use Solarium\Core\Client\Adapter\Curl;
use Schranz\Search\SEAL\Adapter\Solr\SolrAdapter;
use Schranz\Search\SEAL\Engine;

$client = new Client('http://127.0.0.1:8983');
use Symfony\Component\EventDispatcher\EventDispatcher;

$client = new Client(new Curl(), new EventDispatcher(), [
'endpoint' => [
'localhost' => [
'host' => '127.0.0.1',
'port' => '8983',
// authenticated required for configset api https://solr.apache.org/guide/8_9/configsets-api.html
// alternative set solr.disableConfigSetsCreateAuthChecks=true in your server setup
'username' => 'solr',
'password' => 'SolrRocks',
],
]
]);

$engine = new Engine(
new SolrAdapter($client),
Expand Down
115 changes: 62 additions & 53 deletions packages/seal-solr-adapter/SolrSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function dropIndex(Index $index, array $options = []): ?TaskInterface

$this->client->collections($collectionQuery);

$configsetQuery = $this->client->createConfigsets();

$action = $configsetQuery->createDelete()
->setName($index->name);
$configsetQuery->setAction($action);
$this->client->configsets($configsetQuery);

if (true !== ($options['return_slow_promise_result'] ?? false)) {
return null;
}
Expand All @@ -49,24 +56,36 @@ public function dropIndex(Index $index, array $options = []): ?TaskInterface

public function createIndex(Index $index, array $options = []): ?TaskInterface
{
$configsetQuery = $this->client->createConfigsets();

$action = $configsetQuery->createCreate()
->setName($index->name)
->setBaseConfigSet('_default');
$configsetQuery->setAction($action);

$this->client->configsets($configsetQuery);

$collectionQuery = $this->client->createCollections();

$action = $collectionQuery->createCreate([
'name' => $index->name,
'numShards' => 1,
'collection.configName' => $index->name,
]);
$collectionQuery->setAction($action);

$this->client->collections($collectionQuery);

$requests = $this->createRequests($index->fields);
$indexFields = $this->createIndexFields($index->fields);

foreach ($requests as $request) {
foreach ($indexFields as $indexField) {
$query = $this->client->createApi([
'version' => Request::API_V1,
'handler' => $index->name.'/schema',
'method' => Request::METHOD_POST,
'rawdata' => json_encode($request, \JSON_THROW_ON_ERROR),
'rawdata' => json_encode([
'add-field' => $indexField,
], \JSON_THROW_ON_ERROR),
]);

$this->client->execute($query);
Expand All @@ -93,65 +112,55 @@ public function createIndex(Index $index, array $options = []): ?TaskInterface
*
* @return array<string, mixed>
*/
private function createRequests(array $fields): array
private function createIndexFields(array $fields): array
{
$requests = [];
$indexFields = [];

foreach ($fields as $name => $field) {
match (true) {
$field instanceof Field\IdentifierField => null,
$field instanceof Field\TextField => $requests[$name] = [
'add-field' => [
'name' => $name,
'type' => 'string',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\IdentifierField => null, // TODO define primary field
$field instanceof Field\TextField => $indexFields[$name] = [
'name' => $name,
'type' => 'string',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\BooleanField => $requests[$name] = [
'add-field' => [
'name' => $name,
'type' => 'bool',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\BooleanField => $indexFields[$name] = [
'name' => $name,
'type' => 'bool',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\DateTimeField => $requests[$name] = [
'add-field' => [
'name' => $name,
'type' => 'pdate',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\DateTimeField => $indexFields[$name] = [
'name' => $name,
'type' => 'pdate',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\IntegerField => $requests[$name] = [
'add-field' => [
'name' => $name,
'type' => 'pint',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\IntegerField => $indexFields[$name] = [
'name' => $name,
'type' => 'pint',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\FloatField => $requests[$name] = [
'add-field' => [
'name' => $name,
'type' => 'pfloat',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
$field instanceof Field\FloatField => $indexFields[$name] = [
'name' => $name,
'type' => 'pfloat',
'indexed' => $field->searchable,
'docValues' => $field->filterable || $field->sortable,
'stored' => false,
'multiValued' => $field->multiple,
],
default => null,
/*
/* TODO implement ObjectField and TypedField
$field instanceof Field\ObjectField => $fields[$name] = [
'type' => 'object',
'properties' => $this->createPropertiesMapping($field->fields),
Expand All @@ -162,6 +171,6 @@ private function createRequests(array $fields): array
};
}

return $requests;
return $indexFields;
}
}
8 changes: 4 additions & 4 deletions packages/seal-solr-adapter/Tests/ClientHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public static function getClient(): Client
$adapter = new Curl();
$eventDispatcher = new EventDispatcher();
$options = [
'endpoint' => array(
'localhost' => array(
'endpoint' => [
'localhost' => [
'host' => $host,
'port' => $port,
),
)
],
]
];

self::$client = new Client($adapter, $eventDispatcher, $options);
Expand Down
4 changes: 4 additions & 0 deletions packages/seal-solr-adapter/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
interval: 5s
timeout: 5s
retries: 10
environment:
SOLR_OPTS: '-Dsolr.disableConfigSetsCreateAuthChecks=true'
volumes:
- solr-data:/var/solr

Expand All @@ -19,6 +21,8 @@ services:
depends_on:
- "solr"
network_mode: "service:solr"
environment:
SOLR_OPTS: '-Dsolr.disableConfigSetsCreateAuthChecks=true'
command: bash -c "set -x; export; wait-for-solr.sh; solr zk -z localhost:9983 upconfig -n default -d /opt/solr/server/solr/configsets/_default; tail -f /dev/null"

volumes:
Expand Down

0 comments on commit dcd5add

Please sign in to comment.