完全匹配SOLR捆绑

时间:2016-12-28 04:16:10

标签: php symfony solr

我正在使用SolrBundle(版本2.2)并尝试获得字段title_tbody_t的完全匹配。这就是我现在所拥有的:

public function findRaw($terms, $max = 10, $offset=0)
{
    $result = [];

    $client = $this->solr->getClient();

    $query = $client->createSelect();

    $termQuery = '';
    foreach ($terms as $term) {
        if ($termQuery != '')
        {
            $termQuery .= 'OR ';
        }
        $termQuery .= 'title_t:*'.urlencode($term).'* OR body_t:*'.urlencode($term).'*';
    }
    $query->setQuery($termQuery);

    $dismax = $query->getDisMax();
    $dismax->setQueryFields('title_t^3 body_t^1');

    $query->setRows($max);
    $query->setStart($offset);

    $query->setFields(array('id','title_t','body_t', 'image_s', 'url_s', 
            'published_date_dt', 'author_s', 'created_at_dt', 'score'));

    $resultset = $client->execute($query);

    foreach ($resultset as $document) {
        $result[] = array(
            "score" => $document->score,
            "id" => $document->id,
            "title" => $document->title_t[0],
            "body" => $document->body_t[0],
            "image" => $document->image_s,
            "url" => $document->url_s,
            "published_date" => $document->published_date_dt,
            "author" => $document->author_s,
            "created_at" => $document->created_at_dt
        );
    }

    return $result;
}

实体档案:

<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use FS\SolrBundle\Doctrine\Annotation as Solr;

/**
 * News
 *
 * @Solr\Document(repository="AppBundle\Repository\NewsRepository")
 * @ORM\Table(name="news", indexes={@ORM\Index(name="fulltext_index",columns={"title","body"},flags={"fulltext"})})
 * @ORM\Entity()
 */
class News
{
    /**
     * @var int
     * @Solr\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var string
     * @ORM\Column(name="md5title", type="string", length=32, unique=true)
     */
    private $md5title;

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

    /**
     * @var string
     * @Solr\Field(type="string")
     * @ORM\Column(name="image", type="string", length=1024, nullable=true)
     */
    private $image;

    /**
     * @var string
     * @Solr\Field(type="string")
     * @ORM\Column(name="url", type="string", length=1024, nullable=true)
     */
    private $url;

    /**
     * @var string
     * @ORM\Column(name="md5url", type="string", length=32, unique=true)
     */
    private $md5url;

    /**
     * @var \DateTime
     * @Solr\Field(type="date")
     * @ORM\Column(name="published_date", type="datetime", nullable=true)
     */
    private $publishedDate;

    /**
     * @var string
     * @Solr\Field(type="string")
     * @ORM\Column(name="author", type="string", length=255, nullable=true)
     */
    private $author;

    /**
     * @var \DateTime
     * @Gedmo\Timestampable(on="create")
     * @Solr\Field(type="date")
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    private $createdAt;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return News
     */
    public function setTitle($title)
    {
        $this->title = $title;
        $this->md5title = md5($title);

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set body
     *
     * @param string $body
     * @return News
     */
    public function setBody($body)
    {
        $this->body = $body;

        return $this;
    }

    /**
     * Get body
     *
     * @return string 
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * Set image
     *
     * @param string $image
     * @return News
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string 
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return News
     */
    public function setUrl($url)
    {
        $this->url = $url;
        $this->md5url = md5($url);

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set published date
     *
     * @param \DateTime $date
     * @return News
     */
    public function setPublishedDate($publishedDate)
    {
        $this->publishedDate = $publishedDate;

        return $this;
    }

    /**
     * Get published date
     *
     * @return \DateTime 
     */
    public function getPublishedDate()
    {
        return $this->publishedDate;
    }

    /**
     * Set author
     *
     * @param string $author
     * @return News
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return string 
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return News
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
}

到目前为止,我已尝试更改实体文件中的注释,并将标题包含在引号中。请让我知道如何解决这个问题。谢谢!

0 个答案:

没有答案