How can I use Verity to index and search database content in ColdFusion 9?

后端 未结 1 1526
感情败类
感情败类 2021-02-09 04:30

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.

相关标签:
1条回答
  • 2021-02-09 05:05

    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>
    
    0 讨论(0)
提交回复
热议问题