PHP code example of floriansemm / solr-bundle

1. Go to this page and download the library: Download floriansemm/solr-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

floriansemm / solr-bundle example snippets


// ....
use FS\SolrBundle\Doctrine\Annotation as Solr;
    
/**
* @Solr\Document()
* @ORM\Table()
*/
class Post
{
    /**
     * @Solr\Id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
    /**
     * @Solr\Field(type="string")
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title = '';

    /**
     * @Solr\Field(type="string")
     *
     * @ORM\Column(name="text", type="text")
     */
    private $text = '';

   /**
    * @Solr\Field(type="date", getter="format('Y-m-d\TH:i:s.z\Z')")
    *
    * @ORM\Column(name="created_at", type="datetime")
    */
    private $created_at = null;
}

/**
 * @Solr\Document(repository="My/Custom/Repository")
 */
class SomeEntity
{
    // ...
}

/**
 * @Solr\Document(index="core0")
 */
class SomeEntity
{
    // ...
}

/**
 * @Solr\Document(indexHandler="indexHandler")
 */
class SomeEntity
{
    public function indexHandler()
    {
        if ($this->language == 'en') {
            return 'core0';
        }
    }
}

class Post
{
    /**
     * @Solr\Id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    private $id;
}

/**
 * // ....
 * @Solr\SynchronizationFilter(callback="shouldBeIndexed")
 */
class SomeEntity
{
    /**
     * @return boolean
    */
    public function shouldBeIndexed()
    {
        // put your logic here
    }
}

$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post');
$query->addSearchTerm('title', 'my title');
$query->addSearchTerm('collection_field', array('value1', 'value2'));

$result = $query->getResult();

$posts = $this->get('solr.client')->getRepository('AcmeDemoBundle:Post')->findOneBy(array(
    'title' => 'my title',
    'collection_field' => array('value1', 'value2')
));

$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post');
$query->queryAllFields('my title');

$result = $query->getResult();

$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post');
$query->setCustomQuery('id:post_* AND (author_s:Name1 OR author_s:Name2)');

$result = $query->getResult();

$queryBuilder = $this->get('solr.client')->getQueryBuilder('AcmeDemoBundle:Post');
$result = $queryBuilder
    ->where('author')
        ->is('Name1')
    ->orWhere('author')
        ->is('Name2')
    ->getQuery()
    ->getResult();



class YourRepository extends Repository
{
    public function findAuthor($name1, $name2)
    {
        return $this->getQueryBuilder()
            ->where('author')
                ->is($name1)
            ->orWhere('author')
                ->is($name2)
            ->getQuery()
            ->getResult();
    }
}


$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post');
$query->setHydrationMode($mode)

public function find($id)
{
    $this->hydrationMode = HydrationModes::HYDRATE_INDEX;
    
    return parent::find($id);
}

namespace AppBundle\Search;

use FS\SolrBundle\Repository\Repository;

class ProviderRepository extends Repository
{
    public function findPost($what)
    {
        $query = $this->solr->createQuery('AcmeDemoBundle:Post');
        // some query-magic here

        return $query->getResult();
    }
}

$helper = $this->get('solr.client')->getDocumentHelper();
$id = $helper->getLastInsertDocumentId();
 php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FS\SolrBundle\FSSolrBundle(),
    );
}
xml
<tag name="solarium.client.plugin" plugin-name="yourPluginName"/>
xml
<tag name="kernel.event_listener" event="solarium.core.preExecuteRequest" method="preExecuteRequest" />