PHP query mode for Sphinx interface

Source: Internet
Author: User
Tags mysql code

Like the following optional matching pattern:

Sph_match_all, matching all query terms (default mode);

Sph_match_any, matches any one of the query words;

Sph_match_phrase, the whole query is regarded as a phrase, which requires a complete match in order;

Sph_match_boolean, consider a query as a Boolean expression

sph_match_extended, the query is treated as an expression Coreseek/sphinx the internal query language. Starting with version Coreseek 3/sphinx 0.9.9, this option is replaced by the option SPH_MATCH_EXTENDED2, which provides more functionality and better performance. This option is retained for compatibility with legacy code-so that legacy application code can continue to work even when Sphinx and its components include API upgrades.

Sph_match_extended2, use the second version of "Extended match mode" to match the query.

Sph_match_fullscan, the query is forced to match by using the full scan mode described below. Note that in this mode, all query terms are ignored, although filters, filter ranges, and groupings still work, but any text match does not occur.

Our main concern is the Sph_match_extended2 extended match pattern, which allows the use of some conditional statements like MySQL

//set the extended matching mode $sphinx->setmatchmode ("Sph_match_extended2");//Use conditional statements in a query, fields begin with @, search content contains tests, Toid equals 1 mail: $result = $sphinx->query (' @content (test) & @toid = 1 ', ' * ');//Set more complex conditions with parentheses and & (with), |, (or),-(not, = =) result = $sphinx->query (' (@content (test) & @subject = uh) | (@fromid-(100)) ', ' * ');//More syntax see the description of the official document matching pattern 

The search field is worth mentioning in the extended match pattern, and if the field is set, the fields that extend the matching search do not contain these properties by default. What should I do if we set the Fromid, Toid, sendtime as properties, but want to use it as a condition in extended match mode only with SetFilter () or setfilterrange () or something like

?

If the field is selected more than once in the Sql_query statement,

Sql_query = Select Emailid,fromid,fromid,toid,toid,subject,content, Sendtime,sendtime,attachement from email

//Setup done remember to re-establish the index

More conditional tricks
Just a few tricks, but not recommended for use in a deployment environment, as for why, see the end of the article



<, <=, >, >=
Default sphinx do not have these comparators.

What if I want the message to be sent more than a certain date? Use the Setfilterrange () method to simulate

Greater than or equal to a certain time intercept $time$sphinx->setfilterrange (' Sendtime ', $time, 10000000000)//Time intercept Max is 10 9, plus 1 is not beyond. Greater than a certain time intercept $time$sphinx->setfilterrange (' Sendtime ', $time +1, 10000000000)//less than equals a certain time intercept $time$sphinx-> Setfilterrange (' Sendtime ',-1, $time)    //time-truncated minimum is 0, so should be reduced 1//is greater than a certain time to intercept $time$sphinx->setfilterrange (' Sendtime ',-1 , $time-1)


is not NULL
How to search for empty fields, such as I want to search for empty attachments, someone might want to @attachment (")? In fact, this is a search for two single quotes ... Sphinx Search for strings without quotes

Currently Sphinx does not provide such a function, in fact, can be in the MySQL statement on the hands and feet:

Sql_query = SELECT Emailid,fromid,toidsubject,content,sendtime,attachement! = "As attach is not null from email//here Returns a A new field attachisnotnull, when the Attachisnotnull is 1, the attachment is not empty.

Setup done remember to re-establish the index



Find_in_set ()
Search for a message containing an attachment, MySQL is accustomed to use find_in_set so simple sentence to be done, in the Sphinx must be set in the configuration property Sql_attr_multi Multi-value attribute (MVA):

Sql_attr_multi = Attachment #attachment可以是逗号分隔的附件ID, or a space, semicolon, etc Sphinx can be recognized

Setup done remember to re-index and then PHP can use SetFilter ()//search contains attachment ID of 1 or 2 messages, MySQL syntax is such find_in_set (' Attachment ', ' $sphinx ') SetFilter (' attachment ', array)//You can use Setfilterrange to search for messages containing the attachment ID in the 50-100 range $sphinx->setfilterrange (' Attachment ', 50, 100)


Summarize
If you want a free, easy-to-use, fast full-text search engine, Sphinx is undoubtedly the best choice, but do not forget the purpose of Sphinx: full-Text search. Don't think about those mess conditions. You want to make the Sphinx search as flexible as MySQL and can be used completely alone in some complex multi-conditional searches, like advanced search for some emails, then I suggest you spend more time on the optimization of PHP or MySQL code, because that might make your search slower.

The best way is to search for the content in the simplest way, and return the ID to the MySQL database search.

PHP query mode for Sphinx interface

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.