CFSEARCH general questions, date ranges, and using multiple criteria

traport
traport used Ask the Experts™
on
1. How do you place conditional search criteria in the criteria line of a cfsearch? I'm trying to say, for example, if the field activityDate1 is not blank, then search this column. Simply writing:

<cfsearch name="searchResults"
        collection="ssd_searchResults"
        criteria="<cfif activityDate1 neq ''>cf_custom3>=#activityDate1#></cfif>"
        >

or trying it like this:

<cfsearch name="searchResults"
        collection="ssd_searchResults"
       <cfif activityDate1 neq ''>
        criteria="cf_custom3>=#activityDate1#"
        </cfif>
        >

causes an error. Can you include this kind of conditional code within a cfsearch?If so, how?

2. How do you search more than one column in a collection within the criteria. In other words, how do you search if

cf_custom1 = #form.field1# AND cf_custom2 = #form.field2#

I can't seem to make this work either.

3. My 3rd question I could likely figure out on my own if the other 2 above are possible and that is conditional AND combining... ie.... if formfield1 neq '' search it AND if formField2 neq '' search it, too...

Thanks in advance for your time.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It's been a while since i've messed with my collections but

1) You can't mix cfif within a cf tag so you have to do it this way

<cfif activityDate1 neq ''>
<cfsearch name="searchResults"
        collection="ssd_searchResults"
        criteria="#criteria1#">
<cfelse>
<cfsearch name="searchResults"
        collection="ssd_searchResults"
        criteria="#criteria2#">
</cfif>
       
2) Criteria will always be your search term. You use your custom indexes that you set up in cfindex. Whatever custom indexes you set will be returned as part of your search query object ie: #searchResults.custom1#
if you want to set up multiple search criteria from a form, simply concatenate the form fields into a comma delim list
ie:

<cfset searchTerm = "#form.field1#,#form.field2#">

<cfsearch name="searchResults"
        collection="ssd_searchResults"
        criteria="#searchTerm#>
</cfif>

3) again you'd do this logic outside the cfsearch tag



oops
2) Criteria will always be your search term. You DON'T use your custom indexes that you set up in cfindex.

Author

Commented:
SidFishes (cute name btw)...

Thanks for the answer to 1). It baffles me that CF didn't provide for some conditional logic within these searches. I'm not sure this makes cfsearch useful to me and I'm wondering it I shouldn't just do an old-fashioned search!

I'm not sure I know what you mean in 2).

I can use criteria="cf_custom1=#activityDate1#"... that does work and searches the column I create here when I index:

<cfindex action="update"
        collection="ssd_searchResults"
        key="citationID"
        type="custom"
        query="qry_getCitations"
        custom1="activityDate"
        body="citationFull"
        >
well as I said - it's been a while since I looked at my search code - and I obviously need more coffee as I misread #2

afaik, #1 is correct
#2 don't know
#3 - didn't know you could do it that way so I learned something

However, to your point "I'm wondering it I shouldn't just do an old-fashioned search! "

That's actually a very good question. cfsearch & verity (and whatever it's called in CF9)  do some things -really- well. Pattern matching and suggestions/close matches/scoring in Text searches of docs or pdfs being the best example.

If you are search for things that are easily definable like dates then you're far better going with a simple query.
Commented:
1.) You can specify attributes for your <cfsearch> tag using an attributeCollection. This is a simple structure where keys correspond to the attributes of the tag, like so:

<cfset attributeCollection = {
    name = 'searchResults',
    collection = 'ssd_searchResults',
    criteria = ''
} />
<cfif activityDate1 neq ''>
    <cfset attributeCollection.criteria = "cf_custom3 >= #activityDate1#" />
</cfif>
<cfsearch attributeCollection="#attributes#" />

Another option is using IIF:
<cfsearch name="searchResults"
        collection="ssd_searchResults"
        criteria="#IIF(activityDate1 neq "", DE("cf_custom3>=#activityDate1#"), DE(""))#"
        >
(You might have to debug that last example, but it shouldn't be that hard.)

2.) If you're using Solr (which is increadibly nice in my opinion) you could find a lead or solution on the following page:
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS82937B1B-240F-4850-B376-5FD9F911E5E5.html

3.) As you mentioned in your original question, this might be answered by the answer at 2.).

I do have to mention that I'm somewhat new at the field of cfsearch, as I've only been using it for two months now in CF9. On a somewhat large scale, but still.

Best of luck!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial