Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Box in Symfony2 with Solr

I want to create Search box in front page of my website but I don't know how to create one using Solr. All my website is developed in Symfony2. I don't use Database for searching.

How I can do this?.

In my project Solr bundle use this:

use SolrClient;
use SolrQuery;
use SolrObject;
use SolrDocument;
use SolrInputDocument;

In base twig file :

<form action="{{ path("home_search") }}" method="get">
  <input type="search" name="search"><br>
  <input type="submit" value="search">
</form>

Some example of my controller:

public function searchAction($templateName = '')
{
 $solrService = $this->get('rocket.solr_service');

    $solrQuery = new SolrQuery('*:*');
    $solrQuery->addField('id')
        ->addField('name');


    if (!empty($templateName)) {
        $solrQuery->addFilterQuery(sprintf('name:"%s" OR design_template_tag_name:"%1$s" OR design_category_name:"%1$s"',
                                           $templateName));
    }


    $solrQuery->setRows(1000);

    $solrObject = $solrService->query(
        'RocketBraPrintBundle:DesignTemplate',
        $solrQuery,
        SolrService::WRITER_FORMAT_SOLR_OBJECT
    );

    $templates = $solrObject->offsetGet('response')->offsetGet('docs');
    if (!$templates) {

        if (!empty($templateName)) {
            $this->setFlash('catalog-message', 'No results found for your search.');
            return $this->searchDesignTemplates($categoryTreeSlug,
                                                $productFamilyFaceId);
        }

        return array();
    }

    return $templates;

}

But in twig file where this is render I don't know what I will write.

like image 937
Kunwar Siddharth Singh Avatar asked Dec 26 '12 13:12

Kunwar Siddharth Singh


1 Answers

you can use https://github.com/nelmio/NelmioSolariumBundle with use solarium implementation

Really great for better solr query

i already use it

here a sample

    $query = 'foo';

    $page = 1;
    if (array_key_exists('page', $params)) {
        $page = (int) $params['page'];
    }
    $rows = 10;
    if (array_key_exists('limit', $params)) {
        $rows = (int) $params['limit'];
    }

    $solarium = $this->get('solarium.client');

    //select
    $select = $solarium->createSelect();
    $escapedQuery = $select->getHelper()->escapePhrase($query);

    //dismax
    $dismax = $select->getDisMax();
    // override the default setting of 'dismax' to enable 'edismax'
    $dismax->setQueryParser('edismax');
    //fields
    $dismax->setQueryFields(
                    array('title^5','description^0.7'));

    $select->setQuery($escapedQuery);

    //limit
    $select->setRows($rows);
    $select->setStart(($page - 1) * $rows);

    //type spot only
    $select->createFilterQuery('typeFilter')
            ->setQuery(sprintf('type:%s', 'spot'));

    $resultset = $solarium->select($select);

use foreach or twig "for in" to display your solr doc

like image 176
Julien Rollin Avatar answered Oct 23 '22 18:10

Julien Rollin