如何使用Verity在ColdFusion 9中索引和搜索数据库内容?
发布时间:2020-05-24 08:00:45 所属栏目:MsSql 来源:互联网
导读:我曾尝试使用ColdFusion 9在我的网站上构建搜索引擎.关键是Verity,我读到它是在我的数据库内容中进行索引和搜索的最佳工具. 但我搜索任何教程都没有运气,告诉我如何做到这一点,即使教程缺失,或者我认为我没有找到它. 我正在使用ColdFusion 9和MySQL服务器.你
|
我曾尝试使用ColdFusion 9在我的网站上构建搜索引擎.关键是Verity,我读到它是在我的数据库内容中进行索引和搜索的最佳工具. 但我搜索任何教程都没有运气,告诉我如何做到这一点,即使教程缺失,或者我认为我没有找到它. 我正在使用ColdFusion 9和MySQL服务器.你能建议我怎么做吗?或者欢迎任何教程,文章或电子书. 解决方法实际上,CF9有两个很棒的引擎: Verity(经典)和 Solr(现代).他们都实现了收藏的想法.创建和维护集合非常明显,可以在手册中找到(参见前面的链接). 您可以在cfindex标签手册页上找到主要提示:您可以使用查询数据填充(更新)集合.设置类型自定义,输入查询名称和所需的所有列(组合可能会有所不同). 之后你需要的就是使用cfsearch. 此外,我可以建议设置由调度程序执行的脚本,以定期刷新您的集合. 编辑 示例代码(注意:代码未经过测试,只是我旧组件的简化版).这是CFC的两种方法. <cffunction name="index" access="public" returntype="any" output="true" hint="Rebuild search index">
<cfargument name="collection" type="string" required="true" hint="Target collection name">
<cfset var local = {} />
<cftry>
<!--- pull the content --->
<cfquery datasource="#variables.dsn#" name="local.getContent">
SELECT C.id,C.title,C.content,P.name AS page
FROM #variables.tableContent# C
INNER JOIN #variables.tablePages# P
ON C.id_page = P.id
</cfquery>
<!--- update collection --->
<cflock name="cfindex_lock" type="exclusive" timeout="30">
<cfindex collection="#arguments.collection#"
action="refresh"
type="custom"
query="local.getContent"
key="id"
custom1="page"
title="title"
body="title,content"
>
</cflock>
<cfreturn true />
<cfcatch type="any">
<!--- custom error handler here --->
<cfreturn false />
</cfcatch>
</cftry>
</cffunction>
<cffunction name="search" access="public" returntype="any" output="true" hint="Perform search through the collection">
<cfargument name="collection" type="string" required="true" hint="Target collection name">
<cfargument name="type" type="string" required="true" hint="Search type">
<cfargument name="criteria" type="string" required="true" hint="Search criteria">
<cfargument name="startrow" type="numeric" required="false" default="1" hint="Select offset">
<cfargument name="maxrows" type="numeric" required="false" default="50" hint="Select items count">
<cfset var local = {} />
<cftry>
<!--- pull the data from collection --->
<cfsearch collection="#arguments.collection#"
name="local.searchResults"
type="#arguments.type#"
criteria="#LCase(arguments.criteria)#"
startrow="#arguments.startrow#"
maxrows="#arguments.maxrows#"
>
<cfset local.resultsArray = [] />
<!--- convert data into the array --->
<cfloop query="local.searchResults">
<cfscript>
local.res = StructNew();
local.res["id"] = local.searchResults.key;
local.res["summary"] = Left(local.searchResults.summary,500) & "...";
// highlight the search phrase
local.res["summary"] = ReplaceNoCase(local.res["summary"],arguments.criteria,"<strong>" & arguments.criteria & "</strong>","ALL");
local.res["page"] = local.searchResults.custom1;
local.res["title"] = local.searchResults.title;
ArrayAppend(local.resultsArray,local.res);
</cfscript>
</cfloop>
<cfreturn local.resultsArray />
<cfcatch type="any">
<!--- custom error handler here --->
<cfreturn false />
</cfcatch>
</cftry>
</cffunction> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
