Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Review Simple Search Engine Cookbook #2659

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't show use statements, this namespace is less useful.


#[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
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
: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
Loading