D
D
Denwebart2015-08-04 19:52:40
Laravel
Denwebart, 2015-08-04 19:52:40

Looking for similar articles in Laravel 4.2?

Tell me how to implement an automatic search for similar articles?
Is it necessary that similar articles are searched by keywords, by title, by internal links in the text, and that the results are sorted by the most similar ones?
Tried to do this, this is what happened:

public function related($page, $limit = 5)
  {
    $metaKey = $page->meta_key ? str_replace(',', '|', $page->meta_key) . '|' : '';
    $keywords = $metaKey . StringHelper::autoMetaKeywords($page->title . ' ' . $page->content, 5, '|');

    $pages0 = $page->relatedArticles;

    $pages1 = Page::whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'))
      ->where('id', '!=', $page->id)
      ->whereType(Page::TYPE_PAGE)
      ->whereIsContainer(0)
      ->where('parent_id', '!=', 0)
      ->whereRaw('LOWER(title) LIKE LOWER("%'. str_replace('|', '%', $keywords) .'%")')
      ->with('parent.parent', 'user')
      ->limit($limit)
      ->get(['id', 'parent_id', 'published_at', 'user_id', 'is_published', 'is_container', 'title', 'alias', 'type']);

    $pages2 = Page::whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'))
      ->where('id', '!=', $page->id)
      ->whereType(Page::TYPE_PAGE)
      ->whereIsContainer(0)
      ->where('parent_id', '!=', 0)
      ->whereRaw('LOWER(content) LIKE LOWER("%'. str_replace('|', '%', $keywords) .'%")')
      ->with('parent.parent', 'user')
      ->limit($limit)
      ->get(['id', 'parent_id', 'published_at', 'user_id', 'is_published', 'is_container', 'title', 'alias', 'type']);


    $pages3 = Page::whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'))
      ->where('id', '!=', $page->id)
      ->whereType(Page::TYPE_PAGE)
      ->whereIsContainer(0)
      ->where('parent_id', '!=', 0)
      ->whereRaw('LOWER(title) REGEXP LOWER("' . $keywords . '")')
      ->with('parent.parent', 'user')
      ->limit($limit)
      ->get(['id', 'parent_id', 'published_at', 'user_id', 'is_published', 'is_container', 'title', 'alias', 'type']);

    $pages4 = Page::whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'))
      ->where('id', '!=', $page->id)
      ->whereType(Page::TYPE_PAGE)
      ->whereIsContainer(0)
      ->where('parent_id', '!=', 0)
      ->whereRaw('LOWER(content) REGEXP LOWER("' . $keywords . '")')
      ->with('parent.parent', 'user')
      ->with('parent.parent', 'user')
      ->limit($limit)
      ->get(['id', 'parent_id', 'published_at', 'user_id', 'is_published', 'is_container', 'title', 'alias', 'type']);

    $pages5 = Page::whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'))
      ->where('id', '!=', $page->id)
      ->whereType(Page::TYPE_PAGE)
      ->whereIsContainer(0)
      ->where('parent_id', '!=', 0)
      ->whereParentId($page->parent_id)
      ->with('parent.parent', 'user')
      ->limit($limit)
      ->get(['id', 'parent_id', 'published_at', 'user_id', 'is_published', 'is_container', 'title', 'alias', 'type']);

    $pages = $pages0->merge($pages1);
    $pages = $pages->merge($pages2);
    $pages = $pages->merge($pages3);
    $pages = $pages->merge($pages4);
    $pages = $pages->merge($pages5);
    $pages = $pages->slice(0, $limit);

    return (string) View::make('widgets.related.index', compact('pages'))->render();
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Elchev, 2015-08-06
@rsi

I would zhahnut some kind of sphinx.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question