Use "WITH PARSER" clause for fulltext index definition #5755

Closed
opened 2026-01-22 15:16:49 +01:00 by admin · 6 comments
Owner

Originally created by @kissge on GitHub (Nov 1, 2017).

Originally assigned to: @Ocramius on GitHub.

Is it possible to use WITH PARSER on index definition? e.g. CREATE FULLTEXT INDEX body_idx ON article (body) WITH PARSER mecab;

cf. https://stackoverflow.com/questions/46812438/doctrine-use-with-parser-clause-for-fulltext-index-definition

Originally created by @kissge on GitHub (Nov 1, 2017). Originally assigned to: @Ocramius on GitHub. Is it possible to use WITH PARSER on index definition? e.g. `CREATE FULLTEXT INDEX body_idx ON article (body) WITH PARSER mecab;` cf. https://stackoverflow.com/questions/46812438/doctrine-use-with-parser-clause-for-fulltext-index-definition
admin added the Question label 2026-01-22 15:16:49 +01:00
admin closed this issue 2026-01-22 15:16:50 +01:00
Author
Owner

@Ocramius commented on GitHub (Nov 1, 2017):

You'd do that in an SQL migration rather than via ORM mappings, in my
opinion.

On 1 Nov 2017 05:34, "kissge" notifications@github.com wrote:

Is it possible to use WITH PARSER on index definition? e.g. CREATE
FULLTEXT INDEX body_idx ON article (body) WITH PARSER mecab;

cf. https://stackoverflow.com/questions/46812438/doctrine-
use-with-parser-clause-for-fulltext-index-definition


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6804, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakHRYn3rxAzLB_QCYKYEuRSnFzjyJks5sx_TCgaJpZM4QNvWr
.

@Ocramius commented on GitHub (Nov 1, 2017): You'd do that in an SQL migration rather than via ORM mappings, in my opinion. On 1 Nov 2017 05:34, "kissge" <notifications@github.com> wrote: > Is it possible to use WITH PARSER on index definition? e.g. CREATE > FULLTEXT INDEX body_idx ON article (body) WITH PARSER mecab; > > cf. https://stackoverflow.com/questions/46812438/doctrine- > use-with-parser-clause-for-fulltext-index-definition > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6804>, or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakHRYn3rxAzLB_QCYKYEuRSnFzjyJks5sx_TCgaJpZM4QNvWr> > . >
Author
Owner

@kissge commented on GitHub (Nov 2, 2017):

Hmm. So, in projects using WITH PARSER fulltext index, basically doctrine:schema:create would be meaningless. I'd like some cleverer ideas :)

@kissge commented on GitHub (Nov 2, 2017): Hmm. So, in projects using WITH PARSER fulltext index, basically `doctrine:schema:create` would be meaningless. I'd like some cleverer ideas :)
Author
Owner

@Ocramius commented on GitHub (Nov 2, 2017):

I think "cleverer" is pretty much the keyword. Not everything should be
funneled through the schema tools. For instance, I have triggers, stored
procedures, index and view definitions that are not representable in ORM
mapping definitions.

If you go down the "represent all possible DDL in the ORM" route, it is a
never ending rabbit hole.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On Thu, Nov 2, 2017 at 7:27 AM, kissge notifications@github.com wrote:

Hmm. So, in projects using WITH PARSER fulltext index, basically
doctrine:schema:create would be meaningless. I'd like some cleverer ideas
:)


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6804#issuecomment-341329325,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakF497yLHzG3CzFfbbp89n867YPNZks5syWDLgaJpZM4QNvWr
.

@Ocramius commented on GitHub (Nov 2, 2017): I think "cleverer" is pretty much the keyword. Not everything should be funneled through the schema tools. For instance, I have triggers, stored procedures, index and view definitions that are not representable in ORM mapping definitions. If you go down the "represent all possible DDL in the ORM" route, it is a never ending rabbit hole. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Thu, Nov 2, 2017 at 7:27 AM, kissge <notifications@github.com> wrote: > Hmm. So, in projects using WITH PARSER fulltext index, basically > doctrine:schema:create would be meaningless. I'd like some cleverer ideas > :) > > — > You are receiving this because you commented. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6804#issuecomment-341329325>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakF497yLHzG3CzFfbbp89n867YPNZks5syWDLgaJpZM4QNvWr> > . >
Author
Owner

@kissge commented on GitHub (Nov 3, 2017):

Thanks Marco... I understand what you mean.
If anyone knows any "hack" to this problem, I'd appreciate it.

@kissge commented on GitHub (Nov 3, 2017): Thanks Marco... I understand what you mean. If anyone knows any "hack" to this problem, I'd appreciate it.
Author
Owner

@lcobucci commented on GitHub (Nov 26, 2017):

I'll close this issue since it has been answered by @Ocramius. Please re-open it if you feel the need of discussing it further.

@lcobucci commented on GitHub (Nov 26, 2017): I'll close this issue since it has been answered by @Ocramius. Please re-open it if you feel the need of discussing it further.
Author
Owner

@zjsxwc commented on GitHub (Jul 24, 2018):

i have the same problem. And i tried the migration way, but every time after i run update --forcethe database index is revert back to the index which *WITHOUT* PARSER ngram.

so my solution is to write a new Driver(which extends the default driver \Doctrine\DBAL\Driver\PDOMySql\Driver) that is configured in symfony file app/config/config.yml to replace the default driver.

doctrine:
    dbal:
        driver_class: Common\Doctrine\PdoMysqlDriver

my new driver

<?php

namespace Common\Doctrine;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDOConnection;
use PDOException;

/**
 * PDO MySql driver.
 *
 * @since 2.0
 */
class PdoMysqlDriver extends \Doctrine\DBAL\Driver\PDOMySql\Driver
{

    /**
     * {@inheritdoc}
     */
    public function createDatabasePlatformForVersion($version)
    {
        if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
            throw DBALException::invalidPlatformVersionSpecified(
                $version,
                '<major_version>.<minor_version>.<patch_version>'
            );
        }

        if (false !== stripos($version, 'mariadb')) {
            return $this->getDatabasePlatform();
        }

        $majorVersion = $versionParts['major'];
        $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
        $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : 0;
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;

        if (version_compare($version, '5.7', '>=')) {
            return new \Common\Doctrine\MySQL57FixPlatform();
        }

        return $this->getDatabasePlatform();
    }

}

my new MySQL57FixPlatform

<?php
namespace Common\Doctrine;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff;

/**
 * Provides the behavior, features and SQL dialect of the MySQL 5.7 database platform.
 *
 * @author Steve Müller <st.mueller@dzh-online.de>
 * @link   www.doctrine-project.org
 * @since  2.5
 */
class MySQL57FixPlatform extends \Doctrine\DBAL\Platforms\MySQL57Platform
{
    /**
     * Whether the platform supports partial indexes.
     *
     * @return boolean
     */
    public function supportsPartialIndexes()
    {
        return true;
    }


    /**
     * Adds condition for partial index.
     *
     * @param \Doctrine\DBAL\Schema\Index $index
     *
     * @return string
     */
    protected function getPartialIndexSQL(Index $index)
    {
        $retArr = [""];
        if ($this->supportsPartialIndexes()) {
            if ($index->hasOption('where')) {
                $retArr[] = ' WHERE ' . $index->getOption('where');
            }
            if ($index->hasOption('with')) {
                $retArr[] = ' WITH ' . $index->getOption('with');
            }
        }

        return implode("", $retArr);
    }
}


And finally in my Entity file add the with options:

/**
 * Item
 *
 * @ORM\Table(name="item", indexes={
 *     @ORM\Index(name="ft_title_idx", columns={"title"}, flags={"fulltext"}, options={"with"="PARSER ngram"}),
 * })
 * @ORM\Entity(repositoryClass="ItemBundle\Repository\ItemRepository")
 */
class Item
{
}
@zjsxwc commented on GitHub (Jul 24, 2018): i have the same problem. And i tried the migration way, but every time after i run `update --force`the database index is revert back to the index which `*WITHOUT* PARSER ngram`. so my solution is to write a new Driver(which extends the default driver \Doctrine\DBAL\Driver\PDOMySql\Driver) that is configured in symfony file `app/config/config.yml` to replace the default driver. ``` doctrine: dbal: driver_class: Common\Doctrine\PdoMysqlDriver ``` my new driver ``` <?php namespace Common\Doctrine; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\PDOConnection; use PDOException; /** * PDO MySql driver. * * @since 2.0 */ class PdoMysqlDriver extends \Doctrine\DBAL\Driver\PDOMySql\Driver { /** * {@inheritdoc} */ public function createDatabasePlatformForVersion($version) { if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { throw DBALException::invalidPlatformVersionSpecified( $version, '<major_version>.<minor_version>.<patch_version>' ); } if (false !== stripos($version, 'mariadb')) { return $this->getDatabasePlatform(); } $majorVersion = $versionParts['major']; $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0; $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : 0; $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; if (version_compare($version, '5.7', '>=')) { return new \Common\Doctrine\MySQL57FixPlatform(); } return $this->getDatabasePlatform(); } } ``` my new MySQL57FixPlatform ``` <?php namespace Common\Doctrine; use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\TableDiff; /** * Provides the behavior, features and SQL dialect of the MySQL 5.7 database platform. * * @author Steve Müller <st.mueller@dzh-online.de> * @link www.doctrine-project.org * @since 2.5 */ class MySQL57FixPlatform extends \Doctrine\DBAL\Platforms\MySQL57Platform { /** * Whether the platform supports partial indexes. * * @return boolean */ public function supportsPartialIndexes() { return true; } /** * Adds condition for partial index. * * @param \Doctrine\DBAL\Schema\Index $index * * @return string */ protected function getPartialIndexSQL(Index $index) { $retArr = [""]; if ($this->supportsPartialIndexes()) { if ($index->hasOption('where')) { $retArr[] = ' WHERE ' . $index->getOption('where'); } if ($index->hasOption('with')) { $retArr[] = ' WITH ' . $index->getOption('with'); } } return implode("", $retArr); } } ``` And finally in my Entity file add the `with` options: ``` /** * Item * * @ORM\Table(name="item", indexes={ * @ORM\Index(name="ft_title_idx", columns={"title"}, flags={"fulltext"}, options={"with"="PARSER ngram"}), * }) * @ORM\Entity(repositoryClass="ItemBundle\Repository\ItemRepository") */ class Item { } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5755