Back to the list of posts

Sphinx Search Configuration Example

Sphinx Search is a powerful and fast full text search engine that can be used to search through large amounts of data. In this article, we will look at an example configuration for Sphinx Search using the example of searching for products in an online store.

Installation and configuration

Before you can start working with Sphinx Search, you need to install it on your server. To do this, you can use the package manager of your operating system or download it from the official website.

After installation, you need to create a configuration file sphinx.conf that will contain indexing and search settings. In our example, we will search for products by title and description, so we need to create an index for each of these fields.

Example configuration file:


source products
{
     type = mysql

     sql_host=localhost
     sql_user = root
     sql_pass=password
     sql_db = mydatabase
     sql_port = 3306

     sql_query = \\
         SELECT id, name, description \\
         FROM products

     sql_attr_uint = category_id
}

index products_index
{
     source = products
     path = /var/lib/sphinxsearch/data/products_index
     docinfo = external

     morphology = stem_enru
     min_word_len = 3
     charset_type = utf-8

     min_infix_len = 2
     enable_star = 1
     expand_keywords = 1

     html_strip = 1
     html_remove_elements = style, script
     html_remove_entities = 1
     html_index_attrs = class, title, alt

     stopwords=/etc/sphinxsearch/stopwords.txt
}

searchd
{
     listen=9312
     listen=9306:mysql41
     log = /var/log/sphinxsearch/searchd.log
     query_log = /var/log/sphinxsearch/query.log
     read_timeout = 5
     max_children = 30
     pid_file = /var/run/searchd.pid
     seamless_rotate = 1
     preopen_indexes = 1
     unlink_old = 1
     workers = threads
     binlog_path = /var/lib/sphinxsearch/data
}

In this example, we are creating a products data source that will use a MySQL database to retrieve data. We specify the host, user, password, database and port to connect to MySQL. In sql_query we specify a query that will retrieve the required fields from the products table. We also specify the category_id attribute, which will be used to filter the results by product category.

Next, we create an index products_index that will use the products data source. We specify the path where the index data will be stored, as well as indexing and search settings. In this example, we use stem_enru for morphological parsing of words, utf-8 for encoding, and also specify the minimum word length, minimum inner fragment length, and settings for searching using an asterisk.

Finally, we set up searchd, which will run as a daemon and process search requests. We specify the port on which the server will listen, the path for logging, settings for running in multiple threads and other parameters.

Launching and using

After setting up Sphinx Search, you can start the server and index the data. To do this, you can use the commands:


sudo searchd
sudo indexer --all --rotate

You can then search using the Sphinx Search client, for example using the SphinxAPI library for PHP:


require_once('sphinxapi.php');

$cl = new SphinxClient();
$cl->SetServer('localhost', 9312);

$result = $cl->Query('product name', 'products_index');

In this example, we are looking for products that have the word 'product name' in their title or description. The result of the query will be an array with the found products.

Conclusion

Sphinx Search is a powerful tool for searching through large amounts of data. In this article, we looked at a configuration example for searching for products in an online store, but it can be used for other tasks as well. I hope this example helps you set up Sphinx Search for your needs.

#DevOps #Search #Sphinx

Back to the list of posts Next post

menuclose

start a project

Заявка отправлена

Спасибо! Заявка отправлена. Свяжемся с вами в течении часа!