I started to work on elasticsearch 2 days ago. everything is ok to index and send documents on elasticsearch server. But i'm unable to query them with elastica/ruflin client.
I'm working on Symfony 4 and i want to implement search bar to find articles.
Don't understand what's wrong because i've created a command to populate index and it works. I can make a query on kibana or on localhost:9200/{index}/_search.
I'm running this with docker, webserver is nginx and using php-fpm-7.2,
When i try to make a query error is : Couldn't connect to host, Elasticsearch down?
Here is the code:
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
Thank you in advance!!