mardi 29 mars 2016

Creating facet checkbox in Symfony with solr

I made a fulltext search, where I can search users for now. I want to build a facet with an checkbox for choosing results. Is there any example how I can do faceted search.

My searchController look like this:

<?php

namespace User\SearchBundle\Controller;


use User\DocumentBundle\Entity\DocumentRepository;
use User\SearchBundle\Helper\Array;
use User\SearchBundle\Helper\User;
use User\UserBundle\Entity\Role;
use User\UserBundle\Entity\RoleRepository;
use User\UserBundle\Entity\User;
use User\UserBundle\Entity\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;


class DefaultController extends BaseController
{

/**
 * @param Request $request
 * @return string
 */
public function searchAction(Request $request)
{
    /** @var \User\SearchBundle\Service\SearchService $service */
    $service = $this->container->get('solr_search');

    $postfixs = [User::POSTFIX => "User"];

    /** get document structure */
    $documentOverall = array_merge(User::$document);

    /** get solr alias document structure */
    $documentAlias = Array::getSolrAlias($documentOverall);
    $searchQuery = "";
    $sources = [];


    $formData = $request->query->all();
    if (!empty($formData['search'])) {
        $searchQuery = trim($formData['search']);
        $searchQuery = strtolower($searchQuery);
    }

    $query = " ";
    $documents = [];

    /** build solr query */
    $query .= " ( {$documentAlias["title"]}:*{$searchQuery}*)";
    $resultSolr = $service->findInIndex($query);

    foreach ($postfixs as $documentPf => $name) {
        foreach ($resultSolr as $doc) {
            if ($documentPf == $doc["document"]) {
                $ids[$documentPf][] = $doc["id"];
            }
        }
    }


    $userResult = [];
    if (!empty($ids[User::POSTFIX])) {
        /** @var UserRepository $repoUser */
        $repoUser = $this->getDoctrine()->getRepository("UserBundle:User");
        $users = $repoUser->getByIds($ids[User::POSTFIX]);



        /** @var \User\UserBundle\Entity\User $user */
        foreach ($users as $user) {
            $userName = [];

            $fullName = $user->getFullName();
            if (!empty($fullName)) {
                $userName[] = "Name: " . $user->getFullName();
            }




            $userResult[$user->getId()] = [
                "userName" => substr(implode(", ", $userName), 0, 200),
            ];


        }
    }

    $resultArray = [];
    foreach ($resultSolr as $solr) {
        switch ($solr["document"]) {

          case UserHelper::POSTFIX:
              if (!empty($userResult[$solr["id"]])) {
                  $userResult[$solr["id"]]["score"] = $solr["score"];
                  $resultArray[] = $userResult[$solr["id"]];
              }
              break;
        }
    }

    return $this->render('SearchBundle:Default:searchResult.html.twig', array(
        'resultArray' => $resultArray,
        'searchQuery' => $searchQuery,
        'linkParam' => $this->generateUrl('searchBundle_searchResultPage', $formData),
        'menu' => Array('top' => 'Search'),
    ));
}

}

I would be thankful if there is any example how to create facet checkbox and how to build a facet query.

Thank you!




Aucun commentaire:

Enregistrer un commentaire