tapronto/solr-bundle

此包的最新版本(0.2.1)没有提供许可证信息。

Symfony2 Solr集成包

安装: 71

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

0.2.1 2012-11-03 12:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:09:22 UTC


README

此包提供了一种简单的API,用于索引和查询Solr索引。

安装

Solr服务器

请遵循以下教程中的安装说明:教程

PHP扩展

	sudo pecl install -n solr-beta

  1. 在AppKernel.php中注册包

    # app/AppKernel.php
    
    $bundles = array(
        // ...
        new FS\SolrBundle\FSSolrBundle(),
        // ...
    );
    
  2. 添加包到自动加载

    # app/autoload.php
    
    $loader->registerNamespaces(array(
        // ...
        'FS' => __DIR__.'/../vendor/bundles',
        // ...
    ));
    

配置

您必须设置连接选项

	# app/config/config.yml
	
	fs_solr:
		solr:
			hostname: localhost
			port: 8983
            path:
                core0: /solr/core0
                core1: /solr/core1
        auto_index: true|false
		entity_manager: default 

用法

要将实体放入索引,您必须在实体中添加一些注解

	// your Entity

	// ....
	use FS\SolrBundle\Doctrine\Annotation as Solr;
	
	/**
	 * 
	 *
	 * @Solr\Document(repository="Full\Qualified\Class\Name")
	 * @ORM\Table()
	 */
	class Post
	{
		/**
		 * @Solr\Id
		 *
		 * @ORM\Column(name="id", type="integer")
		 * @ORM\Id
		 * @ORM\GeneratedValue(strategy="AUTO")
		 */
		private $id;

		/**
		 *
		 * @Solr\Field(type="string")
		 *
		 * @ORM\Column(name="title", type="string", length=255)
		 */
		private $title = '';

		/**
		 * 
		 * @Solr\Field(type="string")
		 *
		 * @ORM\Column(name="text", type="text")
		 */
		private $text = '';

		/**
		 * @Solr\Field(type="date")
		 *
		 * @ORM\Column(name="created_at", type="datetime")
		 */
		private $created_at = null;
	}

如果持久化此实体,它将自动放入索引。更新和删除也将自动进行。

要查询索引,您必须调用一些服务。

	$query = $this->get('solr')->createQuery('AcmeDemoBundle:Post');
	$query->addSearchTerm('title', 'my title');
	$query->addField('id');
	$query->addField('text');
	
	$result = $query->getResult();

$result数组包含所有找到的实体。solr-service为您执行从SolrDocument到实体的所有映射。在这种情况下,只有字段id和text将被映射(addField()),因此标题和created_at将为空。如果没有找到任何内容,$result将为空。

如果没有明确添加字段,则将映射所有字段。

	$query = $this->get('solr')->createQuery('AcmeDemoBundle:Post');
	$query->addSearchTerm('title', 'my title');
	
	$result = $result = $query->getResult();

前面的示例仅查询了字段'title'。您也可以用字符串查询所有字段。

	$query = $this->get('solr')->createQuery('AcmeDemoBundle:Post');
	$query->queryAllFields('my title);
		
	$result = $query->getResult();

要手动索引实体,您可以这样做

	$this->get('solr')->addDocument($entity);
	$this->get('solr')->updateDocument($entity);
	$this->get('solr')->deleteDocument($entity);

删除操作需要实体的id。

如果您指定了自己的存储库,则必须扩展FS\SolrBundle\Repository\Repository类。用法与Doctrine-Repositories相同

$myRepository = $this->get('solr')->getRepository('AcmeDemoBundle:Post');
$result = $myRepository->mySpecialFindMethod();

如果您在实体中未声明具体的存储库,并调用$this->get('solr')->getRepository('AcmeDemoBundle:Post'),您将获得一个FS\SolrBundle\Repository\Repository的实例。

MongoDB

所有这些功能也适用于mongo-db实体。实体配置通过注解完全相同。

使用多个核心

Solr支持多个索引。如果您的应用程序中有不同的语言,则可以将文档索引在不同的索引中。

设置很简单

path选项下,您可以指定不同的索引。

        path:
                core0: /solr/core0
                core1: /solr/core1

在这种情况下,默认核心是core0。如果您使用多个核心,则应禁用自动索引功能。否则,所有文档都将索引在一个核心中。要禁用,请使用配置中的auto_index标志(默认值为true)。

要使用addDocument方法索引文档,需要具体的核心

    $this->get('solr')->core('core0')->addDocument($document);

命令

此包包含两个命令

  • solr:index:clear - 删除索引中的所有文档
  • solr:synchronize - 同步数据库与索引。您必须指定一个实体。