views:

426

answers:

1

Hi, everyone.

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.

+3  A: 

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>
Sergii
Thank you!, Sergii! you pointed me the way!but Could you guide me more about the script? Can I done it in Coldfusion server? with CFM or CFC? I found vSpider but I wonder it should be more easier way to done this.
Teerasej
You can manage collections using cfcollection tag. You can wrap the cfindex and cfsearch into the CFC methods (recommended way). Examples of each tag usage are in manual, you have the links.
Sergii
Please see edit.
Sergii
If you will need to index over 250,000 documents/records stay away from Verity. The CF OEM license for it is limited to 250k docs. SOLR is really the way to go.
kevink
Thank for all of you, gentlemen.
Teerasej