A simple example of phpsphinx

Source: Internet
Author: User
A simple example of phpsphinx
This is a simple example of php sphinx. For more information, see.

The Code is as follows:

     SetFilter ("cat_id", array (2); // search $ cl-> SetFilter ("forum_id ", array (123,124, 7); // range filtering // The filtering time is today, and the parameter is the int timestamp $ cl-> SetFilterRange ("starttime ); // filter price $ cl-> SetFilterRange ("price", 10.0, 99.9); // grouping by item_id, sort by order desc $ cl-> SetGroupBy ("item_id", SPH_GROUP_ATTR, "order desc "); // sorting mode // sort by price desc $ cl-> SetSortMode (SPH_SORT_ATTR_DESC, "price"); Note: It will be overwritten by the sorting in SetGroupBy // match any $ cl in the query term -> SetMatchMode (SPH_MATCH_ANY); SPH_MATCH_ALL: matches all query words (default mode); SPH_MATCH_ANY: matches any of the query words. SPH_MATCH_PHRASE considers the entire query as a phrase and requires a complete match in order; SPH_MATCH_BOOLEAN: queries are considered as a Boolean expression (see section 5.2 "Boolean query syntax"); SPH_MATCH_EXTENDED, consider a query as an expression in the internal Query Language of CoreSeek/sphek (see section 5.3 "extended query syntax "). starting from Coreseek 3/sph00000.9.9, this option is replaced by SPH_MATCH_EXTENDED2, which provides more functions and better performance. Retain this option to be compatible with the legacy code-so that the old application code can continue to work even when sphenders and its components, including the API, are upgraded. SPH_MATCH_EXTENDED2: uses the second version of "extended matching mode" to match the query. SPH_MATCH_FULLSCAN, and forces the "Full scan" mode described below to match the query. Note: In this mode, all query words are ignored. Although the filter, filter range, and grouping still work, no text match will happen. // start from 0 and query 30 items. The maximum returned result is 1000 $ cl-> setLimits (1000 ); // Query "movie tickets" $ cl-> Query ("movie tickets", "index") from the sphsf-index named index "); // query "movie tickets" $ sp-> SetGroupBy ('item _ id', SPH_GROUP_ATTR,'s _ order desc ') from the sphsf-index named index '); $ sp-> SetFilter ('city _ id', '1'); $ sp-> SetFilter ('cat _ id', array (1 )); $ sp-> SetLimit (1000,); $ sp-> AddQuery ('movie pass ', 'index'); $ sp-> ResetFilters (); // reset the filter condition $ sp-> ResetGroupBy (); // reset the group $ sp-> SetGroupBy ('item _ id', SPH_GROUPBY_ATTR,'s _ order desc '); $ sp-> setFilter ('city _ id', '2'); $ sp-> setFilter ('cat _ id', array (2 )); $ sp-> setLimits (0, 20,100 0); $ sp-> AddQuery ('hot springs, 'index'); $ sp-> ResetFilters (); // reset the filter condition $ sp-> ResetGroupBy (); // reset the group $ results = $ sp-> RunQuries (); // http://bbs.it-home.org ?>

For more information about php sphx., see http://bbs.it-home.org/shouce/php5/book.sphinx.html.

Batch query (or multi-query) enables searchd to perform possible internal optimization, and reduces network connection and Process Creation overhead under any circumstances. Compared with separate queries, batch queries do not introduce any additional overhead. Therefore, when your web page runs several different queries, you must consider using batch queries.

For example, if the same full-text query is run multiple times but different sorting or grouping settings are used, the searchd will only run one expensive full-text search and relevance calculation, then, multiple grouping results are generated.

Sometimes you need to display not only the search results, but also the counting information related to the category, such as the number of products grouped by the manufacturer. batch query saves a lot of cost. If there is no batch query, you must run these essentially identical queries multiple times and retrieve the same matching items to generate different result sets. If you use batch query, you only need to make up a batch query. Sphinx will optimize the redundant full-text search internally.

AddQuery () stores all the current settings and queries internally. You can also change the settings in subsequent AddQuery () calls. Previously added queries will not be affected. In fact, there is no way to change them.

With the code above, the first query queries "hello world" on the "documents" index and sorts the results by relevance, the second query queries ipod on the "products" index and sorts the results by price. The third query searches for "harry potter" on the "books" index ", the results are still sorted by price. Note that the second SetSortMode () call does not affect the first query (because it has been added), but the other two queries will be affected.

In addition, any filters set before AddQuery () will be used by subsequent queries. Therefore, if SetFilter () is used before the first query, the second query executed through AddQuery () (and subsequent batch queries) will apply the same filter, unless you call ResetFilters () to clear the filter rules first. You can also add new filter rules at any time.

AddQuery () does not modify the current status. That is to say, all the existing sorting, filtering, and grouping settings will not change because of this call. Therefore, subsequent queries can easily reuse existing settings.

AddQuery () returns a subscript in the array returned by RunQueries () results. It is an incremental integer starting from 0, that is, 0 is returned for the first call, 1 is returned for the second call, and so on. This convenient feature allows you Not to manually record these icons when you need them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.