13

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.

hsuk
  • 6,770
  • 13
  • 50
  • 80
Kunwar Siddharth Singh
  • 1,676
  • 6
  • 33
  • 66
  • 1
    What bundle are you using to connect and search in solr ? – Artem L Dec 26 '12 at 14:04
  • @ArtemL thanks for comment .I am using solr bundle and for searching nothing because i am new in search with solr. – Kunwar Siddharth Singh Dec 26 '12 at 14:16
  • What bundle you are using exactly? And how? Some cod may help answering the question. – Artem L Dec 26 '12 at 14:17
  • @ArtemL I have edited my question you can see that deps file and i have no any code for search. – Kunwar Siddharth Singh Dec 26 '12 at 14:28
  • Can't see any solr bundle. Can you point out which bundle you are already using with solr? – Artem L Dec 26 '12 at 14:34
  • @ArtemL see this edited question and in project use this with apacheSolr. – Kunwar Siddharth Singh Dec 26 '12 at 14:44
  • You are not using any of Symfony2 bundles. Consider looking in one of these: [KNP Bundles SOLR](http://knpbundles.com/search?q=solr). I'm using [NelmioSolariumBundle](https://github.com/nelmio/NelmioSolariumBundle), but seems it's incompatible with Symfony2.0.x you are using. – Artem L Dec 26 '12 at 14:48
  • Thanks. Any other suggestion because my project totally made in symfon2. – Kunwar Siddharth Singh Dec 26 '12 at 14:53
  • @ArtemL you can see my controller.I know this is wrong but may be this is helpful for you. – Kunwar Siddharth Singh Dec 26 '12 at 15:01
  • @KunwarSiddharthSingh Have you considered using ElasticSearch? Check out: http://stackoverflow.com/questions/10213009/solr-vs-elasticsearch for a comparison. If you opt for it, check out here for a tutorial on how to integrate with Symfony: http://richardmiller.co.uk/2011/11/11/symfony2-integrating-elasticsearch/ – GordyD Jan 16 '13 at 15:08
  • As mentioned by @ArtemL, the Solarium Bundle is the best option. I don't see why it wouldn't work with Symfony2.0 though. You would just have to install it like anything else in Symfony2.0, by modifying the deps, autoload, and AppKernel files. – Squazic Jan 22 '13 at 14:47

4 Answers4

2

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

julien rollin
  • 1,607
  • 1
  • 12
  • 17
  • I have solarium set up and I am able to make queries as shown however I am struggling to try and index my documents. I have a Symfony2 annotated object and I have a method toSolrDocument(\Solarium_Document_ReadWrite $doc) to translate the document as shown in Xavier Briand's presentation. However, when i call it like so foreach ($sheets as $sheet) { $documents[] = $sheet->toSolrDocument($update->createDocument()); } I get 'Solarium_Document_ReadWrite' not found any idea how to resolve this issue? – Scott Sherwood Mar 16 '13 at 17:47
  • It's weird, Are you sure your solarium lib is in your vendor lib ? Make sure you use solarium 2.4.x, last release 3.x uses php 5.3 namespaces – julien rollin Mar 17 '13 at 19:43
  • I resolved the issue by removing the requirement for the readwrite class. However, I have a new issue having multiple cores. I have set up multiple cores in my solr example installation and added the core i require to my symfony2 config however, somehow the default core I am getting access to is the one specified in the sorl.xml file under the defaultCoreName property. Any ideas? – Scott Sherwood Mar 17 '13 at 23:25
  • 1
    please open a new question, it is not related to the searchbox question. Thanks – julien rollin Mar 18 '13 at 13:53
0

Try something like this..

Controlloer.php:

public function searchAction($templateName = '')
{
    // ------
    // ------

    $resultset = $solrObject->offsetGet('response');

    // Do some error checks

    return array('resultset'=>$resultset);

}

twig:

<ul>
{% for doc in resultset %}
  <li> {{ doc.name }} </li>
{% endfor %}
</ul>
Tobias Nyholm
  • 1,142
  • 9
  • 16
0

I still have not tried, but you can try use this new Jobeet tutorial in Symfony2 that explains how to build a system and also how to use Solr in it.
I hope this help.

0

You get GET-parameters via

$searchName = $request->query->get('search_name');

But for this you still need the $request variable. You can use it as parameter and change your method signature to this:

public function anagraficaAction(Request $request)

This way, you can call the $request parameter in your method.

The other way is to get the request of the current controller inside your method.

$request = $this->get('request');

Using this you can change your setParameter to this:

setParameter('nome', $searchName)