You are browsing a version that has not yet been released.

Simple Search Engine

It is very easy to implement a simple keyword search engine with MongoDB. Because of its flexible schema less nature we can store the keywords we want to search through directly on the document. MongoDB is capable of indexing the embedded documents so the results are fast and scalable.

Sample Model: Product

Imagine you had a Product document and you want to search the products by keywords. You can setup a document like the following with a $keywords property that is mapped as a collection:

1<?php #[Document] class Product { #[Id] public string $id; #[Field(type: 'string')] public string $title; /** @var Collection<string> */ #[Field(type: 'collection')] #[Index] public Collection $keywords; public function __construct() { $this->keywords = new ArrayCollection(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Working with Keywords

Now, create a product and add some keywords:

1<?php $product = new Product(); $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();
2
3
4
5
6
7
8
9
10
11
12

The above example populates the keywords manually but you could very easily write some code which automatically generates your keywords from a string built by the Product that may include the title, description and other fields. You could also use a tool like the AlchemyAPI if you want to do some more intelligent keyword extraction.

Searching Keywords

Searching the keywords in the Product collection is easy! You can run a query like the following to find documents that have at least one of the keywords:

1<?php $keywords = ['nike shoes', 'air jordan']; $qb = $dm->createQueryBuilder(Product::class) ->field('keywords')->in($keywords);
2
3
4
5
6

You can make the query more strict by using the all() method instead of in():

1<?php $keywords = ['nike shoes', 'air jordan']; $qb = $dm->createQueryBuilder(Product::class) ->field('keywords')->all($keywords);
2
3
4
5
6

The above query would only return products that have both of the keywords!

User Input

You can easily build keywords from a user search form by exploding whitespace and passing the results to your query. Here is an example:

1<?php $queryString = $_REQUEST['q']; $keywords = explode(' ', $queryString); $qb = $dm->createQueryBuilder(Product::class) ->field('keywords')->all($keywords);
2
3
4
5
6
7

Embedded Documents

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

You can setup a Keyword document like the following:

1<?php #[EmbeddedDocument] class Keyword { public function __construct( #[Field(type: 'string')] #[Index] public $keyword, #[Field(type: 'int')] public $weight, ) {} }
2
3
4
5
6
7
8
9
10
11
12
13
14

Now you can embed the Keyword document many times in the Product:

1<?php namespace Documents; #[Document] class Product { // ... /** @var Collection<Keyword> */ #[EmbedMany(targetDocument: Keyword::class)] public Collection $keywords; // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15

With the new embedded document to add a keyword to a Product the API is a little different, you would have to do the following:

1<?php $product->keywords->add(new Keyword('nike shoes', 1));
2
3

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, which can be integrated using Search Indexes