Skip to content

Commit

Permalink
Possible to set non-default context on an endpoint (#967)
Browse files Browse the repository at this point in the history
* Possible to set non-default context on an endpoint

* Release as 6.2.1
  • Loading branch information
thomascorthals authored Dec 26, 2021
1 parent f63861d commit c50b383
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [6.2.1]
### Added
- Possibility to set the context on an endpoint for SolrCloud instances with a non-default `hostContext` or Solr instances behind a reverse proxy, defaults to `solr` if omitted


## [6.2.0]
### Added
- Component\FacetSet::setOffset()
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ See https://solr.apache.org/guide/local-parameters-in-queries.html for an introd

### Pitfall when upgrading from 3.x or 4.x

In the past, the V1 API endpoint **_solr_** was not added automatically, so most users set it as path on the endpoint.
In the past, the V1 API endpoint `solr` was not added automatically, so most users set it as path on the endpoint.
This bug was discovered with the addition of V2 API support. In almost every setup, the path has to be set to `/`
instead of `/solr` with this release!

Expand All @@ -95,6 +95,21 @@ has to be changed to something like
'collection' => 'xxxx',
```

This led to a problem if the endpoint _isn't_ the default `solr`. Since 6.2.1, a different context can be configured.

An old settings like
```
'path' => '/index/xxxx/'
```
can be changed to something like
```
'path' => '/',
'context' => 'index',
'collection' => 'xxxx',
```

This works for SolrCloud instances with a non-default `hostContext` and Solr instances behind a reverse proxy.

## Run the examples

To run the examples read through the _Example code_ section of
Expand Down
18 changes: 17 additions & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ htmlFooter();

### Pitfall when upgrading from earlier versions to 5.x

In the past, the V1 API endpoint **_solr_** was not added automatically, so most users set it as path on the endpoint.
In the past, the V1 API endpoint `solr` was not added automatically, so most users set it as path on the endpoint.
This bug was discovered with the addition of V2 API support. In almost every setup, the path has to be set to `/`
instead of `/solr` with this release!

Expand All @@ -93,6 +93,20 @@ has to be changed to something like
'collection' => 'xxxx',
```

This led to a problem if the endpoint _isn't_ the default `solr`. Since 6.2.1, a different context can be configured.

An old settings like
```
'path' => '/index/xxxx/'
```
can be changed to something like
```
'path' => '/',
'context' => 'index',
'collection' => 'xxxx',
```

This works for SolrCloud instances with a non-default `hostContext` and Solr instances behind a reverse proxy.

### Available integrations

Expand Down Expand Up @@ -142,6 +156,8 @@ $config = array(
'core' => 'techproducts',
// For SolrCloud you need to provide a collection instead of core:
// 'collection' => 'techproducts',
// Set the `hostContext` for the Solr web application if it's not the default 'solr':
// 'context' => 'solr',
)
)
);
Expand Down
1 change: 1 addition & 0 deletions examples/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/',
// 'context' => 'solr', # only necessary to set if not the default 'solr'
'core' => 'techproducts',
)
)
Expand Down
48 changes: 39 additions & 9 deletions src/Core/Client/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Endpoint extends Configurable
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/',
'context' => 'solr',
'collection' => null,
'core' => null,
'leader' => false,
Expand All @@ -45,7 +46,7 @@ class Endpoint extends Configurable
*/
public function __toString()
{
$output = __CLASS__.'::__toString'."\n".'host: '.$this->getHost()."\n".'port: '.$this->getPort()."\n".'path: '.$this->getPath()."\n".'collection: '.$this->getCollection()."\n".'core: '.$this->getCore()."\n".'authentication: '.print_r($this->getAuthentication(), true);
$output = __CLASS__.'::__toString'."\n".'host: '.$this->getHost()."\n".'port: '.$this->getPort()."\n".'path: '.$this->getPath()."\n".'context: '.$this->getContext()."\n".'collection: '.$this->getCollection()."\n".'core: '.$this->getCore()."\n".'authentication: '.print_r($this->getAuthentication(), true);

return $output;
}
Expand Down Expand Up @@ -133,11 +134,7 @@ public function getPort(): ?int
*/
public function setPath(string $path): self
{
if ('/' === substr($path, -1)) {
$path = substr($path, 0, -1);
}

$this->setOption('path', $path);
$this->setOption('path', rtrim($path, '/'));

return $this;
}
Expand All @@ -152,6 +149,32 @@ public function getPath(): ?string
return $this->getOption('path');
}

/**
* Set context option.
*
* If the context has a leading or trailing slash it will be removed.
*
* @param string $context
*
* @return self Provides fluent interface
*/
public function setContext(string $context): self
{
$this->setOption('context', trim($context, '/'));

return $this;
}

/**
* Get context option.
*
* @return string|null
*/
public function getContext(): ?string
{
return $this->getOption('context');
}

/**
* Set collection option.
*
Expand Down Expand Up @@ -236,10 +259,11 @@ public function getScheme(): ?string
public function getCollectionBaseUri(): string
{
$uri = $this->getServerUri();
$context = $this->getContext();
$collection = $this->getCollection();

if ($collection) {
$uri .= 'solr/'.$collection.'/';
$uri .= $context.'/'.$collection.'/';
} else {
throw new UnexpectedValueException('No collection set.');
}
Expand All @@ -259,11 +283,12 @@ public function getCollectionBaseUri(): string
public function getCoreBaseUri(): string
{
$uri = $this->getServerUri();
$context = $this->getContext();
$core = $this->getCore();

if ($core) {
// V1 API
$uri .= 'solr/'.$core.'/';
$uri .= $context.'/'.$core.'/';
} else {
throw new UnexpectedValueException('No core set.');
}
Expand Down Expand Up @@ -300,7 +325,7 @@ public function getBaseUri(): string
*/
public function getV1BaseUri(): string
{
return $this->getServerUri().'solr/';
return $this->getServerUri().$this->getContext().'/';
}

/**
Expand Down Expand Up @@ -384,8 +409,10 @@ public function isLeader(): ?bool
* Initialization hook.
*
* In this case the path needs to be cleaned of trailing slashes.
* The context needs to be cleaned of leading and trailing slashes.
*
* @see setPath()
* @see setContext()
*/
protected function init()
{
Expand All @@ -394,6 +421,9 @@ protected function init()
case 'path':
$this->setPath($value);
break;
case 'context':
$this->setContext($value);
break;
}
}
}
Expand Down
Loading

0 comments on commit c50b383

Please sign in to comment.