尝试调用名为“ FOS \ ElasticaBundle \ Repository”的类的名为“ search”的未定义方法

时间:2019-01-14 13:49:22

标签: symfony elasticsearch elastica

我想使用Elastica存储库中的搜索方法,但出现错误 Attempted to call an undefined method named "search" of class "FOS\ElasticaBundle\Repository". 我将symfony 4与最新版本的fos elastica捆绑包一起使用。在仓库我工作查找方法,但它不会返回任何结果。 我想像这里https://www.codevate.com/blog/14-implementing-search-as-you-type-autocomplete-with-elasticsearch-and-symfony

那样按类型搜索自动完成功能

控制器:

/**
     * @Route("/search")
     */
    public function searchElastic(RepositoryManagerInterface $finder, Request $request){
        $searchTerm = $request->query->get('s');
        $searchTerm = htmlentities($searchTerm, ENT_QUOTES);

//        $finder = $finder->getRepository(\App\Entity\User::class)->find($searchTerm);
//        return new response(var_dump($finder[0]->getUsername()));

        $completion = new Suggest\Completion('suggest', 'name_suggest');
        $completion->setText($searchTerm);
        $completion->setFuzzy(array('fuzziness' => 2));
        /** var array of App\Entity\User */
        $resultSet = $finder->getRepository(\App\Entity\User::class)->search((Query::create($completion)));
        var_dump($resultSet);
        $suggestions = array();
        foreach ($resultSet->getSuggests() as $suggests) {
            foreach ($suggests as $suggest) {
                foreach ($suggest['options'] as $option) {
                    $suggestions[] = array(
                        'id' => $option['_source']['id'],
                        'username' => $option['_source']['username']
                    );
                }
            }
        }
        return new JsonResponse(array(
            'suggestions' => $suggestions,
        ));
    }

配置:

# Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Resources/doc/setup.md
fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
#    indexes:
#        app: ~
    indexes:
        app:
            client: default
#FOR AUTOCOMPLETE
            settings:
                index:
                    analysis:
                        analyzer:
                            name_analyzer:
                                type: custom
                                tokenizer: standard
                                filter: [standard, lowercase, asciifolding, elision]
#END FOR AUTOCOMPLETE
            types:
                user:
                    properties:
#                        username: ~
#                        username:
                        name_suggest:
                          #                    MAPPINGS ADDED FOR AUTOCOMPLETE
                            type: completion
                            analyzer: name_analyzer
                            search_analyzer: name_analyzer
#                            payloads: true
                        id:
                            type: keyword
                        username:
                            type: keyword

#MAPPINGS ADDED FOR AUTOCOMPLETE
                    persistence:
                        # the driver can be orm, mongodb, phpcr or propel
                        # listener and finder are not supported by
                        # propel and should be removed
                        driver: orm
                        model: App\Entity\User
                        provider: ~
                        listener: ~
                        finder: ~


用户实体

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Repository\UserRepository;
use Elastica\Search;

/**
 * @UniqueEntity(fields="email", message="Email already taken")
 * @UniqueEntity(fields="username", message="Username already taken")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @Search(repositoryClass="AppBundle\SearchRepository\PropositionRepository")
 */
class User implements UserInterface
{

请帮助,我是Elastica的初学者。

更新:

现在我收到此错误。

Cannot autowire argument $finder of "App\Controller\DevController::searchElastic2()": it references class "FOS\ElasticaBundle\Doctrine\RepositoryManager" but no such service exists. Try changing the type-hint to "FOS\ElasticaBundle\Manager\RepositoryManagerInterface" instead.

更新:

Argument 1 passed to App\Repository\UserRepository::__construct() must implement interface Symfony\Bridge\Doctrine\RegistryInterface, instance of FOS\ElasticaBundle\Finder\TransformedFinder given, called in C:\xampp\htdocs\projects\symfonysite\vendor\friendsofsymfony\elastica-bundle\src\Manager\RepositoryManager.php on line 101

更新:

UserRepository.php a link

User.php实体a link

控制器a link

**更新:**

我不明白。链接要求搜索功能中的教程(不在我的版本中),而对我有用的查找功能仅能找到一个。这是对我有用的简单代码,但是找到了一个结果。

    /**
     * @Route("/search")
     */
    public function searchElastic(RepositoryManagerInterface $finder, Request $request){
        $searchTerm = $request->query->get('s');
        $searchTerm = htmlentities($searchTerm, ENT_QUOTES);

        $finder = $finder->getRepository(\App\Entity\User::class)->find($searchTerm);

        return new response(var_dump($finder[0]->getUsername())); //i tried changing index to 1 but always it return undefined offset

1 个答案:

答案 0 :(得分:0)

由于已合并PR的外观,搜索注释支持已被删除,因为不应再通过Bundle:Entity表示法访问存储库,而是请求索引/类型。

要使用自定义存储库,请在您的用户的映射中将其指定,只需在您的fos_elastica下将其删除:

fos_elastica:
    ...
    indexes:
        app:
            client: default
            types:
                user:
                    properties:
                        # your properties
                    persistence:
                        ...
                        repository: App\Repository\UserRepository

然后,当使用从管理器返回的存储库时,自定义查询将可用:

use FOS\ElasticaBundle\Repository;

class UserRepository extends Repository
{
  ...
  function search(RepositoryManagerInterface $finder, Request $request)
    {
    /** var FOS\ElasticaBundle\Manager\RepositoryManagerInterface */
    $repositoryManager = $finder;
    /** var FOS\ElasticaBundle\Repository */
    $repository = $repositoryManager->getRepository(AppEntityUser::class);

    /** var array of Acme\UserBundle\Entity\User */
    $users = $repository->search('bob');
....