PHP code example of samman / yii2-solr

1. Go to this page and download the library: Download samman/yii2-solr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samman / yii2-solr example snippets


'solr' => [
   'class' => \Samman\solr\SolrHelper::class,
   'collection' => '{collectionName}',
   'port' => 8983,
   'url' => 'http://localhost',
   'schemaFilesPath' => 'path/to/schema/'
]


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"],
];

public function solrFields(): array
{
    return ArrayHelper::merge($this->fields(), [
        'book_author_name' => function () {
            return $this->author->id;
        },
        'stock' => function () {
            return $this->quantity > 0;
        }
    ]);
}

Yii::$app->solr->createCollection(); // Create a new collection
Yii::$app->solr->dropCollection(); // Drop existing collection
Yii::$app->solr->defineSchema(); // Define the collection schema

$query = Books::find()->where(['available' => 1]);
Yii::$app->solr->indexByQuery($query);

Yii::$app->solr->indexByArray($array);

$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();

$query->asModel(Book::class)->all();

$dataProvider = new \Samman\solr\SolrDataProvider([
    'query' => Yii::$app->solr->find()->all(),
]);
bash
php composer.phar