Added per-entity weighting changes.
Now Books score higher than chapters which score higher than pages.
Reduced queries required on search by only searching once but at a
higher count to see if there's another page.
class Book extends Entity
{
class Book extends Entity
{
+ public $searchFactor = 2;
protected $fillable = ['name', 'description', 'image_id'];
protected $fillable = ['name', 'description', 'image_id'];
class Chapter extends Entity
{
class Chapter extends Entity
{
+ public $searchFactor = 1.3;
+
protected $fillable = ['name', 'description', 'priority', 'book_id'];
protected $with = ['book'];
protected $fillable = ['name', 'description', 'priority', 'book_id'];
protected $with = ['book'];
class Entity extends Ownable
{
class Entity extends Ownable
{
+ /**
+ * @var string - Name of property where the main text content is found
+ */
public $textField = 'description';
public $textField = 'description';
+ /**
+ * @var float - Multiplier for search indexing.
+ */
+ public $searchFactor = 1.0;
+
/**
* Compares this entity to another given entity.
* Matches by comparing class and id.
/**
* Compares this entity to another given entity.
* Matches by comparing class and id.
$nextPageLink = baseUrl('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
$results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
$nextPageLink = baseUrl('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
$results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
- $hasNextPage = $this->searchService->searchEntities($searchTerm, 'all', $page+1, 20)['count'] > 0;
return view('search/all', [
'entities' => $results['results'],
'totalResults' => $results['total'],
'searchTerm' => $searchTerm,
return view('search/all', [
'entities' => $results['results'],
'totalResults' => $results['total'],
'searchTerm' => $searchTerm,
- 'hasNextPage' => $hasNextPage,
+ 'hasNextPage' => $results['has_more'],
'nextPageLink' => $nextPageLink
]);
}
'nextPageLink' => $nextPageLink
]);
}
$terms = $this->parseSearchString($searchString);
$entityTypes = array_keys($this->entities);
$entityTypesToSearch = $entityTypes;
$terms = $this->parseSearchString($searchString);
$entityTypes = array_keys($this->entities);
$entityTypesToSearch = $entityTypes;
if ($entityType !== 'all') {
$entityTypesToSearch = $entityType;
if ($entityType !== 'all') {
$entityTypesToSearch = $entityType;
$entityTypesToSearch = explode('|', $terms['filters']['type']);
}
$entityTypesToSearch = explode('|', $terms['filters']['type']);
}
$total = 0;
foreach ($entityTypesToSearch as $entityType) {
if (!in_array($entityType, $entityTypes)) {
continue;
}
$total = 0;
foreach ($entityTypesToSearch as $entityType) {
if (!in_array($entityType, $entityTypes)) {
continue;
}
- $search = $this->searchEntityTable($terms, $entityType, $page, $count);
- $total += $this->searchEntityTable($terms, $entityType, $page, $count, true);
+ $search = $this->searchEntityTable($terms, $entityType, $page, $count + 1);
+ $total += $this->searchEntityTable($terms, $entityType, $page, $count + 1, true);
$results = $results->merge($search);
}
return [
'total' => $total,
'count' => count($results),
$results = $results->merge($search);
}
return [
'total' => $total,
'count' => count($results),
- 'results' => $results->sortByDesc('score')->values()
+ 'has_more' => $results->count() > $count,
+ 'results' => $results->sortByDesc('score')->slice(0, $count)->values()
public function indexEntity(Entity $entity)
{
$this->deleteEntityTerms($entity);
public function indexEntity(Entity $entity)
{
$this->deleteEntityTerms($entity);
- $nameTerms = $this->generateTermArrayFromText($entity->name, 5);
- $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1);
+ $nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
+ $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
$terms = array_merge($nameTerms, $bodyTerms);
foreach ($terms as $index => $term) {
$terms[$index]['entity_type'] = $entity->getMorphClass();
$terms = array_merge($nameTerms, $bodyTerms);
foreach ($terms as $index => $term) {
$terms[$index]['entity_type'] = $entity->getMorphClass();
{
$terms = [];
foreach ($entities as $entity) {
{
$terms = [];
foreach ($entities as $entity) {
- $nameTerms = $this->generateTermArrayFromText($entity->name, 5);
- $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1);
+ $nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
+ $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
foreach (array_merge($nameTerms, $bodyTerms) as $term) {
$term['entity_id'] = $entity->id;
$term['entity_type'] = $entity->getMorphClass();
foreach (array_merge($nameTerms, $bodyTerms) as $term) {
$term['entity_id'] = $entity->id;
$term['entity_type'] = $entity->getMorphClass();