微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Verity在ColdFusion 9中索引和搜索数据库内容?

我曾尝试使用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>

原文地址:https://www.jb51.cc/mssql/77047.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐