2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2022-08-16 06:27:22 -04:00
|
|
|
namespace BookStack\Search;
|
2020-11-21 19:17:45 -05:00
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\Models\Tag;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\EntityProvider;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
2021-11-12 08:47:23 -05:00
|
|
|
use BookStack\Entities\Models\Page;
|
2023-11-14 10:46:32 -05:00
|
|
|
use BookStack\Util\HtmlDocument;
|
2021-11-12 08:47:23 -05:00
|
|
|
use DOMNode;
|
2021-11-22 18:33:55 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-11-28 11:42:12 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2020-11-21 19:17:45 -05:00
|
|
|
|
|
|
|
class SearchIndex
|
|
|
|
{
|
2021-11-12 13:03:44 -05:00
|
|
|
/**
|
|
|
|
* A list of delimiter characters used to break-up parsed content into terms for indexing.
|
|
|
|
*/
|
2023-02-23 17:59:26 -05:00
|
|
|
public static string $delimiters = " \n\t.,!?:;()[]{}<>`'\"";
|
2020-11-21 19:17:45 -05:00
|
|
|
|
2023-02-23 17:59:26 -05:00
|
|
|
public function __construct(
|
|
|
|
protected EntityProvider $entityProvider
|
|
|
|
) {
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index the given entity.
|
|
|
|
*/
|
2023-02-23 17:59:26 -05:00
|
|
|
public function indexEntity(Entity $entity): void
|
2020-11-21 19:17:45 -05:00
|
|
|
{
|
|
|
|
$this->deleteEntityTerms($entity);
|
2021-11-11 08:36:49 -05:00
|
|
|
$terms = $this->entityToTermDataArray($entity);
|
|
|
|
SearchTerm::query()->insert($terms);
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Index multiple Entities at once.
|
|
|
|
*
|
2020-11-21 19:17:45 -05:00
|
|
|
* @param Entity[] $entities
|
|
|
|
*/
|
2023-02-23 17:59:26 -05:00
|
|
|
public function indexEntities(array $entities): void
|
2020-11-21 19:17:45 -05:00
|
|
|
{
|
|
|
|
$terms = [];
|
|
|
|
foreach ($entities as $entity) {
|
2021-11-11 08:36:49 -05:00
|
|
|
$entityTerms = $this->entityToTermDataArray($entity);
|
|
|
|
array_push($terms, ...$entityTerms);
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$chunkedTerms = array_chunk($terms, 500);
|
|
|
|
foreach ($chunkedTerms as $termChunk) {
|
2021-11-11 08:36:49 -05:00
|
|
|
SearchTerm::query()->insert($termChunk);
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete and re-index the terms for all entities in the system.
|
2021-11-11 09:10:11 -05:00
|
|
|
* Can take a callback which is used for reporting progress.
|
|
|
|
* Callback receives three arguments:
|
|
|
|
* - An instance of the model being processed
|
|
|
|
* - The number that have been processed so far.
|
|
|
|
* - The total number of that model to be processed.
|
|
|
|
*
|
2021-11-20 09:03:56 -05:00
|
|
|
* @param callable(Entity, int, int):void|null $progressCallback
|
2020-11-21 19:17:45 -05:00
|
|
|
*/
|
2023-02-23 17:59:26 -05:00
|
|
|
public function indexAllEntities(?callable $progressCallback = null): void
|
2020-11-21 19:17:45 -05:00
|
|
|
{
|
2021-11-11 08:36:49 -05:00
|
|
|
SearchTerm::query()->truncate();
|
2020-11-21 19:17:45 -05:00
|
|
|
|
|
|
|
foreach ($this->entityProvider->all() as $entityModel) {
|
2021-11-12 08:47:23 -05:00
|
|
|
$indexContentField = $entityModel instanceof Page ? 'html' : 'description';
|
|
|
|
$selectFields = ['id', 'name', $indexContentField];
|
2021-11-22 18:33:55 -05:00
|
|
|
/** @var Builder<Entity> $query */
|
|
|
|
$query = $entityModel->newQuery();
|
|
|
|
$total = $query->withTrashed()->count();
|
2021-11-11 09:10:11 -05:00
|
|
|
$chunkSize = 250;
|
|
|
|
$processed = 0;
|
|
|
|
|
|
|
|
$chunkCallback = function (Collection $entities) use ($progressCallback, &$processed, $total, $chunkSize, $entityModel) {
|
|
|
|
$this->indexEntities($entities->all());
|
|
|
|
$processed = min($processed + $chunkSize, $total);
|
|
|
|
|
|
|
|
if (is_callable($progressCallback)) {
|
|
|
|
$progressCallback($entityModel, $processed, $total);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
$entityModel->newQuery()
|
|
|
|
->select($selectFields)
|
2021-11-12 12:06:01 -05:00
|
|
|
->with(['tags:id,name,value,entity_id,entity_type'])
|
2021-11-11 09:10:11 -05:00
|
|
|
->chunk($chunkSize, $chunkCallback);
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete related Entity search terms.
|
|
|
|
*/
|
2023-02-23 17:59:26 -05:00
|
|
|
public function deleteEntityTerms(Entity $entity): void
|
2020-11-21 19:17:45 -05:00
|
|
|
{
|
|
|
|
$entity->searchTerms()->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-12 08:47:23 -05:00
|
|
|
* Create a scored term array from the given text, where the keys are the terms
|
|
|
|
* and the values are their scores.
|
2021-11-11 08:36:49 -05:00
|
|
|
*
|
2021-11-12 08:47:23 -05:00
|
|
|
* @returns array<string, int>
|
2020-11-21 19:17:45 -05:00
|
|
|
*/
|
2023-01-21 15:50:04 -05:00
|
|
|
protected function generateTermScoreMapFromText(string $text, float $scoreAdjustment = 1): array
|
2021-11-12 08:47:23 -05:00
|
|
|
{
|
|
|
|
$termMap = $this->textToTermCountMap($text);
|
|
|
|
|
|
|
|
foreach ($termMap as $term => $count) {
|
2023-01-21 15:50:04 -05:00
|
|
|
$termMap[$term] = floor($count * $scoreAdjustment);
|
2021-11-12 08:47:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $termMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a scored term array from the given HTML, where the keys are the terms
|
|
|
|
* and the values are their scores.
|
|
|
|
*
|
|
|
|
* @returns array<string, int>
|
|
|
|
*/
|
|
|
|
protected function generateTermScoreMapFromHtml(string $html): array
|
|
|
|
{
|
|
|
|
if (empty($html)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$scoresByTerm = [];
|
|
|
|
$elementScoreAdjustmentMap = [
|
|
|
|
'h1' => 10,
|
|
|
|
'h2' => 5,
|
|
|
|
'h3' => 4,
|
|
|
|
'h4' => 3,
|
|
|
|
'h5' => 2,
|
|
|
|
'h6' => 1.5,
|
|
|
|
];
|
|
|
|
|
2022-06-20 18:47:42 -04:00
|
|
|
$html = str_ireplace(['<br>', '<br />', '<br/>'], "\n", $html);
|
2023-11-14 10:46:32 -05:00
|
|
|
$doc = new HtmlDocument($html);
|
2022-06-20 18:47:42 -04:00
|
|
|
|
2021-11-12 08:47:23 -05:00
|
|
|
/** @var DOMNode $child */
|
2023-11-14 10:46:32 -05:00
|
|
|
foreach ($doc->getBodyChildren() as $child) {
|
2021-11-12 08:47:23 -05:00
|
|
|
$nodeName = $child->nodeName;
|
|
|
|
$termCounts = $this->textToTermCountMap(trim($child->textContent));
|
|
|
|
foreach ($termCounts as $term => $count) {
|
|
|
|
$scoreChange = $count * ($elementScoreAdjustmentMap[$nodeName] ?? 1);
|
|
|
|
$scoresByTerm[$term] = ($scoresByTerm[$term] ?? 0) + $scoreChange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scoresByTerm;
|
|
|
|
}
|
|
|
|
|
2021-11-12 12:06:01 -05:00
|
|
|
/**
|
|
|
|
* Create a scored term map from the given set of entity tags.
|
|
|
|
*
|
|
|
|
* @param Tag[] $tags
|
|
|
|
*
|
|
|
|
* @returns array<string, int>
|
|
|
|
*/
|
|
|
|
protected function generateTermScoreMapFromTags(array $tags): array
|
|
|
|
{
|
|
|
|
$names = [];
|
|
|
|
$values = [];
|
|
|
|
|
2021-11-13 08:28:17 -05:00
|
|
|
foreach ($tags as $tag) {
|
2021-11-12 12:06:01 -05:00
|
|
|
$names[] = $tag->name;
|
|
|
|
$values[] = $tag->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nameMap = $this->generateTermScoreMapFromText(implode(' ', $names), 3);
|
|
|
|
$valueMap = $this->generateTermScoreMapFromText(implode(' ', $values), 5);
|
|
|
|
|
|
|
|
return $this->mergeTermScoreMaps($nameMap, $valueMap);
|
|
|
|
}
|
|
|
|
|
2021-11-12 08:47:23 -05:00
|
|
|
/**
|
|
|
|
* For the given text, return an array where the keys are the unique term words
|
|
|
|
* and the values are the frequency of that term.
|
|
|
|
*
|
|
|
|
* @returns array<string, int>
|
|
|
|
*/
|
|
|
|
protected function textToTermCountMap(string $text): array
|
2020-11-21 19:17:45 -05:00
|
|
|
{
|
|
|
|
$tokenMap = []; // {TextToken => OccurrenceCount}
|
2021-11-12 13:03:44 -05:00
|
|
|
$splitChars = static::$delimiters;
|
2020-11-21 19:17:45 -05:00
|
|
|
$token = strtok($text, $splitChars);
|
|
|
|
|
|
|
|
while ($token !== false) {
|
|
|
|
if (!isset($tokenMap[$token])) {
|
|
|
|
$tokenMap[$token] = 0;
|
|
|
|
}
|
|
|
|
$tokenMap[$token]++;
|
|
|
|
$token = strtok($splitChars);
|
|
|
|
}
|
|
|
|
|
2021-11-12 08:47:23 -05:00
|
|
|
return $tokenMap;
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|
2021-11-11 08:36:49 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For the given entity, Generate an array of term data details.
|
|
|
|
* Is the raw term data, not instances of SearchTerm models.
|
|
|
|
*
|
2021-11-12 08:47:23 -05:00
|
|
|
* @returns array{term: string, score: float, entity_id: int, entity_type: string}[]
|
2021-11-11 08:36:49 -05:00
|
|
|
*/
|
|
|
|
protected function entityToTermDataArray(Entity $entity): array
|
|
|
|
{
|
2021-11-12 08:47:23 -05:00
|
|
|
$nameTermsMap = $this->generateTermScoreMapFromText($entity->name, 40 * $entity->searchFactor);
|
2021-11-12 12:06:01 -05:00
|
|
|
$tagTermsMap = $this->generateTermScoreMapFromTags($entity->tags->all());
|
2021-11-12 08:47:23 -05:00
|
|
|
|
|
|
|
if ($entity instanceof Page) {
|
|
|
|
$bodyTermsMap = $this->generateTermScoreMapFromHtml($entity->html);
|
|
|
|
} else {
|
2021-11-22 18:33:55 -05:00
|
|
|
$bodyTermsMap = $this->generateTermScoreMapFromText($entity->getAttribute('description') ?? '', $entity->searchFactor);
|
2021-11-12 08:47:23 -05:00
|
|
|
}
|
2021-11-11 08:36:49 -05:00
|
|
|
|
2021-11-12 12:06:01 -05:00
|
|
|
$mergedScoreMap = $this->mergeTermScoreMaps($nameTermsMap, $bodyTermsMap, $tagTermsMap);
|
2021-11-12 08:47:23 -05:00
|
|
|
|
|
|
|
$dataArray = [];
|
|
|
|
$entityId = $entity->id;
|
|
|
|
$entityType = $entity->getMorphClass();
|
|
|
|
foreach ($mergedScoreMap as $term => $score) {
|
|
|
|
$dataArray[] = [
|
2021-11-13 08:28:17 -05:00
|
|
|
'term' => $term,
|
|
|
|
'score' => $score,
|
2021-11-12 08:47:23 -05:00
|
|
|
'entity_type' => $entityType,
|
2021-11-13 08:28:17 -05:00
|
|
|
'entity_id' => $entityId,
|
2021-11-12 08:47:23 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dataArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the given term data arrays, Merge their contents by term
|
|
|
|
* while combining any scores.
|
|
|
|
*
|
|
|
|
* @param array<string, int>[] ...$scoreMaps
|
|
|
|
*
|
|
|
|
* @returns array<string, int>
|
|
|
|
*/
|
|
|
|
protected function mergeTermScoreMaps(...$scoreMaps): array
|
|
|
|
{
|
|
|
|
$mergedMap = [];
|
|
|
|
|
|
|
|
foreach ($scoreMaps as $scoreMap) {
|
|
|
|
foreach ($scoreMap as $term => $score) {
|
|
|
|
$mergedMap[$term] = ($mergedMap[$term] ?? 0) + $score;
|
|
|
|
}
|
2021-11-11 08:36:49 -05:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:47:23 -05:00
|
|
|
return $mergedMap;
|
2021-11-11 08:36:49 -05:00
|
|
|
}
|
2020-11-21 19:17:45 -05:00
|
|
|
}
|