symfony FOSElastica在多个实体中搜索

4ioopgfo  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(89)

我设法在我的symfony项目中安装了FOSElastica,我的fos_elsastica. yaml。

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        user:
            persistence:
                driver: orm
                model: App\Entity\User
                provider: ~
                finder: ~
            properties:
                email: ~

是否可以在多个实体中搜索,例如:

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        user:
            persistence:
                driver: orm
                model: App\Entity\User
                provider: ~
                finder: ~
            properties:
                email: ~
        course:
            persistence:
                driver: orm
                model: App\Entity\Course
                provider: ~
                finder: ~
            properties:
                name: ~

如果是,如何处理服务。yaml文件和控制器功能?我需要在正确的方向指出。
我尝试了很多方法,但都没有成功。
先谢了。

dzhpxtsq

dzhpxtsq1#

我不知道它的用途是什么。使用不同的索引有多种选择。
如果你想在另一个索引中搜索,你可以使用IndexManager,例如:

services:
    app.elasticsearch_search:
        class: App\Service\ElasticsearchSearchService
        arguments:
            - '@fos_elastica.index'

<?php 

namespace App\Service;

use FOS\ElasticaBundle\Manager\IndexManager;

class ElasticsearchSearchService
{
    private $indexManager;

    public function __construct(IndexManager $indexManager)
    {
        $this->indexManager = $indexManager;
    }

    public function searchInIndex(string $index, string $query)
    {
        $index = $this->indexManager->getIndex($index);
        return $userIndex->search($query);
    }
}

但如果你想合并索引,看看嵌套对象:https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/indexes.md#nested-objects-in-foselasticabundle在文档中,我不知道你的实体设置,但这里有一个小例子与相同的假设。

fos_elastica:
   clients:
       default: { host: localhost, port: 9200 }
   indexes:
       course:
           use_alias: true
           properties:
               id: {type: integer}
               name: {type: text}
               user:
                   type: 'object'
                   properties:
                       id: {type: integer}
                       name: {type: text}
           persistence:
               driver: orm
               model: App\Entity\Course
               provider: ~
               finder: ~

相关问题