2017 © Pedro PelĂĄez
 

symfony-bundle search-bundle

A Symfony2 bundle for indexing and searching into single/multiple entities

image

it/search-bundle

A Symfony2 bundle for indexing and searching into single/multiple entities

  • Tuesday, May 23, 2017
  • by pierre-vassoilles
  • Repository
  • 2 Watchers
  • 0 Stars
  • 57 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

SearchBundle

Symfony2 Bundle : fulltext index and search for multiple Doctrine entities, (*1)

Installation

Install with composer :, (*2)

composer require it/search-bundle

Enable the bundle in your project

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new IT\SearchBundle\ITSearchBundle(),
        // ...
    );
}

Config

Add the following line to your config.yml :, (*3)

# app/config/config.yml

imports:
    - { resource: ../../vendor/it/search-bundle/IT/SearchBundle/Resources/config/config.yml }

it_search:
  indexes:
    enable_event_listener: ~ # Default to false, enables an Event listener that automatically update/create a mapped entity index
    min_score: ~ # Default to 0.8. Minimum score for the MATCH_AGAINST mysql function
    use_result_cache: ~ # Default to false. Enables the Doctrine result cache for 3600s
    projects:
      classname: 'ACMEBundle\Entity\EntityFQCN'
      identifier: id #identifier fieldName
      fields:
        - title
        - description
        #Other fields
      filters: #array used in a findBy method while indexing content
        isPublished: true
    news:
      classname: 'ACMEBundle\Entity\EntityFQCN2'
      identifier: id #identifier fieldName
      fields:
        - title
        - catchphrase
        - content
        #Other fields
      filters: #array used in a findBy method while indexing content
        published: true

CLI Tools

To manually index the fields, use the following command :, (*4)

php app/console search:index:build -d

How to use the bundle

In your controller, use the following lines to get the results in your search page :, (*5)

The search() function returns a SlidingPagination object (from Knp/Paginator)., (*6)

$databaseSearcher = $this->get('it_search.database.searcher');
$results = $databaseSearcher->search($terms, $page = 1, $limit = 10, array $entityClassnames = array(), $enableLikeSearch  = false);

Note : When you call the "search" method, il you don't provide a list of classnames as 4th parameter, the service will search in all entities indexed., (*7)

How to index content

Manual indexing

The bundle provides one service with two methods to index content., (*8)

The following method clears the index table and reindex all contents :, (*9)

$this->get('it_search.database.indexer')->indexContent();

The second method update an object's index into the database :, (*10)

$this->get('it_search.database.indexer')->updateIndex($object);

There is no method for removing a specific index from the database for now. The feature will be implemented soon., (*11)

Automatic indexing

New feature since 1.3.0 is an EventListener that automatically create, update and remove indexes when you create, update or remove entities that at mapped by the indexer. As the listener is disabled by default, use this config to enable it :, (*12)

# app/config/config.yml

it_search:
  enable_event_listener: true
  # ...

Note : The listener is disabled by default to stay compatible with older versions of the bundle, (*13)

Events

Three event are dispatched during objects indexation : - ITSearchEvents::PRE_INDEX Dispatched at the beginning of the indexing., (*14)

  • ITSearchEvents::PRE_INDEX_OBJECT Dispatched before indexing a specific object. The object is available in the event object., (*15)

  • ITSearchEvents::POST_INDEX Dispatched after indexing all objects. The SearchIndex objects list is available in the event object., (*16)

Example of use

Here is an example of an All-in-one event subscriber :, (*17)

// ACMEBundle\EventSubscriber\SearchEventSubscriber.php

use IT\SearchBundle\Event\ITSearchEvents;
use IT\SearchBundle\Event\SearchPostIndexEvent;
use IT\SearchBundle\Event\SearchPreIndexEvent;
use IT\SearchBundle\Event\SearchPreIndexObjectEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SearchEventSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {

        return array(
            ITSearchEvents::PRE_INDEX           => 'preIndex',
            ITSearchEvents::PRE_INDEX_OBJECT    => 'preIndexObject',
            ITSearchEvents::POST_INDEX          => 'postIndex',
        );
    }

    public function preIndex(SearchPreIndexEvent $evt)
    {
        // Do stuff here
    }

    public function preIndexObject(SearchPreIndexObjectEvent $evt)
    {
        // Do stuff here
    }

    public function postIndex(SearchPostIndexEvent $evt)
    {
        // Do stuff here
    }

}
    <!--services.yml-->
    <services>
        <!--...-->
        <service id="acme.search.subscriber" class="ACMEBundle\EventSubscriber\SearchEventSubscriber">
            <tag name="kernel.event_subscriber"></tag>
        </service>
        <!--...-->
    </services>

OR, in Yaml :, (*18)

#services.yml

services:
  acme.search.subscriber:
    class: "ACMEBundle\EventSubscriber\SearchEventSubscriber"
    tags:
      - { name: "kernel.event_subscriber" }

The Versions

23/05 2017

dev-master

9999999-dev

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

23/05 2017

1.3.5

1.3.5.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

23/05 2017

1.3.4

1.3.4.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

23/05 2017

1.3.3

1.3.3.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

11/05 2017

1.3.2

1.3.2.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

11/05 2017

1.3.1

1.3.1.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

30/01 2017

1.3.0

1.3.0.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

30/01 2017

1.2.0

1.2.0.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

25/01 2017

1.1.0

1.1.0.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

25/01 2017

1.0.0

1.0.0.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

25/01 2017

0.9.1

0.9.1.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires

 

25/01 2017

0.9

0.9.0.0

A Symfony2 bundle for indexing and searching into single/multiple entities

  Sources   Download

MIT

The Requires