Time for action - searching with extended query syntax

  1. Create a PHP script search_extended_mode.php in your webroot with 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);
    // Set the mode to SPH_MATCH_EXTENDED2
    $client->SetMatchMode(SPH_MATCH_EXTENDED2);
    // Returns documents whose title matches "php" and
    // content matches "significant"
    display_results(
    $client->Query('@title php @content significant'),
    'field search operator'),
    // Returns documents where "development" comes
    // before 8th position in content field
    display_results(
    $client->Query('@content[8] development'),
    'field position limit modifier'),
    // Returns only those documents where both title and content
    // matches "php" and "namespaces"
    display_results(
    $client->Query('@(title,content) php namespaces'),
    'multi-field search operator'),
    // Returns documents where any of the field
    // matches "games"
    display_results(
    $client->Query('@* games'),
    'all-field search operator'),
    // Returns documents where "development framework"
    // phrase matches exactly
    display_results(
    $client->Query('"development framework"'),
    'phrase search operator'),
    // Returns documents where there are three words
    // between "people" and "passion"
    display_results(
    $client->Query('"people passion"~3'),
    'proximity search operator'),
    // Returns documents where any of the
    // two words from the phrase matches
    display_results(
    $client->Query('"people development passion framework"/2'),
    'quorum search operator'),
    
  2. Execute the script in a browser (the output is explained in the next section).

What just happened?

For using extended query syntax, we set the match mode to SPH_MATCH_EXTENDED2:

$client->SetMatchMode(SPH_MATCH_EXTENDED2);

The first operator we used was field search operator. Using this operator we can tell Sphinx which fields to search against (instead of searching against all fields). In our example we searched for all documents whose title matches "php" and whose content matches "significant". As an output, we got posts (documents) with the id 5, which was the only document that satisfied this matching condition as shown below:

@title php @content significant

The search for that term returns the following result:

What just happened?

Following this we used field position limit modifier. The modifier instructs Sphinx to select only those documents where "development" comes before the 8th position in the content field, that is, it limits the search to the first eight positions within given field.

@content[8] development

And we get the following result:

What just happened?

Next, we used the multiple field search operator. With this operator you can specify which fields (combined) should match the queried terms. In our example, documents are only matched when both title and content matches "php" and "namespaces".

@(title,content) php namespaces

This gives the following result:

What just happened?

The all-field search operator was used next. In this case the query is matched against all fields.

@* games

This search term gives the following result:

What just happened?

The phrase search operator works exactly same as when we set the matching mode to SPH_MATCH_PHRASE. This operator implicitly does the same. So, a search for the phrase "development framework" returns posts with id 7, since the exact phrase appears in its content.

"development framework"

The search term returns the following result:

What just happened?

Next we used the proximity search operator. The proximity distance is specified in words, adjusted for word count, and applies to all words within quotes. So,"people passion"~3 means there must be a span of less than five words that contain both the words "people" and "passion". We get the following result:

What just happened?

The last operator we used is called as a quorum operator. In this, Sphinx returns only those documents that match the given threshold of words."people development passion framework"/2 matches those documents where at least two words match out of the four words in the query. Our query returns the following result:

What just happened?

Using what we have learnt above, you can create complex search queries by combining any of the previously listed search operators. For example:

@title programming "photo gallery" (asp|jsp) @* opensource

The query means that:

  • The document's title field should match 'programming'
  • The same document must also contain the words 'photo' and 'gallery' adjacently in any of the fields
  • The same document must not contain the words 'asp' or 'jsp'
  • The same document must contain the word 'opensource' in any of its fields

There are few more operators in extended query syntax and you can see their examples at http://sphinxsearch.com/docs/manual-0.9.9.html#extended-syntax.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset