Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
I have read and re-read the two pages in the documentation regarding search, but they haven't been terribly helpful because they don't address how to display the information returned after querying.

Is there any sort of "k_is_search" variable to check?
Is there any way to sort search results in descending order? I tried order="desc" but to no avail.
Can I change the length of text output by "k_search_excerpt"?

I'm new to Couch, so maybe I'm just not aware of how these things are done. Advice or a helping hand would be greatly appreciated.

Thanks!
-Matt
Hi Matt and welcome :)

I am sorry you found the search documentation not helpful enough but don't worry I'll be happy to fill in the perceived gaps.
..they don't address how to display the information returned after querying.

OK, I assume you understood the relation between the 'search_form' and the 'search' tag -
the 'search_form' simply sets the keywords searched for in the querystring and the 'search' tag picks them up from there.

The 'search' tag goes through the database looking for matching pages and, for our example, suppose 3 pages are found. The following snippet then iterates through each of the 3 pages and sets ALL the variables (the same that you'd expect to find had the pages been fetched using cms:pages tag) for each of the page -
Code: Select all
<cms:search>
   <!-- All the variables of the pages found will be available here -->
</cms:search>

Try putting cms:dump tag to see what I mean
Code: Select all
<cms:search>
   <!-- All the variables of the pages found will be available here -->
   <cms:dump />
</cms:search>

In addition to the regular variables, the search tag also sets two extra variables
k_search_title and k_search_excerpt.
These two variables have the search terms highlighted. The k_search_excerpt additionally also brings together excerpts of the various places in the page that the search terms were found (a term, for example, could be found at the top of the page and the second time at the bottom - k_search_excerpt outputs both the lines).
Is there any sort of "k_is_search" variable to check?

Such a variable is not needed. If the querystring contains the search terms, the 'search' tag will display the result else not.
Is there any way to sort search results in descending order? I tried order="desc" but to no avail.

The search tag actually uses an algorithm where it ranks the pages according to their relevance with the terms being searched (e.g. how many times the term appears in a page, where does the term appear etc.) and then sorts the pages with the most important listed first. This, IMHO, makes the most sense for search results. No other form of display is supported.
Can I change the length of text output by "k_search_excerpt"?

I suppose you can try using cms:excerptHTML tag (http://www.couchcms.com/docs/tags-refer ... thtml.html). But please remember you could end up cutting off the highlighted search terms. the decision is yours.

Hope this answers your questions. Please feel free to post in if you still have any confusions on this topic.

Thanks
Thank you :]
Another question regarding search..

I'd like to display a message informing a viewer if their search query returns no results. Is this functionality included already?
Or, is there some way to tell if the search returns no results? I've tried doing this:

cms:search
cms:if k_total_pages == '0'
/search

But it doesn't seem to work, and if the search returns nothing, then cms:dump doesn't do anything either.

Again, any help would be appreciated, I looked around, and tried a few things by synthesizing the information available about searching with information on the if function, but I'm having no luck.

Many thanks,
-Matt
Hi Matt, you could try this method:
Code: Select all
<cms:search>
<!-- Your Code Here -->
<cms:if k_paginated_top ><cms:set results='yes' scope='global' /></cms:if>
</cms:search>

<cms:if results >
<cms:else />
<p>Sorry, no relevant pages were found.</p>
</cms:if>

The idea is basically to set a variable 'results' that can be used outside of the 'cms:search' tag to let us know if there were any search results. This variable can only be set a maximum of one time because of the wrapping 'cms:if' tag.
ingenious ... I got stuck on this too, thanks cheesypoof
6 posts Page 1 of 1