FriendsOfSymfony / FOSElasticaBundle

Elasticsearch PHP integration for your Symfony project using Elastica.
http://friendsofsymfony.github.io
MIT License
1.25k stars 788 forks source link

FOSElasticaBundle : impossible to find results #346

Closed GBCVisible closed 10 years ago

GBCVisible commented 10 years ago

Hi,

My problem is the implementation of a search engine job with ElasticaSearch.

I use FOSElasticaBundle 3.0.* with Symfony 2.3.2 My Config :

#\app\config\config.yml

#Config Elasticsearch
fos_elastica:
    clients:
        default: { host: localhost, port: 8080 }
    indexes:
        website:
            client: default
            types:
                Job:
                    mappings:
                        Title: { boost: 5 }
                        Location: { type: geo_point, boost: 3, lat_lon: true }
                    persistence:
                        driver: orm # Le mode de stockage des données à indexer  (orm, mongodb, propel)
                        model: Xxxx\YyyyBundle\Entity\Job # L'entité sur laquelle porte le mapping défini ci-dessus
                        provider: ~
                        finder: ~ # Indique que l'on va utiliser un finder (voir la doc officielle pour plus d'infos)
                        listener:  # Indique que l'on va utiliser un listener sur l'entité pour la MàJ de l'index

My Controller :

public function SearchAction()
{

//Récupération du formulaire/*
$SearchJob_entity = new SearchJob();
    $SearchJob_form = $this->createForm(new SearchJobType(), $SearchJob_entity);
    $request = $this->getRequest();
    $SearchJob_form->handleRequest($request);

    $Job = $SearchJob_form['JobSearch']->getData();
    $Where = $SearchJob_form['WhereSearch']->getData();

    $StringSearch = $Job . " / " . $Where;

//Job
    $finder = $this->container->get('fos_elastica.finder.website.job');

    $Data = $finder->find($Job);

    $StringSearch = $Job . " / " . $Where . " / " . var_dump($Data);

    return $this->render('CViJobBundle::JobResult.html.twig', array(
                'StringSearch'      => $StringSearch,
                ));
}

The command "php app/console fos:elastica:populate" it's OK.

But, don't find results, even one that matches exactly one title in this BDD.

One idea ?

Thanhs

lonamiaec commented 10 years ago

Hi @GBCVisible, i think your controller is incomplete. You are passing the text you are searching directly to the finder but you are not specifying where to look. I write here what i think you missed

use Elastica\Query\Bool;
use Elastica\Query\Text;
......
$boolQuery = new Bool();
$jobQuery = new Text();
$jobQuery->setFieldQuery('website.Job.Title', $Job);
$boolQuery->addMust($jobQuery); 
.....
$Data = $finder->find($boolQuery);

Try it and tell us!

Hendra-Huang commented 10 years ago

i have the same problem here... i have tested @lonamiaec solution, but it still comes with no result.. somebody please help...

lonamiaec commented 10 years ago

Hi @Hendra-Huang paste your code here and we'll try to help you

Hendra-Huang commented 10 years ago

My config :

fos_elastica:
    clients:
        default: { host: localhost, port: 80 }
    indexes:
        website:
            client: default
            types:
                book:
                    mappings:
                        title: { boost: 3 }
                    persistence:
                        driver: orm
                        model: xxxx\xxxBundle\Entity\Book
                        provider:
                            query_builder_method: populateElasticSearch
                        identifier: id
                        listener: 
                        finder: 

My BookRepository :

    public function populateElasticSearch()
    {
        $em = $this->getEntityManager();

        return $em->createQueryBuilder()
            ->select('b')
            ->from('xxxBundle:Book', 'b')
            ->orderBy('b.id');
    }

My BookController :

    public function searchAction()
    {
        $finder = $this->container->get('fos_elastica.finder.website.book');
        $request = $this->container->get('request');
        $queryString = $this->getRequest()->query->get('query');
        $boolQuery = new Bool();
        $bookQuery = new Text();
        $bookQuery->setFieldQuery('title', $queryString);
        $boolQuery->addMust($bookQuery); 

        $paginator = $finder->findPaginated($boolQuery);
        $paginator->setCurrentPage($request->get('page', 1), false, true );

        return $this->render('xxxBundle:Book:search.html.twig', array(
                'paginator' => $paginator
            )
        );
    }

Is there anything wrong @lonamiaec ?

lonamiaec commented 10 years ago

Have you seen $queryStrging content and is it ok? After that, have you search this term directly in elasticsearch? If you get the expected results, can you debug $paginator to see its value before return? maybe elasticsearch is returning some kind of error

Hendra-Huang commented 10 years ago

$queryString contains the words that going to be search.. the $paginator->getNbResults() returns 0... i never tried to search this term directly.. can you show me the way to search this term directly in elasticsearch ?

lonamiaec commented 10 years ago

@Hendra-Huang http://mobz.github.io/elasticsearch-head/ is a plugin to enable elasticsearch access from the browser. There you can create an structured query with your parameters, and see the search result. I hope this can help you!

Hendra-Huang commented 10 years ago

okay.. it's working now.. i think the problem is the port.. i reinstall elasticsearch and config the port to 9200 and it's working... thx @lonamiaec for your help.. ^.^