search
HomeBackend DevelopmentPHP TutorialInstall and use Sphinx under windows [Supports Chinese full-text search]_PHP tutorial

A while ago I tried to use Sphinx, a full-text search system that can be easily called by various languages ​​​​(PHP/Python/Ruby/etc). Most of the information on the Internet is installed and used in the Linux environment. Of course, as a production environment, it is necessary to deploy it in a *nix environment. For learning and testing, the Windows environment is more convenient.

This article aims to provide a convenient way to install and configure Sphinx under Windows to support Chinese full-text search. The configuration part is common under Linux.

1. About Sphinx

Sphinx is a full-text search engine released under GPLv2. Commercial licensing (for example, embedding into other programs) requires contacting the author (Sphinxsearch.com) to obtain commercial licensing.

Generally speaking, Sphinx is an independent search engine, intended to provide high-speed, low-space-occupancy, and high-result-relevant full-text search capabilities for other applications. Sphinx can be easily integrated with SQL databases and scripting languages.

The current system has built-in support for MySQL and PostgreSQL database data sources, and also supports reading XML data in specific formats from standard input. By modifying the source code, users can add new data sources (for example, native support for other types of DBMS).

The search API supports PHP, Python, Perl, Rudy and Java, and can also be used as a MySQL storage engine. The search API is very simple and can be ported to new languages ​​within a few hours.

Sphinx Features:

  • High-speed indexing (on modern CPUs, peak performance can reach 10MB/sec);
  • High-performance search (average response time per retrieval is less than 0.1 seconds on 2–4GB of text data);
  • Can handle massive amounts of data (currently known to be able to process more than 100GB of text data, and 100M documents on a single CPU system);
  • Provides an excellent relevance algorithm, a composite Ranking method based on phrase similarity and statistics (BM25);
  • Support distributed search;
  • Providing excerpt generation of documents;
  • Can be used as a MySQL storage engine to provide search services;
  • Supports multiple search modes such as Boolean, phrase, word similarity, etc.;
  • The document supports multiple full-text search fields (maximum no more than 32);
  • Documents support multiple additional attribute information (for example: grouping information, timestamp, etc.);
  • Stop word query;
  • Supports single byte encoding and UTF-8 encoding;
  • Native MySQL support (supports both MyISAM and InnoDB);
  • Native PostgreSQL support.

The Chinese manual is available here, thanks to the translator for his hard work.

2. Installation of Sphinx on Windows

1. Find the latest windows version directly at http://www.sphinxsearch.com/downloads.html. What I downloaded here is Win32 release binaries with MySQL support. After downloading, unzip it in the D:sphinx directory;

2. Create a new data directory under D:sphinx to store index files and a log directory for log files. Copy D:sphinxsphinx.conf.in to D:sphinxbinsphinx.conf (note to modify the file name);

3. Modify D:sphinxbinsphinx.conf. Here are a few that need to be modified:

type        = mysql # 数据源,我这里是mysql<br>sql_host    = localhost # 数据库服务器<br>sql_user    = root # 数据库用户名<br>sql_pass    = '' # 数据库密码<br>sql_db      = test # 数据库<br>sql_port    = 3306 # 数据库端口
sql_query_pre   = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的
index test1<br>{<br># 放索引的目录<br> path   = D:/sphinx/data/<br># 编码<br> charset_type  = utf-8<br> #  指定utf-8的编码表<br> charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F<br> # 简单分词,只支持0和1,如果要搜索中文,请指定为1<br> ngram_len    = 1<br># 需要分词的字符,如果要搜索中文,去掉前面的注释<br> ngram_chars   = U+3000..U+2FA1F<br>}
# index test1stemmed : test1<br># {<br> # path   = @CONFDIR@/data/test1stemmed<br> # morphology  = stem_en<br># }<br><br># 如果没有分布式索引,注释掉下面的内容<br><br># index dist1<br># {<br> # 'distributed' index type MUST be specified<br> # type    = distributed
 # local index to be searched<br> # there can be many local indexes configured<br> # local    = test1<br> # local    = test1stemmed
 # remote agent<br> # multiple remote agents may be specified<br> # syntax is 'hostname:port:index1,[index2[,...]]<br> # agent    = localhost:3313:remote1<br> # agent    = localhost:3314:remote2,remote3
 # remote agent connection timeout, milliseconds<br> # optional, default is 1000 ms, ie. 1 sec<br> # agent_connect_timeout = 1000
 # remote agent query timeout, milliseconds<br> # optional, default is 3000 ms, ie. 3 sec<br> # agent_query_timeout  = 3000<br># }
# 搜索服务需要修改的部分<br>searchd<br>{<br> # 日志<br> log     = D:/sphinx/log/searchd.log
 # PID file, searchd process ID file name<br> pid_file   = D:/sphinx/log/searchd.pid
# windows下启动searchd服务一定要注释掉这个<br> # seamless_rotate  = 1<br>}

4. Import test data

C:Program FilesMySQLMySQL Server 5.0bin>mysql -uroot test

5. Create index

D:sphinxbin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec

D:sphinxbin>

6. Search for ‘test’ and try

D:sphinxbin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec

displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups

words:
1. ‘test’: 3 documents, 5 hits
D:sphinxbin>

Everyone has come out.

6. Test Chinese search

Modify the documents data table in the test database,

UPDATE `test`.`documents` SET `title` = 'Test Chinese', `content` = 'this is my test document number two, you should be able to find it' WHERE `documents`.`id` = 2 ;

Rebuild index:

D:sphinxbin>indexer.exe –all

Try searching for ‘中文’:

D:sphinxbin>search.exe Chinese
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘中文‘: returned 0 matches of 0 total in 0.000 sec

words:
D:sphinxbin>

It seems that it is not found. This is because the encoding in the windows command line is gbk, so of course it cannot be found. We can try it with a program, create a new file foo.php under D:sphinxapi, pay attention to utf-8 encoding

require ‘sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(’localhost’,3312);
$result = $s->Query('中文');
var_dump($result);
?>

Start Sphinx searchd service

D:sphinxbin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:3312
accepting connections

Execute PHP query:

php d:/sphinx/api/foo.php

Have the results come out? The remaining work is to read the manual and slowly explore the high-level configuration.

Articles you may be interested in

  • Installation and configuration tutorial of memcache under windows
  • How to run PHP files under the command line under windows
  • Implementation of sphinx Chinese full-text search
  • Master-slave synchronization backup steps of mysql database in windows environment
  • Installing memcache under windows system
  • How to install and configure PHP to run under Windows 7 Environment
  • How to determine whether a string is in English, pure Chinese, or a combination of Chinese and English
  • Windows cannot start this hardware because its configuration information (in the registry) is incomplete or damaged equipment. (Code 19) Solution

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764167.htmlTechArticleI tried to use Sphinx a while ago, which can be used by various languages ​​​​(PHP/Python/Ruby/etc) Full-text search system that is easy to call. Most of the information on the Internet is for installation and use in the Linux environment...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)