如何使用Verity在ColdFusion 9中编制索引和搜索数据库内容? [英] How can I use Verity to index and search database content in ColdFusion 9?

查看:142
本文介绍了如何使用Verity在ColdFusion 9中编制索引和搜索数据库内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ColdFusion 9在我的网站建立搜索引擎。关键是Verity,我读它是在我的数据库内容中进行索引和搜索的最好的工具。



但是我搜索周围没有运气关于任何教程我如何做到这一点,即使一个教程缺失,或者我认为我没有找到它。



我使用ColdFusion 9与MySQL服务器。你能告诉我如何做到这一点吗?

解决方案

实际上,CF9有两个伟大的引擎: a href =http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf364104-7ffc.html =nofollow noreferrer> Verity (classic)和 Solr (现代)。



两者都实现了集合的想法。



您可以在 cfindex 标签手册页:您可以使用查询数据。设置类型 ,输入查询名称和您需要的所有列(组合可能不同)。



a href =http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d02.html =nofollow noreferrer> cfsearch 。



此外,我建议您设置由计划程序执行的脚本,以定期刷新您的收藏。



EDIT >

示例代码(注:代码未测试,只是我的旧组件的简化剪切)。这些是CFC的两种方法。

 < cffunction name =indexaccess =publicreturntype =any output =truehint =重建搜索索引> 
< cfargument name =collectiontype =stringrequired =truehint =目标集合名称>
< 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_locktype =exclusivetimeout =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>
<!---自定义错误处理程序here --->
< cfreturn false />
< / cfcatch>
< / cftry>
< / cffunction>



< cffunction name =searchaccess =publicreturntype =anyoutput =truehint =通过集合执行搜索
< cfargument name =collectiontype =stringrequired =truehint =目标集合名称>
< cfargument name =typetype =stringrequired =truehint =搜索类型>
< cfargument name =criteriatype =stringrequired =truehint =搜索条件>
< cfargument name =startrowtype =numericrequired =falsedefault =1hint =Select offset>
< cfargument name =maxrowstype =numericrequired =falsedefault =50hint =选择项目数>
< cfset var local = {} />
< cftry>

<!---从收集中提取数据--->
< cfsearch collection =#arguments.collection#
name =local.searchResults
type =#arguments.type#
criteria =#LCase arguments.criteria)#
startrow =#arguments.startrow#
maxrows =#arguments.maxrows#
>


< cfset local.resultsArray = [] />

<!---将数据转换为数组--->
< cfloop query =local.searchResults>
< cfscript>
local.res = StructNew();
local.res [id] = local.searchResults.key;
local.res [summary] = Left(local.searchResults.summary,500)& ...;
//突出显示搜索短语
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>

< cfreturn local.resultsArray />

< cfcatch type =any>
<!---自定义错误处理程序here --->
< cfreturn false />
< / cfcatch>
< / cftry>
< / cffunction>


I have tried to use ColdFusion 9 to build search engine in my site. The key is Verity which I read it is the best tool to do the indexing and searching in my database content.

But I search around with no luck about any tutorial to tell me how to done this, even a tutorial is missing, or I think I don't found it.

I am using ColdFusion 9 with MySQL server. Could you advice me how to do this? or any tutorial, article, or e-book is also welcome.

解决方案

Actually, you have two great engines for CF9: Verity (classic) and Solr (modern).

Both of them implement the idea of collections. Creating and maintanence of the collection is pretty obvious and can be found in manual (see previous links).

The main hint for you can be found on cfindex tag manual page: you can populate (update) the collection with query data. Set type custom, enter the query name and all columns you need (combinations may vary).

All you need after that is to use cfsearch.

Also I can recommend to set up the script executed by scheduler to refresh your collection periodically.

EDIT

Sample code (note: code not tested, just the simplified cut from my old component). These are two methods of the 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>

这篇关于如何使用Verity在ColdFusion 9中编制索引和搜索数据库内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆