Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I sort the data returned from cfsearch?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
When creating a search engine, using cfindex and cfsearch, is there a way I can sort the result returned from cfsearch?
Thank you.
 
Well, since no one else has answered this, I guess I'll take a shot at it. If I remember correctly, the results are an array. So, you should be able to sort the results. I use a custom tag from Allaire's Developer's Exchange called CF_ArrayTable that sorts a 2 dimensional array. Let me know how it goes. Calista :-X
Jedi Knight,
Champion of the Force
 
I think the resultset is normaly sorted by relevance (or score).
The most relevant result is the first en least relevant is the last.
 
Here's one way to do it. Basically, you take the list of scores and and pass into an array. Then you take that array and add it to the query before you sort it (i.e. no ORDER BY statement in the SQL). Then you use Nate Weiss' querysort tag and sort the query on the score attribute, thereby maintaining the ability to sort the search results by relevance.

CF_QuerySort

<CFIF ParameterExists(form.criteria)>
<CFSEARCH COLLECTION=&quot;test&quot;
NAME=&quot;search&quot;
CRITERIA=&quot;#LCase(Criteria)#&quot;>

<CFSET application.resultsList = ValueList(search.key)>

[COLOR=666666]<!--- add score to application variables --->[/color]
<CFSET ScoreList = ValueList(search.score)>
<CFSET application.scoreArray = ListToArray(ScoreList)>
</CFIF>

[COLOR=666666]<!--- if no results are returned, do not do the additional query --->[/color]
[COLOR=666666]<!--- no files found for specified criteria? --->[/color]
<CFIF search.RecordCount is 0>
[COLOR=000080]<CENTER>[/color][COLOR=000080]<I>[/color]No files found for
<CFOUTPUT>&quot;#Form.criteria#&quot;</CFOUTPUT>
[COLOR=000080]</I>[/color][COLOR=000080]</CENTER>[/color]
<CFELSE>

[COLOR=666666]<!--- run a query to pull the information about the records that were returned as &quot;matches&quot; on the verity search --->[/color]
<CFQUERY NAME=&quot;unsortedResults&quot; DATASOURCE=&quot;kbase&quot;>
SELECT href, title, description, id, os
FROM docs
WHERE docs.id IN (#application.resultsList#)
<CFIF ParameterExists(form.limit)
AND ParameterExists(criteria)>

AND docs.os = '#LCase(form.limit)#'
</CFIF>
</CFQUERY>

[COLOR=666666]<!--- add score variable to query --->[/color]
<CFSET temp = QueryAddColumn(unsortedResults, &quot;Score&quot;, application.scoreArray)>

[COLOR=666666]<!--- sort the query based on score --->[/color]
<CFIF Find(&quot;0&quot;,scoreList)>
<CF_QUERYSORT QUERY=&quot;unsortedResults&quot;
SORTCOLUMN=&quot;Score&quot; SORTORDER=&quot;Desc&quot;
SORTTYPE=&quot;Numeric&quot; SORTEDQUERY=&quot;results&quot;>

</CFIF>
</CFIF> - tleish
 
I'm doing a similar type of search as in tleish's post where the cfsearch gets the records based on the search term and then I do a secondary query to narrow the records down even further. I copied tleish's code and most if it works fine but I've got a problem.

For some reason the second query's recordcount is still showing 16 records (the # of results from the cfsearch) even though only two records actually match in the second query. And on other searches the 'next N records' buttons still show up because it thinks that there are still records to show.

Jessica
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top