首页 > 解决方案 > Symfony 3.4 中 mongodb 中的全文搜索

问题描述

我正在尝试在 mongodb 中使用全文搜索:

db.Product.createIndex({"name": "text"})
db.Product.find({$text: {$search: "xxxxx"}})

如何在控制器中使用它来 symfony?

标签: mongodbsymfonydoctrine-odm

解决方案


首先创建产品实体(根据您的需要调整)

<?php

/**
 * @Document
 * @Index(keys={"name"="text"})
 */
class Product
{
    /** @Id */
    public $id;

    /** @Field(type="string") */
    public $name;

    /** @Field(type="float") */
    public $price;
}

查看$name@Index注释

然后使用查询构建器 text() 方法

// Run a text search against the index
$qb = $dm->createQueryBuilder('Product')
    ->text('words you are looking for');

您可以在这里找到更多信息

另一种方法是使用来自学说查询构建器的 expr() 创建本机查询


推荐阅读