3
votes

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!!

2

2 Answers

3
votes

You need to set the host to the elasticsearch docker container.
Here localhost refers to php-fpm container as your code is executed in it.

So the configuration file should be:

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

Did you solve your problem ?

You forgot to add the container name like this container_name: elasticsearch And to resume, your will have :

 elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.4.2
        container_name: elasticsearch
        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