Saturday, September 13, 2008

verity search in coldfusion

Verity Search

If your site has a lot of content, text files, or even Excel spreadsheets, you may want to consider using the Verity Search Collection in ColdFusion. Verity Collections allow you to index your site and create a ?map? if you will, of your files. Even the contents of your doc files, PDF?s, and spreadsheets. What will really amaze you is how little it takes to set up and create.

First of all you need access to the CF administrator, or you need to have the server administrator configure the server to index your site. If you run your own server then this will be a snap.

==========================
PART ONE - Configuring the server
==========================

Open the ColdFusion administration page and log in. On the Left hand side under ?Datasources? is a link labeled ?Verity Collections? Click the link.

VERITY COLLECTIONS
This is the page you will no be on. In the first section create a NAME for your collection. This can be any name that you wish to use. I will call it ?boxcar? for now.

The PATH field is for the location you would like to store the collection file. It is not to be confused with the location of the files you want index. Leave it at the default setting unless you have reason to change it.

Next Click the button ?Create Collection?

INDEX VERITY COLLECTIONS
The field for file extensions has a few default file types already in place, however you may want to add other file types. Here is a list of files that are common that you might use. Just add the appropriate extension to the list.
ASCII Text
HTML
Lotus
Rich Text Format
Microsoft word
PDF
PowerPoint
Excel

DIRECTORY PATH is the physical location of the files on your server. IE C:\www\my_site

The RECURSIVE INDEX SUB DIRECTORY check box may be checked if you want the index to include files beneath the selected directory.

Last but not least, the RETURN URL. This is the actual path for your HTTP files.
IE. http://my_server/my_site/
It?s important to get the URL correct as this is used to create the links in your search results.

Now click Submit, your server will now index your site.

=============================
PART TWO ? Creating the search forms
=============================
Search_form.cfm
-------------------------------------
<html>
<head>
<title>Search this site</title>
</head>

<body>
<strong>What would you like to search for? </strong>
<form name="form1" method="post" action="verity_search_results.cfm">
<p>
<input name="criteria" type="text" id="criteria">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>



-------------------------------
search_results.cfm
-------------------------------
<!--- Define a search name and specify the COLLECTION name used in the CF Admin.--->
<CFSEARCH NAME="search_boxcar"
COLLECTION="boxcar"
CRITERIA="#FORM.criteria#">

<html>
<head>
<title>Search Results</title>
</head>

<body>
<h3>Your search results </h3>

<!--- Output search results complete with score --->
<CFOUTPUT QUERY="search_boxcar">
<a href="#URL#">#Title#</a><br>
Score: #Score#<p>
</CFOUTPUT>

</body>
</html>

The key here is the CFSEARCH tag. It is the easiest search ever and no SQL involved. However, if you want to add an SQL statement you can do more with it. Like search you database or your query.
For part two of this tutorial we will be keeping the search form that was created earlier in part one. We will also need to add an update page to update our collection with the db info. Lastly we will be modifying the results page just a bit.

Just to recap here is the search form once more:
<html>
<head>
<title>Search this site</title>
</head>

<body>
<strong>What would you like to search for? </strong>
<form name="form1" method="post" action="verity_search_results.cfm">
<p>
<input name="criteria" type="text" id="criteria">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Now, we want to the ability to update our collection as our site grows so, what we need to do is create an update collection page. Ill call this verity_update.cfm

Here Is what I did:

<html>
<head>
<title>Update Verity Collection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<cfquery name="get_db_info" datasource="#dsn#">
select *
from YourTable
</cfquery>

<cfindex query="get_db_info" <!--- Specify the query from above. --->
collection="YourCollection" <!--- Specify What Collection to Update. --->
action="update" <!--- Actions include: update, delete, refresh, or purge. --->
type="custom" <!--- If action is update or delete "custom" type is required. --->
key="SomeFieldname" <!--- Key is usually set to your Key Fieldname. It will be referenced later. --->
title="AnotherFieldname" <!--- The Title will appear in the list of verity results. --->
Custom1="CustomFieldname" <!--- You may add a custom1 and custom2 field if additional information is needed. --->
body="Fields, To, Be, Indexed"> <!--- Body to be indexed, sepertaed by comas. --->

Your Collection has been updated.
</body>
</html>

Lastly we need to make the modifications to the results page.

<html>
<head>
<title>Verity Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<!--- Use CFSEARCH Tag with collection information here --->
<cfsearch name="YourSearchName"
collection="YourCollection"
criteria="#form.criteria#">

<!--- Begin displaying results of your serch --->
<strong>Your Results</strong>



<!--- Here I'll demonstrate How to use verity to display different types of data --->
<!--- By checking the results to see if the page is indexed or if the data is from the db I can switch modes here. --->
<cfoutput query="YourSearchName">
<cfif URL IS "">
<p><a href="item_details.cfm?Item_ID=#key#">#title#</a><br>
Score: #score#</p>
<cfelse>
<p><a href="#URL#">#title#</a><br>
Score: #score#</p>
</cfif>
</cfoutput>
</body>
</html>

No comments: