dongyongju9560 2016-12-28 04:16
浏览 40

完全匹配SOLR捆绑

I'm using the SolrBundle (version 2.2) and trying to get an exact match of the field title_t or body_t. Here's what I have right now:

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;
}

Entity file:

<?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;
    }
}

So far I've tried changing the annotations in the entities file, and encasing the title in quotes. Please let me know how to fix this. Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度