定制 samman/yii2-solr 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

samman/yii2-solr

最新稳定版本:0.1.0

Composer 安装命令:

composer require samman/yii2-solr

包简介

Yii2 SOLR (Search Engine) Extension

README 文档

README

Yii2 SOLR

Yii2 Package for SOLR active query.

Yii2 Framework

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 asModel function in your query.
$query->asModel(Book::class)->all();
  • You can use the Samman\solr\SolrDataProvider if you want to return the data as a dataProvider.
$dataProvider = new \Samman\solr\SolrDataProvider([
    'query' => Yii::$app->solr->find()->all(),
]);

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-03-21