Skip to main content
Known Participant
September 25, 2008
Question

Verity and CFQUERY Order By Problem

  • September 25, 2008
  • 5 replies
  • 672 views
I am hoping there is a simple cure, but as I have learned nothing really ever comes easy. Here's the problem I am having.

In the 4 steps below I query a verity collection, run a query (QoQ) against it in order to sort the results by the search score. I then put all items found (key) into a list, then pass this list to a query that looks in the main database to find all items in the list. The problem I am having is that the last query (step 4) doesn't pull the items in the same order they appear in the list. When I output the results of the last query the results are no longer in order of relevance (score). Is there a way to have the last cfquery pull the items in order they appear in the list created in step 3?

I have tried an alternative method of doing a list loop around the query and output, but found issues when trying to allow the user to drill down further (by manufacturer, category, etc.) and also with creating a "Previous, Next" button navigation. Any help would be greatly appreciated.

Ron
    This topic has been closed for replies.

    5 replies

    Inspiring
    September 25, 2008
    you could also write another query that pulls all necessary data from
    your product_info table and then do a QoQ between the verity results
    query and this products query:

    <CFSEARCH NAME="Getresults" collection="XYZ" type="simple"
    CRITERIA="#Keyword#">

    <cfquery name="qProducts" datasource="your_dsn_here">
    SELECT productid, title, vendor, sell_price
    FROM product_info
    ORDER BY productid
    </cfquery>

    <cfquery name="qoq1" dbtype="query">
    SELECT DISTINCT
    Getresults.KEY, Getresults.score, Getresults.summary, Getresults.title,
    qProducts.productid, qProducts.title AS prod_title, qProducts.vendor,
    qProducts.sell_price
    FROM Getresults, qProducts
    WHERE qProducts.productid = Getresults.KEY
    ORDER BY score DESC
    </cfquery>

    you can cache the qProducts query if it returns a lot of results, so it
    is pulled from memory instead of hitting your db with every search...

    hth

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Known Participant
    September 25, 2008
    Actually, Just found out that if the results bring back more than 80 items then the loop errors out (errno:24) which is a "too many files open" error. I can put a maxrows="80" but then I can't drill down further (category, mfg) if those items aren't in the first 80 results.

    Known Participant
    September 25, 2008
    Hi Azadi.

    You're a genius. Thank you so much. This actually worked once I get the syntax correct.
    I have one question. If the results find 50 items. Would this union loop cause major
    query delays?

    Known Participant
    September 25, 2008
    This method makes sense but it wouldn't run. Is this code compatible with MX 6.1?
    Inspiring
    September 25, 2008
    1) if you are doing a QoQ, specify dbtype="query" instead of
    datarource="Step2"

    2) <cfset Itemsfound = "#valuelist(key)#"> should really be <cfset
    Itemsfound = valuelist(Step2.key)>

    3) in your step 3 loop over your itemsfound list creating a UNION query
    with an added sort column to sort your results according to score.
    something like this:

    <cfquery name="Step3" datasource="step3">
    SELECT productid, title, vendor, sell_price, '1' AS sortcol
    FROM product_info
    WHERE productid = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#listfirst(Itemsfound)#">
    <cfif listlen(Itemsfound) gt 1>
    <cfloop from="2" to="#listlen(Itemsfound)#" index="idx">
    UNION
    SELECT productid, title, vendor, sell_price, '#idx#' AS sortcol
    FROM product_info
    WHERE productid = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#listgetat(Itemsfound, idx)#">
    </cfloop>
    </cfif>
    ORDER BY sortcol, title
    </cfquery>

    i assume you have necessary checks in place to make sure your step 1 and
    step 2 actually returned some results and your Itemsfound list is not
    empty, so i did not include that code above...

    hth


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/