gpt4 book ai didi

symfony - 无法连接到主机,elasticsearch 关闭? Symfony 4 elastica/rulfin 客户端。 docker

转载 作者:行者123 更新时间:2023-12-02 18:21:06 25 4
gpt4 key购买 nike

2天前开始做elasticsearch。一切都可以在 elasticsearch 服务器上索引和发送文档。但我无法使用 elastica/ruflin 客户端查询它们。

我正在开发 Symfony 4,我想实现搜索栏来查找文章。

不明白哪里出了问题,因为我已经创建了一个命令来填充索引并且它有效。我可以在 kibana 或 localhost:9200/{index}/_search 上进行查询。

我用 docker 运行它,网络服务器是 nginx 并使用 php-fpm-7.2,

当我尝试进行查询时出现错误:无法连接到主机,Elasticsearch 已关闭?

代码如下:

docker-compose.yml:

webserver:
image: nginx:alpine
container_name: docker-symfony4-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"

php-fpm:
build: phpdocker/php-fpm
container_name: docker-symfony4-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.4.2
environment:
- cluster.name=demo
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- transport.host=127.0.0.1
ulimits:
memlock:
soft: -1
hard: -1
ports:
- 9200:9200

Controller :

/**
* @Route("/_search", methods={"GET"}, name="blog_search")
*/
public function search(Request $request, Client $client): Response
{

if (!$request->isXmlHttpRequest()) {

return $this->render('blog/search.html.twig');
}

$query = $request->query->get('q', '');
$limit = $request->query->get('l', 10);

$match = new MultiMatch();
$match->setQuery($query);
$match->setFields(["title^4", "summary", "content", "author"]);

$bool = new BoolQuery();
$bool->addMust($match);

$elasticaQuery = new Query($bool);
$elasticaQuery->setSize($limit);


$foundPosts = $client->getIndex('blog')->search($elasticaQuery);
$results = [];
foreach ($foundPosts as $post) {
$results[] = $post->getSource();
}

return $this->json($results);
}

services.yaml:

Elastica\Client:
$config:
host: localhost

提前致谢!

最佳答案

需要设置host为elasticsearch docker容器。
这里 localhost 指的是 php-fpm 容器,因为您的代码在其中执行。

所以配置文件应该是:

Elastica\Client:
$config:
host: elasticsearch
port: 9200

关于symfony - 无法连接到主机,elasticsearch 关闭? Symfony 4 elastica/rulfin 客户端。 docker ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com