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

[3.4.0] Adds Aggregators - Multiple entities in the same index #257

Merged
merged 40 commits into from
Dec 11, 2018

Conversation

nunomaduro
Copy link
Contributor

@nunomaduro nunomaduro commented Sep 9, 2018

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Related Issue Fix #201
Need Doc update yes

Describe your change

  • This Pull Request is a work in progress. Any feedback about the technical approach, implementation or would be highly appreciated. 🙏
  • If you are running algolia/search-bundle in a Symfony application, please try this feature locally and report bugs (if any) on this discussion. 👍

This Pull Request introduces a new feature on Algolia algolia/search-bundle called Aggregators.

Larastan Example

An aggregator object allows to aggregate more than one entity type in the same index. With aggregators, developers can provide a better search experience since the search results will contain content from various sources (entities).

How to configure an aggregator:

  1. First, add a new aggregator on the configuration file algolia_search.yml:
        - indices:
            - name: news
              class: App\Entity\News
  1. Then, create a new Aggregator object and declare the entities that you want to aggregate:
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Algolia\SearchBundle\Entity\Aggregator;

/**
 * @ORM\Entity
 */
class News extends Aggregator
{
    /**
     * Returns the entities class names that should be aggregated.
     *
     * @return string[]
     */
    public static function getEntities()
    {
        return [
            Post::class,
            Comment::class,
        ];
    }
}

Indexation & Search:

  1. IndexManager::search method:
$indexManager->index($post, $objectManager);
$indexManager->index($comment, $objectManager);

$results = $indexManager->search('', News::class, $objectManager);

// $results[0] will contain a Post.
// $results[1] will contain a Comment.
  1. IndexManager::rawSearch method:
$indexManager->index($post, $objectManager);
$indexManager->index($comment, $objectManager);

$results = $indexManager->rawSearch('', News::class);

/**
 * 
 * Results:
 * 
 * {
 *     "hits": [
 *         {
 *             "id": 2,
 *             "title": "Test",
 *             "content": "Test content",
 *             "objectID": "App\Entity\Post::1",
 *             ...
 *         },
 *         {
 *             "content": "Comment content",
 *             "post_title": "What a post!",
 *             "objectID": "App\Entity\Comment::1",
 *             ...
 *         },
 *         ...
 *     ]
 * }
*/
  1. IndexManager::remove method:
$indexManager->index($post, $objectManager);
$indexManager->index($comment, $objectManager);
$indexManager->remove($comment, $objectManager);

$results = $indexManager->search('', News::class, $objectManager);
// count($results) will be 1.
  1. Advanced Aggregators:

I will wait for some feedback from the community before writing out for a detailed explanation of the advanced usage of aggregators.

@nunomaduro
Copy link
Contributor Author

I will review this Pull Request again later. Now that I have better understanding of the product, things can be even better on this Pull Request.

@nunomaduro nunomaduro changed the title [WIP] Adds Aggregators - Multiple entities in the same index Adds Aggregators - Multiple entities in the same index Dec 6, 2018
@nunomaduro nunomaduro changed the title Adds Aggregators - Multiple entities in the same index [3.4.0] Adds Aggregators - Multiple entities in the same index Dec 6, 2018
src/Aggregator.php Outdated Show resolved Hide resolved
src/Command/SearchImportCommand.php Outdated Show resolved Hide resolved
src/Aggregator.php Outdated Show resolved Hide resolved
src/IndexManager.php Show resolved Hide resolved
src/IndexManager.php Outdated Show resolved Hide resolved
src/IndexManager.php Outdated Show resolved Hide resolved
src/Aggregator.php Outdated Show resolved Hide resolved
src/Aggregator.php Outdated Show resolved Hide resolved
Copy link
Contributor

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

One more note about ODM mappings, apart from that this looks good 👍

src/Resources/config/doctrine/aggregator.mongodb.xml Outdated Show resolved Hide resolved
@nunomaduro nunomaduro merged commit 9161d37 into algolia:master Dec 11, 2018
@nunomaduro nunomaduro deleted the feature/aggregators branch December 11, 2018 10:44
@kiler129
Copy link
Contributor

@nunomaduro Is there any more docs on this feature? How such index will look in Algolia index?

@nunomaduro
Copy link
Contributor Author

nunomaduro commented Dec 18, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Several entity for an index
3 participants