floriansemm / SolrBundle

Solr-Integration into Symfony and Doctrine2
http://floriansemm.github.io/SolrBundle
MIT License
123 stars 73 forks source link

Adding a Search Term Returns Nothing #133

Closed xpersonas closed 6 years ago

xpersonas commented 8 years ago

If I simply get the solr.client and getResult(), I can return all of my post entities. But adding any kind of search to the query returns nothing. I've been trying every style and example I can find in your doc. Can you think of anything or help in any way to help me understand why I seem to have a full SOLR index, but can't actually search for any terms?

Returns All Posts:

        $query = $this->get('solr.client')->createQuery('XpersonasBlogBundle:Post');
        $result = $query->getResult();

Returns Nothing:

        $query = $this->get('solr.client')->createQuery('XpersonasBlogBundle:Post');
        $query->addSearchTerm('title', 'deer');

        $result = $query->getResult();

Additional Details:

class SearchController extends Controller
{
    public function indexAction()
    {
        $query = $this->get('solr.client')->createQuery('XpersonasBlogBundle:Post');
        $query->addSearchTerm('title', 'deer');
        //$query->setHydrationMode(HydrationModes::HYDRATE_INDEX);
        //$query->addField('id');

        $result = $query->getResult();

        return $this->render('AppBundle:Search:index.html.twig', array(
            'result' => $result
        ));
    }

}
namespace Xpersonas\BlogBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use FS\SolrBundle\Doctrine\Annotation as Solr;

/**
 * Post
 *
 * @Solr\Document()
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="Xpersonas\BlogBundle\Repository\PostRepository")
 * @ORM\HasLifecycleCallbacks()
 * @Vich\Uploadable
 */
class Post
{
    /**
     * @var int
     *
     * @Solr\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @Solr\Field(type="string")
     * @ORM\Column(name="title", type="string", length=255)
     * @Assert\NotBlank(message="This field is required.")
     */
    private $title;
xpersonas commented 8 years ago

I've finally managed to at least see why I'm getting no results. I looked at a select request from SOLR and all that I have is...

http://127.0.0.1:30000/solr/select returns....

<response>
<result name="response" numFound="20" start="0">
<doc>
<str name="id">post_1</str>
<long name="_version_">1543672402808930304</long>
<date name="timestamp">2016-08-25T21:32:03.433Z</date>
</doc>
...

So I guess my solr index is not populating correctly. Though I'm not sure and have no idea why.

But this works as a proof of concept I guess...

$query->addSearchTerm('id', 'post_1');

Sorry I'm so green with this. But I'm trying to work my way through it.

floriansemm commented 8 years ago

Run solr:index:populate to synchronize your DB and Solr.

xpersonas commented 8 years ago

Yes I did. I can clear it and populate it. But if I look at the index through the SOLR admin at /solr/select all I see is this...

<response>
<result name="response" numFound="20" start="0">
<doc>
<str name="id">post_1</str>
<long name="_version_">1543672402808930304</long>
<date name="timestamp">2016-08-25T21:32:03.433Z</date>
</doc>
...

I can search "post_1" and get the result. But I can't search anything like a title or entity field.

floriansemm commented 8 years ago

what does a complete post-document look like? Your example contains only the fields id and timestamp, a title field is completely missing.

xpersonas commented 8 years ago

Right. That's the problem, and that's the hard part for me to debug. Here's what it looks like when I run the solr:scheme:show command:

screenshot at aug 30 07-48-29

Clearly I should have a title and description in the index.

screenshot at aug 30 07-51-59

But when I look at /solr/select in the Solr admin interface, I only see id and timestamp. I'm trying to figure out how to work through this issue from here.