Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Nov 12, 2022
2 parents 69ca2f9 + 988407e commit dc74eee
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 50 deletions.
20 changes: 4 additions & 16 deletions src/Engines/ScoutPhpmorphyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@

namespace VI\ScoutPhpmorphy\Engines;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use Illuminate\Database\Eloquent\Collection;
use VI\ScoutPhpmorphy\Models\Index;
use VI\ScoutPhpmorphy\Models\Word;
use VI\ScoutPhpmorphy\Services\ArrayService;
use VI\ScoutPhpmorphy\Services\StringService;
use VI\ScoutPhpmorphy\Services\WordsService;

class ScoutPhpmorphyEngine extends Engine
{

public function update($models): Collection
{

$models->each(function ($model) use (&$params) {
$words = WordsService::prepareArray($model->toSearchableArray());
$words = Word::getOrCreateItems($words);

foreach ($words as $word) {
Index::firstOrCreate([
'index' => $model->searchableAs(),
'key' => $model->getScoutKey(),
'word_id' => $word->id,
'index' => $model->searchableAs(),
'key' => $model->getScoutKey(),
'word_id' => $word->id,
'count_words' => $word->count_words,
]);
}
Expand All @@ -47,15 +43,13 @@ public function delete($models)

public function search(Builder $builder)
{

$index = $this->buildIndexQuery($builder)
->selectRaw('`index`, `key`, COUNT(*) AS `count_lines`, SUM(`count_words`) AS `sum_count_words`')
->groupBy('index', 'key')
->orderByRaw('`count_lines` DESC, `sum_count_words` DESC')
->get();

return $index;

}

public function paginate(Builder $builder, $perPage, $page)
Expand All @@ -72,12 +66,10 @@ public function paginate(Builder $builder, $perPage, $page)
public function mapIds($results)
{
dd(__METHOD__);

}

public function map(Builder $builder, $results, $model)
{

dd($builder);

if ($results->isEmpty()) {
Expand All @@ -91,14 +83,11 @@ public function map(Builder $builder, $results, $model)
)->get()->keyBy($model->getKeyName());

return $models;

}

public function getTotalCount($results)
{

return $results->total();

}

public function flush($model)
Expand Down Expand Up @@ -134,5 +123,4 @@ protected function buildIndexQuery(Builder $builder)

return $query;
}

}
7 changes: 1 addition & 6 deletions src/Models/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

namespace VI\ScoutPhpmorphy\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Index extends Model
{

public $timestamps = false;

public function getTable()
{
return config('scout-phpmorphy.table_prefix') . 'index';
return config('scout-phpmorphy.table_prefix').'index';
}

protected $fillable = [
Expand All @@ -24,10 +21,8 @@ public function getTable()
'count_words',
];


public function word(): BelongsTo
{
return $this->belongsTo(Word::class);
}

}
24 changes: 9 additions & 15 deletions src/Models/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@

class Word extends Model
{

public $timestamps = false;

public int $count_words = 0;

public bool $is_dictionary = false;

public function getTable()
{
return config('scout-phpmorphy.table_prefix') . 'words';
return config('scout-phpmorphy.table_prefix').'words';
}

protected $fillable = [
'word',
];


public function index(): HasMany
{
return $this->hasMany(Index::class);
}


public static function getOrCreateItems($array): Collection
{

$items = new Collection;

foreach ($array as $wordItem) {
Expand All @@ -43,31 +41,27 @@ public static function getOrCreateItems($array): Collection
}

return $items;

}


public static function getItems($array): Collection
{

$items = new Collection;

if (!empty($array)) {
if (! empty($array)) {
$items = self::query()
->when(array_filter($array, fn($word) => $word['is_dictionary']),
fn(Builder $query, $words) => $query->whereIn('word', collect($words)->pluck('word')))
->when(array_filter($array, fn($word) => !$word['is_dictionary']),
->when(array_filter($array, fn ($word) => $word['is_dictionary']),
fn (Builder $query, $words) => $query->whereIn('word', collect($words)->pluck('word')))
->when(array_filter($array, fn ($word) => ! $word['is_dictionary']),
function (Builder $query, $words) {
foreach ($words as $word) {
$query->orWhere('word', 'LIKE', $word['word'] . '%');
$query->orWhere('word', 'LIKE', $word['word'].'%');
}

return $query;
})
->get();
}

return $items;

}

}
2 changes: 0 additions & 2 deletions src/ScoutPhpmorphyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ScoutPhpmorphyServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{

/*
* This class is a Package Service Provider
*
Expand All @@ -21,7 +20,6 @@ public function configurePackage(Package $package): void
->name('scout-phpmorphy')
->hasConfigFile()
->hasMigrations(['create_phpmorphy_words_table', 'create_phpmorphy_index_table']);

}

public function boot()
Expand Down
3 changes: 1 addition & 2 deletions src/Services/ArrayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ class ArrayService
{
public static function flatten($array): array
{

$result = [];

foreach ($array as $value) {
if (is_scalar($value)) {
$result[] = $value;
} else {
foreach (self::flatten((array)$value) as $subValue) {
foreach (self::flatten((array) $value) as $subValue) {
$result[] = $subValue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/StringService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class StringService
{

public static function toArray(string $string)
{
$string = strip_tags($string);

return preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $string);
}
}
14 changes: 6 additions & 8 deletions src/Services/WordsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace VI\ScoutPhpmorphy\Services;

use cijic\phpMorphy\Facade\Morphy;

class WordsService
{

public static function prepareString(string $string)
{
$words = StringService::toArray($string);
Expand All @@ -28,28 +25,29 @@ protected static function prepare(array $array)
});
$words = ArrayService::flatten($words);
$words = collect($words)
->map(fn($word) => str($word)->upper()->trim()->toString())
->map(fn ($word) => str($word)->upper()->trim()->toString())
->filter()
->filter(fn($word) => str($word)->length() >= 2);
->filter(fn ($word) => str($word)->length() >= 2);

$morphy = new \cijic\phpMorphy\Morphy();

$words = $words->map(function ($word) use ($morphy) {
$array = [
[
'is_dictionary' => false,
'word' => $word,
'word' => $word,
],
];
if ($morphyWord = $morphy->getPseudoRoot($word)) {
$array = [];
foreach ($morphyWord as $morphyWordNew) {
$array[] = [
'is_dictionary' => true,
'word' => $morphyWordNew,
'word' => $morphyWordNew,
];
}
}

return $array;
});

Expand All @@ -68,7 +66,7 @@ protected static function prepare(array $array)
foreach ($temp as $wordItems) {
$wordItems['word'] = trim($wordItems['word']);
if ($wordItems['word']) {
if (!isset($words[$wordItems['word']])) {
if (! isset($words[$wordItems['word']])) {
$words[$wordItems['word']] = $wordItems;
$words[$wordItems['word']]['count'] = 0;
}
Expand Down

0 comments on commit dc74eee

Please sign in to comment.