mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-26 04:05:57 +00:00
Refactored search indexer, Increase title/name score boost
- Title score boost changed from 5 to 40 (8x increase). - Extracted entity parsing to its own function
This commit is contained in:
parent
0ddd052818
commit
9f32613982
@ -9,19 +9,14 @@ use Illuminate\Support\Collection;
|
|||||||
|
|
||||||
class SearchIndex
|
class SearchIndex
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var SearchTerm
|
|
||||||
*/
|
|
||||||
protected $searchTerm;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EntityProvider
|
* @var EntityProvider
|
||||||
*/
|
*/
|
||||||
protected $entityProvider;
|
protected $entityProvider;
|
||||||
|
|
||||||
public function __construct(SearchTerm $searchTerm, EntityProvider $entityProvider)
|
public function __construct(EntityProvider $entityProvider)
|
||||||
{
|
{
|
||||||
$this->searchTerm = $searchTerm;
|
|
||||||
$this->entityProvider = $entityProvider;
|
$this->entityProvider = $entityProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,14 +26,8 @@ class SearchIndex
|
|||||||
public function indexEntity(Entity $entity)
|
public function indexEntity(Entity $entity)
|
||||||
{
|
{
|
||||||
$this->deleteEntityTerms($entity);
|
$this->deleteEntityTerms($entity);
|
||||||
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
|
$terms = $this->entityToTermDataArray($entity);
|
||||||
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
|
SearchTerm::query()->insert($terms);
|
||||||
$terms = array_merge($nameTerms, $bodyTerms);
|
|
||||||
foreach ($terms as $index => $term) {
|
|
||||||
$terms[$index]['entity_type'] = $entity->getMorphClass();
|
|
||||||
$terms[$index]['entity_id'] = $entity->id;
|
|
||||||
}
|
|
||||||
$this->searchTerm->newQuery()->insert($terms);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,18 +39,13 @@ class SearchIndex
|
|||||||
{
|
{
|
||||||
$terms = [];
|
$terms = [];
|
||||||
foreach ($entities as $entity) {
|
foreach ($entities as $entity) {
|
||||||
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
|
$entityTerms = $this->entityToTermDataArray($entity);
|
||||||
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
|
array_push($terms, ...$entityTerms);
|
||||||
foreach (array_merge($nameTerms, $bodyTerms) as $term) {
|
|
||||||
$term['entity_id'] = $entity->id;
|
|
||||||
$term['entity_type'] = $entity->getMorphClass();
|
|
||||||
$terms[] = $term;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$chunkedTerms = array_chunk($terms, 500);
|
$chunkedTerms = array_chunk($terms, 500);
|
||||||
foreach ($chunkedTerms as $termChunk) {
|
foreach ($chunkedTerms as $termChunk) {
|
||||||
$this->searchTerm->newQuery()->insert($termChunk);
|
SearchTerm::query()->insert($termChunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +54,7 @@ class SearchIndex
|
|||||||
*/
|
*/
|
||||||
public function indexAllEntities()
|
public function indexAllEntities()
|
||||||
{
|
{
|
||||||
$this->searchTerm->newQuery()->truncate();
|
SearchTerm::query()->truncate();
|
||||||
|
|
||||||
foreach ($this->entityProvider->all() as $entityModel) {
|
foreach ($this->entityProvider->all() as $entityModel) {
|
||||||
$selectFields = ['id', 'name', $entityModel->textField];
|
$selectFields = ['id', 'name', $entityModel->textField];
|
||||||
@ -93,6 +77,8 @@ class SearchIndex
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a scored term array from the given text.
|
* Create a scored term array from the given text.
|
||||||
|
*
|
||||||
|
* @returns array{term: string, score: float}
|
||||||
*/
|
*/
|
||||||
protected function generateTermArrayFromText(string $text, int $scoreAdjustment = 1): array
|
protected function generateTermArrayFromText(string $text, int $scoreAdjustment = 1): array
|
||||||
{
|
{
|
||||||
@ -118,4 +104,24 @@ class SearchIndex
|
|||||||
|
|
||||||
return $terms;
|
return $terms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For the given entity, Generate an array of term data details.
|
||||||
|
* Is the raw term data, not instances of SearchTerm models.
|
||||||
|
*
|
||||||
|
* @returns array{term: string, score: float}[]
|
||||||
|
*/
|
||||||
|
protected function entityToTermDataArray(Entity $entity): array
|
||||||
|
{
|
||||||
|
$nameTerms = $this->generateTermArrayFromText($entity->name, 40 * $entity->searchFactor);
|
||||||
|
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
|
||||||
|
$termData = array_merge($nameTerms, $bodyTerms);
|
||||||
|
|
||||||
|
foreach ($termData as $index => $term) {
|
||||||
|
$termData[$index]['entity_type'] = $entity->getMorphClass();
|
||||||
|
$termData[$index]['entity_id'] = $entity->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $termData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user