Cannot create index Solr



I'm usings symfony 2.5 and I'm trying to create index of solr with solarium. For this, i had created an index console command. But, i have two problems the first, the script can't recognize the Doctrine's service it's weird because I use it with no problem in my controllers. And the second, it can't create the document. This is my code :



<?php
namespace Ads\SolrBundle\Command;

use Ads\ManagerBundle\Entity\Post as Post;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Solarium\QueryType\Select\Result\AbstractDocument;

class IndexAdsCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('solr:index')
->setDescription('Indexes Ads in Solr')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->getContainer()->get('solarium.client');
$doctrine = $this->getContainer()->get('doctrine');

// get an update query instance
$update = $client->createUpdate();

$em = $doctrine->getManager();
$query = $em->createQuery("SELECT COUNT(p.id) fROM AdsManagerBundle:Post p");
$nber = $query->getResult();

// create documents
$id = 0;
while($id<$nber)
{
$post = $doctrine->getManager()->getRepository('AdsManagerBundle:Post')->find($id);
try
{
$document = $update->createDocument();
$this->updateDocumentFromPost($document, $post);
$update->addDocument($document);
}
catch (\Exception $e) {
$output->writeln('Exception: '.$e->getMessage().', post '.$post->getId().' non indexe');
}
$id++;
}

// add the documents and a commit command to the update query
$update->addCommit();

// this executes the query and returns the result
$result = $client->update($update);
$output->writeln('index réalisé à succes !!');
}

private function updateDocumentFromPost(AbstractDocument $document, Post $post)
{
$document->setField('id', $post->getId());
$document->setField('nom', $post->getNom());
$document->setField('contenu', $post->getContenu());
$document->setField('categorie', $post->getCategorie());
$document->setField('region', $post->getRegion());
$document->setField('ville', $post->getVille());
$document->setField('code_postal', $post->getCodePostal());
$document->setField('contenu', $package->getContenu());
}
}


my schema.xml :



<?xml version="1.0" encoding="UTF-8" ?>
<schema name="packagist" version="1.4">
<fields>
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="nom" type="text_general_rev" indexed="true" stored="true"/>
<field name="contenu" type="text_general_rev" indexed="true" stored="true"/>
<field name="categorie" type="text_general_rev" indexed="true" stored="true"/>
<field name="region" type="text_general_rev" indexed="true" stored="true"/>
<field name="ville" type="float" indexed="true" stored="true" />
<field name="code_postal" type="string" indexed="false" stored="true" />
</fields>


<uniqueKey>id</uniqueKey>
<defaultSearchField>text</defaultSearchField>
<solrQueryParser defaultOperator="OR"/>
<copyField source="nom" dest="text"/>
<copyField source="contenu" dest="text"/>
</schema>


Thank you for your answers.


No comments:

Post a Comment