diff --git a/docs/en/cookbook/simple-search-engine.rst b/docs/en/cookbook/simple-search-engine.rst index 4ae2b1aeb..2dc412f78 100644 --- a/docs/en/cookbook/simple-search-engine.rst +++ b/docs/en/cookbook/simple-search-engine.rst @@ -16,22 +16,24 @@ setup a document like the following with a ``$keywords`` property that is mapped */ #[Field(type: 'collection')] #[Index] - private $keywords = []; + public Collection $keywords; - // ... + public function __construct() + { + $this->keywords = new ArrayCollection(); + } } Working with Keywords @@ -44,12 +46,12 @@ Now, create a product and add some keywords: setTitle('Nike Air Jordan 2011'); - $product->addKeyword('nike shoes'); - $product->addKeyword('jordan shoes'); - $product->addKeyword('air jordan'); - $product->addKeyword('shoes'); - $product->addKeyword('2011'); + $product->title = 'Nike Air Jordan 2011'; + $product->keywords->add('nike shoes'); + $product->keywords->add('jordan shoes'); + $product->keywords->add('air jordan'); + $product->keywords->add('shoes'); + $product->keywords->add('2011'); $dm->persist($product); $dm->flush(); @@ -106,8 +108,9 @@ the results to your query. Here is an example: Embedded Documents ------------------ -If you want to use an embedded document instead of just an array then you can. It will allow you to store -additional information with each keyword, like its weight. +If you want to use an embedded document instead of just an array then you can. +That will allow you to store additional information with each keyword, like its +weight. Definition ~~~~~~~~~~ @@ -121,20 +124,14 @@ You can setup a ``Keyword`` document like the following: #[EmbeddedDocument] class Keyword { - #[Field(type: 'string')] - #[Index] - private $keyword; - - #[Field(type: 'int')] - private $weight; - - public function __construct(string $keyword, int $weight) - { - $this->keyword = $keyword; - $this->weight = $weight; - } - - // ... + public function __construct( + #[Field(type: 'string')] + #[Index] + public $keyword, + + #[Field(type: 'int')] + public $weight, + ) {} } Now you can embed the ``Keyword`` document many times in the ``Product``: @@ -150,8 +147,9 @@ Now you can embed the ``Keyword`` document many times in the ``Product``: { // ... + /** @var Collection */ #[EmbedMany(targetDocument: Keyword::class)] - private $keywords; + public Collection $keywords; // ... } @@ -163,10 +161,12 @@ you would have to do the following: addKeyword(new Keyword('nike shoes', 1)); + $product->keywords->add(new Keyword('nike shoes', 1)); -This is a very basic search engine example and can work for many small and simple applications. If you -need better searching functionality you can look at integrating something like `Solr`_ in your project. +This is a very basic search engine example and can work for many small and +simple applications. If you need better searching functionality you can look at +`MongoDB Atlas Search`_, read more about the +:doc:`Search Indexes ` .. _AlchemyAPI: http://www.alchemyapi.com -.. _Solr: http://lucene.apache.org/solr +.. _MongoDB Atlas Search: https://www.mongodb.com/products/platform/atlas-search