samman/yii2-solr
Composer 安装命令:
composer require samman/yii2-solr
包简介
Yii2 SOLR (Search Engine) Extension
README 文档
README
Yii2 SOLR
Yii2 Package for SOLR active query.
BASIC FEATURES
- Create new collection
- Drop existing collection
- Define the collection schema
- Index the collection data
- Query to SOLR based on Yii2 Active Query syntax
INSTALLATION
The preferred way to install this extension is through composer.
Either run
php composer.phar require samman/yii2-solr
or add
"samman/yii2-solr": "*"
to the require section of your composer.json file.
In your main-local.php you should add the following
'solr' => [ 'class' => \Samman\solr\SolrHelper::class, 'collection' => '{collectionName}', 'port' => 8983, 'url' => 'http://localhost', 'schemaFilesPath' => 'path/to/schema/' ]
To define the collection schema, create a new file in your schema path mentioned in the main local
with, and the file name will be {collectionName}.php
Replce the {collectionName} with you collection name mentioned in main-local.php
Your Schema file should be as the following
<?php return [ ['name' => 'book_name', 'type' => 'text_general', 'multiValued' => false, 'indexed' => true, 'stored' => true], ['name' => 'book_author', 'type' => 'text_general', 'multiValued' => false, 'indexed' => true, 'stored' => true], ['name' => 'ISBN', 'type' => 'text_general', 'multiValued' => false, 'indexed' => true, 'stored' => true], ['name' => 'quantity', 'type' => 'pint', 'stored' => true], ['name' => 'price', 'type' => 'pfloat', 'multiValued' => false, 'indexed' => true, "stored" => "true"], ];
GETTING STARTED
Link SOLR to your Model
In your model you should implement Samman\solr\interfaces\SolrInterface which have
an abstract function for SOLR fields, Here's an example
public function solrFields(): array { return ArrayHelper::merge($this->fields(), [ 'book_author_name' => function () { return $this->author->id; }, 'stock' => function () { return $this->quantity > 0; } ]); }
This can be used to customize the collection fields.
Deal with Collections
You can use the below functions to work with SOLR collections.
Yii::$app->solr->createCollection(); // Create a new collection Yii::$app->solr->dropCollection(); // Drop existing collection Yii::$app->solr->defineSchema(); // Define the collection schema
To add your data to the SOLR collection, you can run:
$query = Books::find()->where(['available' => 1]); Yii::$app->solr->indexByQuery($query);
Or you can index the array data as follows
Yii::$app->solr->indexByArray($array);
SOLR Query
SOLR Query is built based on Yii QueryInterface, Here's an example
$query = Yii::$app->solr->find() ->where(['ISBN' => '356743423']) ->andWhere(['book_name' => 'Yii2']) ->orWhere(['like', 'author_name', '%samman']) ->limit(10) ->offset(2) ->indexBy('id') ->orderBy(['id' => SORT_DESC]) ->all();
The above query will return all documents which match the query.
CUSTOM FEATURES
- You can set a model for retrieved data, you can pass
asModelfunction in your query.
$query->asModel(Book::class)->all();
- You can use the
Samman\solr\SolrDataProviderif you want to return the data as a dataProvider.
$dataProvider = new \Samman\solr\SolrDataProvider([ 'query' => Yii::$app->solr->find()->all(), ]);
samman/yii2-solr 适用场景与选型建议
samman/yii2-solr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 samman/yii2-solr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samman/yii2-solr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-03-21