【问题标题】:Class 'ZendSearch\Lucene\Lucene' not found ZendFramework2未找到“ZendSearch\Lucene\Lucene”类 ZendFramework2
【发布时间】:2021-03-03 03:24:32
【问题描述】:

我已经使用这些命令安装了 ZendSearch 和 composer:

$ cd /var/www/CommunicationApp/vendor/
$ git clone https://github.com/zendframework/ZendSearch.git
ZendSearch
$ cd ZendSearch/
$ curl -s https://getcomposer.org/installer | php
$ php composer.phar install

我已经按照GITHUB 安装了 Zendskeleton 所以,我不知道我在这里缺少什么。

比,在同一个 book 中,它教如何使用 ZendSearch,但我没有得到相同的结果,而是我得到一个致命错误:致命错误:类 'ZendSearch\Lucene\Lucene' 不是在第 107 行的 /var/www/CommunicationApp/module/Users/src/Users/Controller/SearchController.php 中找到

<?php
namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zend\Http\Headers;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;

use Users\Model\User;
use Users\Model\UserTable;
use Users\Model\Upload;

use Users\Model\ImageUpload;
use Users\Model\ImageUploadTable;

use ZendSearch\Lucene;
use ZendSearch\Lucene\Document;
use ZendSearch\Lucene\Index;

class SearchController extends AbstractActionController
{
    protected $storage;
    protected $authservice;

    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authservice;
    }

    public function getIndexLocation()
    {
        // Fetch Configuration from Module Config
        $config  = $this->getServiceLocator()->get('config');
        if ($config instanceof Traversable) {
            $config = ArrayUtils::iteratorToArray($config);
        }
        if (!empty($config['module_config']['search_index'])) {
            return $config['module_config']['search_index'];
        } else {
            return FALSE;
        }
    }

    public function getFileUploadLocation()
    {
        // Fetch Configuration from Module Config
        $config  = $this->getServiceLocator()->get('config');
        if ($config instanceof Traversable) {
            $config = ArrayUtils::iteratorToArray($config);
        }
        if (!empty($config['module_config']['upload_location'])) {
            return $config['module_config']['upload_location'];
        } else {
            return FALSE;
        }
    }   

    public function indexAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $queryText = $request->getPost()->get('query');
            $searchIndexLocation = $this->getIndexLocation();
            $index = Lucene\Lucene::open($searchIndexLocation);
            $searchResults = $index->find($queryText);
        }

        // prepare search form
        $form  = new \Zend\Form\Form();
        $form->add(array(
            'name' => 'query',
            'attributes' => array(
                'type'  => 'text',
                'id' => 'queryText',
                'required' => 'required'
            ),
            'options' => array(
                'label' => 'Search String',
            ),
        ));
        $form->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Search', 
                'style' => "margin-bottom: 8px; height: 27px;"
            ),
        ));

        $viewModel  = new ViewModel(array('form' => $form, 'searchResults' => $searchResults));
        return $viewModel;
    }


    public function generateIndexAction()
    {
        $searchIndexLocation = $this->getIndexLocation();
        $index = Lucene\Lucene::create($searchIndexLocation);

        $userTable = $this->getServiceLocator()->get('UserTable');
        $uploadTable = $this->getServiceLocator()->get('UploadTable');
        $allUploads = $uploadTable->fetchAll();  
        foreach($allUploads as $fileUpload) {
            //
            $uploadOwner = $userTable->getUser($fileUpload->user_id);

            // id field
            $fileUploadId= Document\Field::unIndexed('upload_id', $fileUpload->id);
            // label field
            $label = Document\Field::Text('label', $fileUpload->label);
            // owner field          
            $owner = Document\Field::Text('owner', $uploadOwner->name);


            if (substr_compare($fileUpload->filename, ".xlsx", strlen($fileUpload->filename)-strlen(".xlsx"), strlen(".xlsx")) === 0) {
                // index excel sheet
                $uploadPath    = $this->getFileUploadLocation();
                $indexDoc = Lucene\Document\Xlsx::loadXlsxFile($uploadPath ."/" . $fileUpload->filename);
            } else if (substr_compare($fileUpload->filename, ".docx", strlen($fileUpload->filename)-strlen(".docx"), strlen(".docx")) === 0) {
                // index word doc
                $uploadPath    = $this->getFileUploadLocation();
                $indexDoc = Lucene\Document\Docx::loadDocxFile($uploadPath ."/" . $fileUpload->filename);
            } else {
                $indexDoc = new Lucene\Document();
            }
            $indexDoc->addField($label);
            $indexDoc->addField($owner);
            $indexDoc->addField($fileUploadId);
            $index->addDocument($indexDoc);
        }
        $index->commit();
    }

}

【问题讨论】:

  • 请不要针对同一问题发布多个问题。

标签: php git lucene zend-framework2 search-engine


【解决方案1】:

这本书给了你奇怪的指示,因为它说 Zend Search 不能通过安装。作曲家,但这不再是这种情况并不完全正确。修复:

  1. 删除您的vendor 文件夹
  2. 编辑您的composer.json 并将zendframework/zendsearch 添加到要求部分
  3. 运行php composer.phar install 安装所有包(包括Zend Search)

那么一切都应该正常工作。

编辑:好的,由于某种原因,这个包没有在 packagist 上列出。您还需要将 repo URL 添加到您的 composer.json:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/zendframework/ZendSearch"
    }
],

然后再试一次。

【讨论】:

  • 在任何版本中都找不到请求的包zendframework/zendsearch,可能包名有错别字。
【解决方案2】:

你应该把zendsearch放在vendor文件夹的zendframework中,按照书上说的安装好后,你应该在vendor/composer/autoload_namespaces.php末尾插入这一行:

'ZendSearch' => array($vendorDir . '/zendframework/zendsearch/library')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多