我正在尝试在mongodb中使用全文搜索:
db.Product.createIndex({"name": "text"})
db.Product.find({$text: {$search: "xxxxx"}})
如何在控制器中使用它来进行symfony?
答案 0 :(得分:0)
首先创建产品实体(根据您的需要进行调整)
TIdHTTP
查看<?php
/**
* @Document
* @Index(keys={"name"="text"})
*/
class Product
{
/** @Id */
public $id;
/** @Field(type="string") */
public $name;
/** @Field(type="float") */
public $price;
}
和$name
注释
然后使用查询生成器text()方法
@Index
更多信息,您可以找到here
另一种方法是从学说查询构建器使用expr()创建本机查询
答案 1 :(得分:0)
感谢您提供所有答案。总而言之,搜索引擎的控制器如下所示:
class SearchController extends Controller
{
public function searchBarAction()
{
$form = $this->createFormBuilder(null)
->setMethod('GET')
->add('search', TextType::class)
->getForm();
return $this->render('AppBundle:Components:_searchBar.html.twig', [
'form' => $form->createView()
]);
}
/**
* @param Request $request
*/
public function handleSearchAction(Request $request)
{
$searchData = $request->query->get('form')['search'];
$dbName = 'ece';
$connection = $this->container->get('doctrine_mongodb')->getConnection();
$mongo = $connection->getMongo();
$db = $mongo->selectDB($dbName);
$resultSetProduct = $db->Product->find([
'$text' => ['$search' => $searchData]
]);
$resultSet = $db->MainData->find([
'$text' => ['$search' => $searchData]
]);
$itemProduct = $resultSetProduct->count();
$itemSet = $resultSet->count() + $itemProduct;
return $this->render('search/index.html.twig', [
'searchData' => $searchData,
'resultSetProduct' => $resultSetProduct,
'itemProduct' => $itemProduct,
'itemSet' => $itemSet,
'resultSet' => $resultSet
]);
}
}