Skip to content

Commit e40c8b1

Browse files
authored
doc: Review Simple Search Engine Cookbook (doctrine#2659)
This cookbook is not very clear about its objective. At the end, it explains how to add weight to keywords without explaining how to use them. I'm just renovating the code and replacing Solr with Atlas Search.
1 parent 8f38d7f commit e40c8b1

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

docs/en/cookbook/simple-search-engine.rst

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ setup a document like the following with a ``$keywords`` property that is mapped
1616
1717
<?php
1818
19-
namespace Documents;
20-
2119
#[Document]
2220
class Product
2321
{
2422
#[Id]
25-
private $id;
23+
public string $id;
2624
2725
#[Field(type: 'string')]
28-
private $title;
26+
public string $title;
2927
28+
/** @var Collection<string> */
3029
#[Field(type: 'collection')]
3130
#[Index]
32-
private $keywords = [];
31+
public Collection $keywords;
3332
34-
// ...
33+
public function __construct()
34+
{
35+
$this->keywords = new ArrayCollection();
36+
}
3537
}
3638
3739
Working with Keywords
@@ -44,12 +46,12 @@ Now, create a product and add some keywords:
4446
<?php
4547
4648
$product = new Product();
47-
$product->setTitle('Nike Air Jordan 2011');
48-
$product->addKeyword('nike shoes');
49-
$product->addKeyword('jordan shoes');
50-
$product->addKeyword('air jordan');
51-
$product->addKeyword('shoes');
52-
$product->addKeyword('2011');
49+
$product->title = 'Nike Air Jordan 2011';
50+
$product->keywords->add('nike shoes');
51+
$product->keywords->add('jordan shoes');
52+
$product->keywords->add('air jordan');
53+
$product->keywords->add('shoes');
54+
$product->keywords->add('2011');
5355
5456
$dm->persist($product);
5557
$dm->flush();
@@ -106,8 +108,9 @@ the results to your query. Here is an example:
106108
Embedded Documents
107109
------------------
108110

109-
If you want to use an embedded document instead of just an array then you can. It will allow you to store
110-
additional information with each keyword, like its weight.
111+
If you want to use an embedded document instead of just an array then you can.
112+
That will allow you to store additional information with each keyword, like its
113+
weight.
111114

112115
Definition
113116
~~~~~~~~~~
@@ -121,20 +124,14 @@ You can setup a ``Keyword`` document like the following:
121124
#[EmbeddedDocument]
122125
class Keyword
123126
{
124-
#[Field(type: 'string')]
125-
#[Index]
126-
private $keyword;
127-
128-
#[Field(type: 'int')]
129-
private $weight;
130-
131-
public function __construct(string $keyword, int $weight)
132-
{
133-
$this->keyword = $keyword;
134-
$this->weight = $weight;
135-
}
136-
137-
// ...
127+
public function __construct(
128+
#[Field(type: 'string')]
129+
#[Index]
130+
public $keyword,
131+
132+
#[Field(type: 'int')]
133+
public $weight,
134+
) {}
138135
}
139136
140137
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``:
150147
{
151148
// ...
152149
150+
/** @var Collection<Keyword> */
153151
#[EmbedMany(targetDocument: Keyword::class)]
154-
private $keywords;
152+
public Collection $keywords;
155153
156154
// ...
157155
}
@@ -163,10 +161,12 @@ you would have to do the following:
163161
164162
<?php
165163
166-
$product->addKeyword(new Keyword('nike shoes', 1));
164+
$product->keywords->add(new Keyword('nike shoes', 1));
167165
168-
This is a very basic search engine example and can work for many small and simple applications. If you
169-
need better searching functionality you can look at integrating something like `Solr`_ in your project.
166+
This is a very basic search engine example and can work for many small and
167+
simple applications. If you need better searching functionality you can look at
168+
`MongoDB Atlas Search`_, which can be integrated using
169+
:doc:`Search Indexes <../reference/search-indexes>`
170170

171171
.. _AlchemyAPI: http://www.alchemyapi.com
172-
.. _Solr: http://lucene.apache.org/solr
172+
.. _MongoDB Atlas Search: https://www.mongodb.com/products/platform/atlas-search

0 commit comments

Comments
 (0)