Time for action - querying Sphinx using MySQL CLI

  1. Create the file /usr/local/sphinx/etc/sphinx-ql.conf and add the following code:
    source items
    {
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass =
    sql_db = sphinx_conf
    sql_query = SELECT id, title, content, 
    UNIX_TIMESTAMP(created) AS created FROM items
    sql_attr_timestamp = created
    }
    index items
    {
    source = items
    path = /usr/local/sphinx/var/data/items-ql
    charset_type = utf-8
    }
    searchd
    {
    listen = localhost:9306:mysql41
    log = /usr/local/sphinx/var/log/ql-searchd.log
    query_log = /usr/local/sphinx/var/log/ql-query.log
    pid_file = /usr/local/sphinx/var/log/ql-searchd.pid
    }
    
  2. Create the index by running the indexer utility:
    $/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx-ql.conf --all
    
  3. Start the searchd daemon:
    $/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx-ql.conf
    
  4. Connect to the MySQL CLI program:
    $mysql -u dbuser pdbpass -h localhost -P 9306
    
  5. The previous command will connect to the MySQL server at localhost on port 9306.
    Time for action - querying Sphinx using MySQL CLI
  6. Issue the following query from MySQL CLI:
    mysql>SELECT * FROM items WHERE MATCH ('search term'),
    
  7. The previous query will return the following results:
    Time for action - querying Sphinx using MySQL CLI

What just happened?

Firstly, we created a new configuration file to index the items database table. We put the following value for the listen option in searchd section of the configuration.

listen = localhost:9306:mysql41

This line in the configuration enables the MySQL protocol support, and configures Sphinx so that when MySQL client is started at port 9306, it will use the Sphinx as the server.

Note

mysql41 is the name of the protocol handler to be used when searchd listens on 9306 port. mysql41 is used for MySQL v4.1 up to at least v5.1.

This new access method is supported in addition to the native Sphinx API. You can specify more than one listen options in the searchd section, so that one uses the native API and other the serves the MySQL:

listen = localhost:9312
listen = localhost:9306:mysql41

We then started the searchd daemon and connected to MySQL CLI. While starting MySQL CLI we used port 9306 , the same port where Sphinx is listening. You would notice that when MySQL gets connected it shows the Sphinx version against the "Server Version", and not the actual MySQL server version. This means that MySQL CLI will now fire queries against Sphinx instead of MySQL server.

After that we fired the following query:

mysql>SELECT * FROM items WHERE MATCH ('search term'),

The query syntax is similar to the MySQL full-text search query syntax. SphinxQL supports the following SQL statements:

  • SELECT
  • SHOW WARNINGS
  • SHOW STATUS
  • SHOW META

Let's see the usage of each of these.

SELECT

The SphinxQL syntax adds several Sphinx specific extensions to the regular SQL syntax. Usage of @ symbol for fields in the index and OPTION clause are few Sphinx specific extensions. In addition, there are a few omissions, such as SphinxQL, which does not support JOINs.

Column list clause

Column (field) names, SQL arbitrary expressions, and star (*) are allowed. Some special names, such as @id and @weight, should be used with a leading @ (at-sign). This requirement will be lifted in future versions:

SELECT @id AS item_id, category_id, (points + 2)
AS totalpoints FROM items WHERE MATCH ('search term'),

Note

Computed expresses must be aliased with a valid unique identifier. For example, totalpoints in the previous query. This is unlike SQL where expressions need not have an alias.

FROM clause

The FROM clause should contain the list of indexes to be searched. Multiple index names are enumerated by a comma. This is unlike SQL, where comma in FROM means JOIN:

SELECT * FROM items1, items2 WHERE MATCH ('search term'),

WHERE clause

WHERE works for both full-text queries and filters. For filtering, normal comparison operators such as =, !=, <, >, <=, >=, IN(), AND, NOT, and BETWEEN are all supported , and these operators map to filters. Full-text search can be performed using the MATCH() method, which takes the full-text search query as an argument. The full-text query is interpreted according to the full-text query language rules as explained in Chapter 4,Searching.

Note

The OR operator is not supported at the time of writing this book. It will be supported in future versions.

SELECT * FROM items WHERE created > 1281912160;

This query will get all the documents from the items index where the created (timestamp) attribute has a value greater than 1281912160, which is the timestamp for 16th August 2010:

SELECT * FROM items
WHERE MATCH ('search term') AND created > 1281912160;

This query will get all the documents where full-text fields match 'search term' and the created attribute is greater than 1281912160:

SELECT * FROM items
WHERE MATCH ('@content (hello | world) @title bye'),

This query will search for all documents whose content field matches "hello" or "world", but whose title field does not match "bye".

Note

There can only be one MATCH() in the clause.

GROUP BY clause

Currently only single column grouping is supported. However, the column can be a computed expression.

AVG(), MIN(), MAX(), SUM(); functions that are used for aggregating data can be used with either plain attributes or arbitrary expressions as arguments. GROUP BY will add an implicit COUNT(*) in the form of @count column to the result:

SELECT *, AVG(points) AS avgpoints FROM items
WHERE created > 1281912160 GROUP BY category_id;

ORDER BY clause

ORDER BY works similar to the SQL ORDER BY clause, with the difference being that ASC and DESC are explicitly required, and only column names are allowed and not expressions:

SELECT * FROM items WHERE MATCH ('search term')
ORDER BY created DESC;

LIMIT clause

LIMIT works exactly similar to the SQL LIMIT clause. However, as in the Sphinx API, an implicit LIMIT 0,20 is always present by default.

Note

LIMIT can not be set to a value greater than max_matches config file setting. The maximum number of results Sphinx will fetch will not go over what is set in config file.

SELECT * FROM items WHERE MATCH ('@content search_term')
LIMIT 50;
SELECT * FROM items WHERE MATCH ('@title search_term')
LIMIT 50, 100;

OPTION clause

This Sphinx-specific extension lets you control a number of per-query options. The options and values are given in the following table:

Option

Values

ranker

None, bm25, proximity_bm25, wordcount, proximity, matchany or fieldmask

max_matches

integer (per query max matches)

cutoff

integer (max found matches threshold)

max_query_time

integer (max search time threshold in milliseconds)

Option

Values

retry_count

integer (distributed retries count)

retry_delay

integer (distributed retry delay in milliseconds)

SELECT * FROM items WHERE MATCH ('search term')
OPTION ranker=bm25, max_matches=5;

SHOW WARNINGS

This statement is used to get the warning messages produced by the previous query:

mysql>SELECT * FROM items WHERE MATCH ('"search term"/3'),

This query will give the following output:

SHOW WARNINGS

We searched for "search term" with a quorum threshold value of 3. This gave a result, but with one warning. To retrieve the warning message we can fire the SHOW WARNINGS statement:

SHOW WARNINGS;

And it gives the following output:

SHOW WARNINGS

This way you can retrieve the warning messages of the latest query.

SHOW STATUS

The following statement shows the performance counters:

SHOW STATUS

SHOW STATUS will show the IO and CPU counters if searchd was started with --iostats and --cpustats switches respectively. The variables returned by SHOW STATUS provide information about the server performance and operation. For example, uptime shows the number of seconds that the server has been running. connections shows the number of attempts that have been made to connect to the server (successful or unsuccessful).

SHOW META

This shows the additional information about the previous query, which includes query time and other statistics:

SELECT * FROM items WHERE MATCH ('test'),
SHOW META;

This query will output the information as shown in the next screenshot:

SHOW META

So, SHOW META gives the same information that we get in the data returned when we use Sphinx Client API and fire a full-text query.

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

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