• This repository has been archived on 23/Apr/2022
  • Stars
    star
    274
  • Rank 144,599 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generic Search extension for indexing and querying ODM/ORM objects with different text-search engine implementations

Doctrine Search

Note: This project is a prototype at the moment. See demo folder for practical implementation example.

Supported search engines

Features

  • SearchManager
    • Can be used stand-alone or in a hybrid configuration
    • Configurable search manager supports aggregate entity manager
    • supports direct API calls through search engine adapters such as Elastica
    • transforms returned ID's via batch operation into hydrated objects as required
    • Supports event manager listeners for customizable entity handling
  • Support for indexing through event listeners via JMS Serializer or simple entity callback.
  • Annotations for index and data type creation using ObjectManager::getClassMetadata() as the base structure

#Usage#

Configuration

The search manager connection can be configured as shown in the following example:

$config = new Doctrine\Search\Configuration();
$config->setMetadataCacheImpl(new Doctrine\Common\Cache\ArrayCache());
$config->setEntitySerializer(
  new Doctrine\Search\Serializer\JMSSerializer(
    JMS\Serializer\SerializationContext::create()->setGroups('search')
  )
);

$eventManager = new Doctrine\Search\EventManager();
$eventManager->addListener($listener);

$searchManager = new Doctrine\Search\SearchManager(
  $config,
  new Doctrine\Search\ElasticSearch\Client(
    new Elastica\Client(array(
      array('host' => 'localhost', 'port' => '9200')
    )
  ),
  $eventManager
);

Mappings

Basic entity mappings for index and type generation can be annotated as shown in the following example. Mappings can be rendered into a format suitable for automatically generating indexes and types using a build script (advanced setup required).

<?php
namespace Entities;

use Doctrine\Search\Mapping\Annotations as MAP;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @MAP\ElasticSearchable(index="indexname", type="post", source=true)
 */
class Post
{
  /**
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   * @MAP\ElasticField(type="integer", includeInAll=false)
   */
  private $id;

  /**
   * @ORM\Column(type="string")
   * @MAP\ElasticField(type="string", includeInAll=true, boost=5.0)
   */
  private $title;

  /**
   * @ORM\Column(type="text")
   * @MAP\ElasticField(type="string", includeInAll=true)
   */
  private $content;

  /**
   * @MAP\ElasticField(name="tags", type="string", includeInAll=false, index="not_analyzed")
   */
  public function getTags() {
    return $this->tags->slice(0,3);
  }
}

Indexing

Documents can be serialized for indexing currently in the following ways. If required an event listener can be used with your ORM as shown in this example. If an event listener is not needed, entities can be persisted or removed directly using the search manager.

<?php
namespace Entities\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Entities\Behaviour\SearchableEntityInterface;

class SearchableListener implements
{
      protected function getSearchManager() {
            return $this->getDatabaseConnection('elasticsearch');
      }

      public function postPersist(LifecycleEventArgs $oArgs) {
            $oEntity = $oArgs->getEntity();
            if($oEntity instanceof SearchableEntityInterface) {
                $this->getSearchManager()->persist($oEntity);
          }
      }

    public function postRemove(LifecycleEventArgs $oArgs) {
        $oEntity = $oArgs->getEntity();
        if($oEntity instanceof SearchableEntityInterface) {
            $this->getSearchManager()->remove($oEntity);
        }
    }
}

CallbackSerializer

This approach simply expects a toArray() method on the entity, although this method be configured as required. The interface suggested in this example can be any interface you desire, as long as your event listener can identify entities that need to be persisted to the search engine (see above example).

...
use Entities\Behaviour\SearchableEntityInterface

class Post implements SearchableEntityInterface
{
  ...
  public function toArray() {
    return array(
      'id' => $this->id,
      'title' => $this->title,
      'content' => $this->content
      ...
    );
  }
}

JMS Serializer

You can alternatively use the advanced serialization power of the JMS Serializer to automatically handle serialization for you based on annotations such as those shown in this example.

...
use JMS\Serializer\Annotation as JMS;
use Entities\Behaviour\SearchableEntityInterface

/**
 * @ORM\Entity
 * @MAP\ElasticSearchable(index="indexname", type="post", source=true)
 * @JMS\ExclusionPolicy("all")
 */
class Post implements SearchableEntityInterface
{
  ...
  /**
   * @ORM\Column(type="string")
   * @MAP\ElasticField(type="string", includeInAll=true, boost=5.0)
   * @JMS\Expose
   * @JMS\Groups({"public", "search"})
   */
  private $title;

  /**
   * @ORM\Column(type="text")
   * @MAP\ElasticField(type="string", includeInAll=true)
   * @JMS\Expose
   * @JMS\Groups({"public", "search"})
   */
  private $content;
  ...
}

AnnotationSerializer

Not yet available.

Queries

Queries can be executed through the search manager as shown below. Use of the result cache refers to using the cache for the hydration query. Search engine specific adapter interfaces are exposed magically so as in this example, Elastica\Query::addSort method is amalgamated with Doctrine\Search\Query methods. Thus any complexity of query supported by the search engine client library is supported.

$hydrationQuery = $entityManager->createQueryBuilder()
  ->select(array('p', 'field(p.id, :ids) as HIDDEN field'))
    ->from('Entities\Post', 'p')
    ->where('p.id IN (:ids)')
    ->orderBy('field')
    ->getQuery();

$query = $searchManager->createQuery()
  ->from('Entities\Post')
  ->searchWith(new Elastica\Query())
    ->hydrateWith($hydrationQuery)
    ->addSort('_score')
    ->setFrom(0)
    ->setLimit(10)
    ->getResult();

Simple Repository ID queries and Term search can by done using the following technique. Deserialization is done independently of Doctrine\ORM but the same models are hydrated according to the registered SerializerInterface.

$entity = $searchManager->getRepository('Entities\Post')->find($id);
$entity = $searchManager->getRepository('Entities\Post')->findOneBy(array($key => $term));

More Repositories

1

inflector

Doctrine Inflector is a small library that can perform string manipulations with regard to uppercase/lowercase and singular/plural forms of words.
PHP
11,171
star
2

lexer

Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
PHP
10,998
star
3

instantiator

PHP
10,899
star
4

orm

Doctrine Object Relational Mapper (ORM)
PHP
9,795
star
5

dbal

Doctrine Database Abstraction Layer
PHP
9,331
star
6

cache

Doctrine Cache component
PHP
7,776
star
7

annotations

Annotations Docblock Parser
PHP
6,723
star
8

event-manager

The Doctrine Event Manager is a library that provides a simple event system.
PHP
5,868
star
9

collections

Collections Abstraction Library
PHP
5,817
star
10

common

Doctrine Common
PHP
5,755
star
11

DoctrineBundle

Symfony Bundle for Doctrine ORM and DBAL
PHP
4,648
star
12

migrations

Doctrine Database Migrations Library
PHP
4,626
star
13

DoctrineMigrationsBundle

Symfony integration for the doctrine/migrations library
PHP
4,146
star
14

persistence

The Doctrine Persistence project is a library that provides common abstractions for object mapper persistence.
PHP
3,883
star
15

reflection

The Doctrine Reflection project is a simple library used by the various Doctrine projects which adds some additional functionality on top of the reflection API that comes with PHP. It allows you to get the reflection information about classes, methods and properties statically.
PHP
2,959
star
16

DoctrineCacheBundle

Symfony2 Bundle for Doctrine Cache
PHP
2,763
star
17

data-fixtures

Doctrine2 ORM Data Fixtures Extensions
PHP
2,740
star
18

DoctrineFixturesBundle

Symfony integration for the doctrine/data-fixtures library
PHP
2,416
star
19

sql-formatter

A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.
HTML
1,580
star
20

deprecations

Thin library around different deprecation strategies
PHP
1,527
star
21

mongodb-odm

The Official PHP MongoDB ORM/ODM
PHP
1,079
star
22

mongodb

PHP MongoDB Abstraction Layer
PHP
442
star
23

DoctrineORMModule

Doctrine ORM Module for Laminas
PHP
432
star
24

DoctrineModule

Doctrine Module for Laminas
PHP
395
star
25

DoctrineMongoDBBundle

Integrates Doctrine MongoDB ODM with Symfony
PHP
381
star
26

coding-standard

The coding standard of the Doctrine project.
PHP
306
star
27

KeyValueStore

Abstraction for Key-Value to Plain Old PHP Object mapping
PHP
201
star
28

phpcr-odm

Doctrine PHPCR ODM
PHP
181
star
29

orientdb-odm

A set of PHP libraries in order to use OrientDB from PHP
PHP
156
star
30

couchdb-odm

A Document Mapper based on CouchDB
PHP
151
star
31

DoctrinePHPCRBundle

This bundle integrates Doctrine PHPCR ODM and PHPCR backends into Symfony
PHP
150
star
32

doctrine2-orm-tutorial

Code for the ORM Tutorial
PHP
143
star
33

rest

Doctrine REST Server and Client Library
PHP
142
star
34

doctrine1

[Abandoned - please upgrade to Doctrine 2] Doctrine 1 Object Relational Mapper
PHP
133
star
35

couchdb-client

CouchDB Client library
PHP
122
star
36

skeleton-mapper

Doctrine Skeleton Object Mapper
PHP
101
star
37

orm-documentation

OUTDATED REPOSITORY - PLEASE MAKE PULL REQUESTS TO DOCTRINE2 REPOSITORY
Python
96
star
38

oxm

PHP Object to XML Mapping (OXM)
PHP
86
star
39

DoctrineMongoODMModule

Laminas Module for Doctrine MongoDB ODM
PHP
83
star
40

rst-parser

PHP
58
star
41

DoctrineCouchDBBundle

Symfony Bundle for DoctrineCouchDB
PHP
49
star
42

jira-github-issues

Scripts to migrate Jira to Github issues
PHP
37
star
43

mongodb-odm-softdelete

Soft delete functionality for the Doctrine MongoDB ODM
PHP
35
star
44

doctrine-laminas-hydrator

Doctrine hydrators for Laminas applications
PHP
31
star
45

doctrine1-documentation

Documentation for Doctrine 1
Python
30
star
46

doctrine-website

Source code for the doctrine-project.org website and documentation.
PHP
28
star
47

automatic-releases

Internal automatic release tool for doctrine/* projects
PHP
28
star
48

shards

Prototype sharding Extension for Doctrine
PHP
26
star
49

doctrine-website-sphinx

Sphinx-based Doctrine Website
CSS
18
star
50

doctrine-mongodb-odm-tailable-cursor-bundle

Doctrine MongoDB ODM Tailable Cursor Bundle
PHP
16
star
51

symfony-doctrine-static-website-generator

PHP
15
star
52

mongodb-odm-softdelete-bundle

Symfony2 bundle that integrates the Doctrine MongoDB ODM SoftDelete library.
PHP
14
star
53

dbal-documentation

MOVED TO DOCTRINE/DBAL REPOSITORY
Python
13
star
54

common-documentation

Documentation for the common Doctrine PHP extensions library.
Python
13
star
55

doctrine-zend-hydrator

Doctrine hydrators for Zend Framework applications
PHP
12
star
56

migrations-documentation

Documentation for the Doctrine Migrations
Python
12
star
57

.github

11
star
58

dbal-bundle

PHP
7
star
59

static-website-generator

Doctrine Static Website Generator
PHP
5
star
60

doctrine-website-build-prod

Generated website artifacts. See https://www.doctrine-project.org/contribute/website/
HTML
5
star
61

doctrine-sphinx-theme

Sphinx Theme for all Doctrine Documentations
CSS
4
star
62

doctrine-build-common

Common Phing Build Scripts and Tasks for all Doctrine Projects
PHP
4
star
63

phpcr-odm-documentation

PHP Content Repository ODM Documentation
Python
4
star
64

orm-benchmarking-suite

Suite to do functional ORM performance tests across versions
PHP
4
star
65

couchdb-documentation

Documentation for Doctrine CouchDB ODM
Python
4
star
66

DoctrineStaticWebsiteGeneratorBundle

DoctrineStaticWebsiteGeneratorBundle
PHP
2
star
67

vagrant-doctrine-website

Shell
1
star
68

jira-redirector

HTML
1
star
69

contributors-guide

A complete guide for Doctrine project collaborators and contributors.
1
star