易优搜索全站内容并从文章内容里搜索关键字解决方法

2022-08-08

以下文章来自网友夜月游弋:

易优搜索默认的是从文章标题里面搜索,因为数据库储存的标题内容是纯文本,所以不用过滤。但是有些时候需要全站搜索并从文章内容中检索关键词,这时候默认的搜索就显得捉襟见肘了。其实我们灵活运用sql语句直接查询就可以实现!如下:

{eyou:sql sql="select aid from ey_article_content where content like '%$keywords%' union select aid from ey_archives where title like '%$keywords%' union select aid from ey_product_content where content like '%$keywords%' limit 0,5" }
{eyou:sql sql="select aid from ey_archives where aid='$aid'" id="field3"}
<li class="dot">
{eyou:arcview aid="$field3.aid" id="field2" addfields="content" }
<a href="{$field2.arcurl}" title="{$field2.title}"><h1>{$keywords,"<font color='red'>$keywords</font>",$field2.title|str_replace}</h1></a>
{eyou:assign name="neirong" value="$field2.content|html_msubstr"}
<div>{$neirong,$keywords|diy_content}</div>
<span>{$field2.add_time|MyDate='Y-m-d',###}</span>
</li>
{/eyou:arcview}
{/eyou:sql} 
{/eyou:sql}

这里是利用联合查询从文章内容和标题中查询包含关键字的文章aid,并去重,然后将aid套入arcview标签,循环输出文章。

这样即可输出文章内容或标题包含关键词的文章列表,全站搜索同理可得。

但是有一个问题,易优数据库content字段储存的是带有html标签的内容,如果搜索像“a”“p”“br”这样的html标签,就会出现全部的文章。勉强能用。


Tag: 易优 eyoucms