search_boolean_mode.php
in your webroot with the following code:<?php // Include the api class require_once('sphinxapi.php'), // Include the file which contains the function to display results require_once('display_results.php'), $client = new SphinxClient(); // Set search options $client->SetServer('localhost', 9312); $client->SetConnectTimeout(1); $client->SetArrayResult(true); display_results( $client->Query('php programming'), '"php programming" (default mode)'), // Set the mode to SPH_MATCH_BOOLEAN $client->SetMatchMode(SPH_MATCH_BOOLEAN); // Search using AND operator display_results( $client->Query('php & programming'), '"php & programming"'), // Search using OR operator display_results( $client->Query('php | programming'), '"php | programming"'), // Search using NOT operator display_results( $client->Query('php -programming'), '"php -programming"'), // Search by grouping terms display_results( $client->Query('(php & programming) | (leadership & success)'), '"(php & programming) | (leadership & success)"'), // Demonstrate how OR precedence is higher than AND display_results( $client->Query('development framework | language'), '"development framework | language"'), // This won't work display_results($client->Query('-php'), '"-php"'),
Execute the script in a browser (the output shown in next section).
We created a PHP script to see how different Boolean operators work. Let's understand the working of each of them.
The first search query, "php programming", did not use any operator. There is always an implicit AND
operator, so "php programming" query actually means: "php & programming". In second search query we explicitly used the& (AND)
operator. Thus the output of both the queries were exactly same, as shown in the following screenshot:
Our third search query used the OR
operator. If either of the terms get matched whilst using OR
, the document is returned. Thus"php | programming"
will return all documents that match either "php" or "programming", as seen in the following screenshot:
The fourth search query used the NOT
operator. In this case, the word that comes just after the NOT
operator should not be present in the matched results. So"php -programming"
will return all documents that match "php" but do not match "programming" We get results as seen in the following screenshot:
Next, we used the grouping operator. This operator is used to group other operators. We searched for"(php & programming) | (leadership & success)"
, and this returned all documents which matched either; "php" and "programming" or "leadership" and "success", as seen in the next screenshot:
After that, we fired a query to see how OR
has a precedence higher than AND
. The query"development framework | language"
is treated by Sphinx as"(development) & (framework | language)"
. Hence we got documents matching"development & framework"
and"development & language"
, as shown here:
Lastly, we saw how a query like"-php"
does not return anything. Ideally it should have returned all documents which do not match"php"
, but for technical and performance reasons such a query is not evaluated. When this happens we get the following output: