Home Backend Development PHP Tutorial Simple example of php sphinx

Simple example of php sphinx

Jul 25, 2016 am 09:00 AM

Let me introduce you to a simple example of php sphinx. Friends in need can refer to it.

The code is as follows:

<?php
//sphinx简单例子
//参数筛选
//筛选cat_id=2
$cl->SetFilter("cat_id",array(2));
//仅在id为1、3、7的子论坛中搜索
$cl->SetFilter("forum_id",array(1,3,7));
 
//范围筛选
//筛选发布时间为今天,参数为int时间戳
$cl->SetFilterRange("starttime",123,124);
//筛选价格
$cl->SetFilterRange("price",10.0,99.9);
 
// 分组
//按照item_id分组,并且按照order desc排序
$cl->SetGroupBy("item_id",SPH_GROUP_ATTR,"order desc");
 
//排序模式
//按照price desc排序
$cl->SetSortMode(SPH_SORT_ATTR_DESC,"price");
 注意:会被SetGroupBy中的排序覆盖 

// 匹配查询词中的任意一个
$cl->SetMatchMode ( SPH_MATCH_ANY );
SPH_MATCH_ALL, 匹配所有查询词(默认模式);
SPH_MATCH_ANY, 匹配查询词中的任意一个;
SPH_MATCH_PHRASE, 将整个查询看作一个词组,要求按顺序完整匹配;
SPH_MATCH_BOOLEAN, 将查询看作一个布尔表达式 (参见 第 5.2 节 “布尔查询语法”);
SPH_MATCH_EXTENDED, 将查询看作一个CoreSeek/Sphinx内部查询语言的表达式 (参见 第 5.3 节 “扩展查询语法”). 从版本Coreseek 3/Sphinx 0.9.9开始, 这个选项被选项SPH_MATCH_EXTENDED2代替,它提供了更多功能和更佳的性能。保留这个选项是为了与遗留的旧代码兼容——这样即使 Sphinx及其组件包括API升级的时候,旧的应用程序代码还能够继续工作。
SPH_MATCH_EXTENDED2, 使用第二版的“扩展匹配模式”对查询进行匹配.
SPH_MATCH_FULLSCAN, 强制使用下文所述的“完整扫描”模式来对查询进行匹配。注意,在此模式下,所有的查询词都被忽略,尽管过滤器、过滤器范围以及分组仍然起作用,但任何文本匹配都不会发生.

//从0开始查询,查询30条,返回结果最多为1000
$cl->setLimits(0,30,1000);
 
// 从名称为index的sphinx索引查询“电影票”
 $cl->Query("电影票","index");
 
// 从名称为index的sphinx索引查询“电影票”
$sp->SetGroupBy('item_id',SPH_GROUP_ATTR,'s_order desc');
$sp->SetFilter('city_id','1');
$sp->SetFilter('cat_id',array(1));
$sp->SetLimit(0,10,1000);
$sp->AddQuery('电影票','index');
$sp->ResetFilters();//重置筛选条件
$sp->ResetGroupBy();//重置分组
 
$sp->SetGroupBy('item_id', SPH_GROUPBY_ATTR, 's_order desc');
$sp->setFilter('city_id', '2');
$sp->setFilter('cat_id', array(2));
$sp->setLimits(0, 20, 1000);
$sp->AddQuery('温泉', 'index');
$sp->ResetFilters();// 重置筛选条件
$sp->ResetGroupBy();//重置分组
$results = $sp->RunQuries();
//by http://bbs.it-home.org
?>
Copy after login

For the official introduction of php sphinx, please refer to the document: http://bbs.it-home.org/shouce/php5/book.sphinx.html.

Batch queries (or multi-queries) enable searchd to perform possible internal optimizations and reduce overhead in terms of network connections and process creation in any case. Relative to individual queries, batch queries do not introduce any additional overhead. So when your Web page runs several different queries, be sure to consider using batch queries.

For example, running the same full-text query multiple times but using different sorting or grouping settings will cause searchd to run the expensive full-text search and relevance calculation only once, and then generate multiple grouped results based on this.

Sometimes it is not only necessary to simply display the search results, but also to display some category-related counting information, such as the number of products grouped by manufacturer. In this case, batch query will save a lot of overhead. Without batch queries, you would have to run these essentially identical queries multiple times and retrieve the same matches, resulting in different result sets. If you use batch queries, you simply combine these queries into a batch query, and Sphinx will optimize these redundant full-text searches internally.

AddQuery() internally stores all current setting status and queries, and you can also change settings in subsequent AddQuery() calls. Queries added earlier will not be affected, there is really no way to change them.

Using the above code, the first query will query "hello world" on the "documents" index and sort the results by relevance. The second query will query "ipod" on the "products" index and sort the results by price. , the third query searches for "harry potter" on the "books" index, and the results are still sorted by price. Note that the second SetSortMode() call does not affect the first query (because it has already been added), but the next two queries will.

In addition, any filtering set before AddQuery() will continue to be used by subsequent queries. Therefore, if you use SetFilter() before the first query, the second query executed via AddQuery() (and subsequent batch queries) will have the same filtering applied, unless you first call ResetFilters() to clear the filtering rules. At the same time, you can add new filtering rules at any time.

AddQuery() does not modify the current state. That is, all existing sorting, filtering, and grouping settings will not be changed by this call, so subsequent queries can easily reuse existing settings.

AddQuery() returns an index in the array returned by the RunQueries() result. It is an incrementing integer starting from 0, i.e. the first call returns 0, the second returns 1, and so on. This convenient feature saves you from having to record the subscripts manually when you need them.



Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles