etc_search

4 October 2012, 12:24

Keywords: search, custom, faceted

The excellent cbs_live_search makes txp search funnier, displaying results “in live” with AJAX. Our new etc_search plugin goes much farther. It allows you to construct complex search forms/queries, make faceted searches and display multiple search inputs/outputs on the same page. Some examples follow.

Default search

In its simplest form, <txp:etc_search /> acts like the built-in Textpattern search, digging articles’ title and body. But it adds a bit of logic without recurring to match="all" or match="any" attribute. For example, many articles of this site contain the word etc, but only one three now contain both etc and job. Try to search firstly for etc, then for etc job and finally for "etc job" to see the difference:

If you hit Return, you will be redirected to the standard search page, with paginated search results output by <txp:etc_search_results /> tag, see plugins help.

We can do more complex searches too, for example table -contents, group will come up with all articles containing ('table' AND NOT 'contents') OR 'group'. The syntax is customizable, and you could extend it with some MySQL logical or comparison operators, like XOR or >=.

Extend the search area

If you want to extend the default search fields to, say, keywords, just create a query form {Title,Body,Keywords}.

You can search in any article field(s), restrict search to some sections/categories/whatever, and modify the matching criteria, by creating a custom query. The following query will display all articles from “tips” section published between min and max days ago:

{/^(\d+)\.{2,}(\d+)$/::DATEDIFF(NOW(), Posted) BETWEEN $1 AND $2} AND Section='tips'

Do you prefer separate min/max inputs? Just put them inside etc_search and combine them in one search query with format attribute:

<txp:etc_search id="2" format="{min}..{max}">
	Tips published between <input name="min" /> and <input name="max" /> days ago:
	<input type="submit" />
</txp:etc_search>
Tips published between and days ago:

… or even say it in plain Engish:

Tips published between and :

Beyond the defaults

But that’s not all. The main news is the ability to query any table of your txp database. Suppose that you want to search for some text in images alt attribute and display the titles of the articles which are associated (via Article image) with the matching images. Then you can create a custom query like this:

SELECT txp.ID,txp.Title,img.alt FROM textpattern txp JOIN txp_image img ON FIND_IN_SET(img.id,txp.Image) WHERE {img.alt}

This very article has the image 5 (with alt="etc_search") associated with it, so if you search the form below for sea, it will point at itself:

Combined queries (from several tables at once) are possible too, as well as faceted queries. For example, tape tips 2014 in the left sidebar search field to match the tips posted in 2014.

Customization

You are not bound to the default input/output forms. Create them at your liking, amend with your own animation effects and “loading” goodies, and so on. The following one will filter the search by category and display the results in the sidebar:


And since nothing stops you from plugging search results into another form, the construction of dynamic menus is fairly easy.

Bonus track

The core feature of etc_search is the ability to transform a human-written search string into db query. The search string does not have to come from site visitors input, you can specify it internally, using query attribute of <txp:etc_search_results /> tag. This greatly extends the search area accessible to <txp:article_custom /> tag. For example, to display the articles with Title containing "list" OR "copy", create a query {Title} with id=2 (say), call

<txp:etc_search_results id="2" query="list, copy" />

and see the result:

Copy image protect
Similar articles list
Alphabetic list
Turn a list into table

File(s)