Skip to content

Commit

Permalink
Review Simple Search Engine Cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jun 27, 2024
1 parent 1408466 commit 6dff12b
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions docs/en/cookbook/simple-search-engine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ setup a document like the following with a ``$keywords`` property that is mapped
<?php
namespace Documents;
#[Document]
class Product
{
#[Id]
private $id;
public string $id;
#[Field(type: 'string')]
private $title;
public string $title;
/** @var Collection<string> */
#[Field(type: 'collection')]
#[Index]
private $keywords = [];
public Collection $keywords;
// ...
public function __construct()
{
$this->keywords = new ArrayCollection();
}
}
Working with Keywords
Expand All @@ -44,12 +46,12 @@ Now, create a product and add some keywords:
<?php
$product = new Product();
$product->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();
Expand Down Expand Up @@ -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
~~~~~~~~~~
Expand All @@ -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``:
Expand All @@ -150,8 +147,9 @@ Now you can embed the ``Keyword`` document many times in the ``Product``:
{
// ...
/** @var Collection<Keyword> */
#[EmbedMany(targetDocument: Keyword::class)]
private $keywords;
public Collection $keywords;
// ...
}
Expand All @@ -163,10 +161,12 @@ you would have to do the following:
<?php
$product->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 <reference/seach-indexes>`

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

0 comments on commit 6dff12b

Please sign in to comment.