Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
search_api_elasticsearchkit_proxy
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
search_api_elasticsearchkit_proxy
Commits
8f44986a
Commit
8f44986a
authored
1 week ago
by
Tim Diels
Committed by
Mohammad Fayoumi
1 week ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3522879
by tim-diels, mohammad-fayoumi: Support Elasticsearch Connector 8.x
parent
e41c3cb6
Branches
2.0.x
Tags
2.0.0
1 merge request
!1
Provide support for elasticsearch connector 8.x
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
search_api_elasticsearchkit_proxy.routing.yml
+9
-1
9 additions, 1 deletion
search_api_elasticsearchkit_proxy.routing.yml
src/Controller/ElasticSearchkitProxyController.php
+64
-58
64 additions, 58 deletions
src/Controller/ElasticSearchkitProxyController.php
with
73 additions
and
59 deletions
search_api_elasticsearchkit_proxy.routing.yml
+
9
−
1
View file @
8f44986a
elastic_searchkit_proxy.search
:
path
:
'
/api/search/_
m
search'
path
:
'
/api/search/_search'
defaults
:
_title
:
'
Elasticsearch
connector'
_controller
:
'
\Drupal\search_api_elasticsearchkit_proxy\Controller\ElasticSearchkitProxyController::search'
requirements
:
_permission
:
'
access
content'
methods
:
[
'
POST'
]
elastic_searchkit_proxy.msearch
:
path
:
'
/api/search/_msearch'
defaults
:
_title
:
'
Elasticsearch
connector'
_controller
:
'
\Drupal\search_api_elasticsearchkit_proxy\Controller\ElasticSearchkitProxyController::msearch'
requirements
:
_permission
:
'
access
content'
methods
:
[
'
POST'
]
elastic_searchkit_proxy.elasticsearch_server_selection
:
path
:
'
/admin/config/search/elasticsearch-proxy-server'
...
...
This diff is collapsed.
Click to expand it.
src/Controller/ElasticSearchkitProxyController.php
+
64
−
58
View file @
8f44986a
...
...
@@ -2,47 +2,38 @@
namespace
Drupal\search_api_elasticsearchkit_proxy\Controller
;
use
Drupal\elasticsearch_connector
\Entity\Cluster
;
use
Drupal\elasticsearch_connector\Plugin\search_api
\backend\ElasticSearchBackend
;
use
Drupal\search_api
\ServerInterface
;
use
Elastic\Elasticsearch\Client
;
use
Elastic\Elasticsearch\Response\Elasticsearch
;
use
Http\Promise\Promise
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\Request
;
use
Drupal\Core\Controller\ControllerBase
;
use
Elasticsearch\ClientBuilder
;
use
Drupal\search_api
\Entity\Server
;
/**
* Class ElasticsearchProxyController.
*
* A controller for proxying Elasticsearch search requests through Drupal.
* This class is designed to be extensible and customi
z
able by other modules.
* This class is designed to be extensible and customi
s
able by other modules.
*/
class
ElasticSearchkitProxyController
extends
ControllerBase
{
/**
* Handles the Elasticsearch search request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object containing the Elasticsearch query.
* Initialize the client.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The response object containing the Elasticsearch response or error.
* @throws \Drupal\search_api\SearchApiException
*/
p
ublic
function
search
(
Request
$request
)
{
p
rivate
function
initialize
():
JsonResponse
|
Client
{
$server
=
$this
->
loadSearchApiServer
();
if
(
!
$server
)
{
if
(
!
$server
instanceof
ServerInterface
)
{
return
$this
->
handleServerNotFound
();
}
$client
=
$this
->
initializeElasticsearchClient
(
$server
);
return
$this
->
initializeElasticsearchClient
(
$server
);
try
{
$body
=
$request
->
getContent
();
$response
=
$this
->
performElasticsearchQuery
(
$client
,
$body
);
return
$this
->
handleSuccessResponse
(
$response
);
}
catch
(
\Exception
$e
)
{
return
$this
->
handleErrorResponse
(
$e
);
}
}
/**
...
...
@@ -51,7 +42,7 @@ class ElasticSearchkitProxyController extends ControllerBase {
* @return \Drupal\search_api\ServerInterface|null
* The search API server or null if not found.
*/
protected
function
loadSearchApiServer
()
{
protected
function
loadSearchApiServer
()
:
?ServerInterface
{
$config
=
$this
->
config
(
'search_api_elasticsearchkit_proxy.settings'
);
$server_id
=
$config
->
get
(
'search_api_server'
);
return
Server
::
load
(
$server_id
);
...
...
@@ -63,7 +54,7 @@ class ElasticSearchkitProxyController extends ControllerBase {
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The error response.
*/
protected
function
handleServerNotFound
()
{
protected
function
handleServerNotFound
()
:
JsonResponse
{
return
new
JsonResponse
([
'error'
=>
'Search API server not found.'
],
404
);
}
...
...
@@ -73,67 +64,82 @@ class ElasticSearchkitProxyController extends ControllerBase {
* @param \Drupal\search_api\ServerInterface $server
* The search API server.
*
* @return \Elasticsearch\Client
* @return
\Elastic
\Elasticsearch\Client
* The Elasticsearch client.
*
* @throws \Drupal\search_api\SearchApiException
*/
protected
function
initializeElasticsearchClient
(
$server
)
{
$cluster
=
$this
->
loadElasticsearchCluster
(
$server
);
$clusterOptions
=
$cluster
->
get
(
'options'
);
$host
=
$cluster
->
get
(
'url'
);
$clientBuilder
=
ClientBuilder
::
create
()
->
setHosts
([
$host
]);
if
(
$this
->
isBasicAuthentication
(
$clusterOptions
))
{
$username
=
$clusterOptions
[
'username'
];
$password
=
$clusterOptions
[
'password'
];
$clientBuilder
->
setBasicAuthentication
(
$username
,
$password
);
protected
function
initializeElasticsearchClient
(
ServerInterface
$server
):
Client
{
$serverBackend
=
$server
->
getBackend
();
if
(
!
$serverBackend
instanceof
ElasticSearchBackend
)
{
throw
new
\RuntimeException
(
'Cannot connect to a Search API server that is not an ElasticSearchBackend.'
);
}
return
$
clientBuilder
->
build
();
return
$
serverBackend
->
getClient
();
}
/**
*
Load
s the Elasticsearch
cluster entity
.
*
Handle
s the Elasticsearch
search request
.
*
* @param \
Drupal\search_api\ServerInterface $server
* The
search API serv
er.
* @param \
Symfony\Component\HttpFoundation\Request $request
* The
request object containing the Elasticsearch qu
er
y
.
*
* @return \
Drupal\elasticsearch_connector\Entity\Cluster
* The
Elasticsearch cluster entity
.
* @return \
Symfony\Component\HttpFoundation\JsonResponse
* The
response object containing the Elasticsearch response or error
.
*/
protected
function
loadElasticsearchCluster
(
$server
)
{
$clusterName
=
$server
->
get
(
'backend_config'
)[
'cluster_settings'
][
'cluster'
];
return
Cluster
::
load
(
$clusterName
);
public
function
search
(
Request
$request
):
JsonResponse
{
$client
=
$this
->
initialize
();
try
{
$body
=
$request
->
getContent
();
$response
=
$this
->
performElasticsearchQuery
(
$client
,
$body
);
return
$this
->
handleSuccessResponse
(
$response
->
asArray
());
}
catch
(
\Exception
$e
)
{
return
$this
->
handleErrorResponse
(
$e
);
}
}
/**
*
Checks if the cluster uses Basic Authentication
.
*
Handles the Elasticsearch msearch request
.
*
* @param
array $clusterOptions
* The
cluster options
.
* @param
\Symfony\Component\HttpFoundation\Request $request
* The
request object containing the Elasticsearch query
.
*
* @return
bool
* T
RUE if Basic Authentication is used, FALSE otherwise
.
* @return
\Symfony\Component\HttpFoundation\JsonResponse
* T
he response object containing the Elasticsearch response or error
.
*/
protected
function
isBasicAuthentication
(
array
$clusterOptions
)
{
return
$clusterOptions
[
'authentication_type'
]
===
'Basic'
&&
isset
(
$clusterOptions
[
'username'
])
&&
isset
(
$clusterOptions
[
'password'
]);
public
function
msearch
(
Request
$request
):
JsonResponse
{
$client
=
$this
->
initialize
();
try
{
$body
=
$request
->
getContent
();
$response
=
$this
->
performElasticsearchQuery
(
$client
,
$body
,
'msearch'
);
return
$this
->
handleSuccessResponse
(
$response
->
asArray
());
}
catch
(
\Exception
$e
)
{
return
$this
->
handleErrorResponse
(
$e
);
}
}
/**
* Performs the Elasticsearch query.
*
* @param \Elasticsearch\Client $client
* @param
\Elastic
\Elasticsearch\Client $client
* The Elasticsearch client.
* @param string $body
* The request body containing the Elasticsearch query.
* @param string|null $type
* The type of search.
*
* @return
array
* @return
\Elastic\Elasticsearch\Response\Elasticsearch|\Http\Promise\Promise
* The Elasticsearch response.
*/
protected
function
performElasticsearchQuery
(
$client
,
$body
)
{
return
$client
->
msearch
([
'body'
=>
$body
]);
protected
function
performElasticsearchQuery
(
Client
$client
,
string
$body
,
?string
$type
=
NULL
):
Elasticsearch
|
Promise
{
return
match
(
$type
)
{
'msearch'
=>
$client
->
msearch
([
'body'
=>
$body
]),
default
=>
$client
->
search
([
'body'
=>
$body
]),
};
}
/**
...
...
@@ -145,7 +151,7 @@ class ElasticSearchkitProxyController extends ControllerBase {
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response containing the Elasticsearch results.
*/
protected
function
handleSuccessResponse
(
array
$response
)
{
protected
function
handleSuccessResponse
(
array
$response
)
:
JsonResponse
{
$jsonResponse
=
new
JsonResponse
(
$response
);
$jsonResponse
->
headers
->
set
(
'Access-Control-Allow-Origin'
,
'*'
);
$jsonResponse
->
headers
->
set
(
'Access-Control-Allow-Methods'
,
'GET, POST, OPTIONS'
);
...
...
@@ -163,7 +169,7 @@ class ElasticSearchkitProxyController extends ControllerBase {
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The error response containing the exception message.
*/
protected
function
handleErrorResponse
(
\Exception
$e
)
{
protected
function
handleErrorResponse
(
\Exception
$e
)
:
JsonResponse
{
$errorResponse
=
new
JsonResponse
([
'error'
=>
$e
->
getMessage
()],
500
);
$errorResponse
->
headers
->
set
(
'Access-Control-Allow-Origin'
,
'*'
);
$errorResponse
->
headers
->
set
(
'Access-Control-Allow-Methods'
,
'GET, POST, OPTIONS'
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment