简介
Redisearch在Redis上面实现了一个搜索引擎,但与其他Redis搜索库不同,它不使用内部数据结构,如排序集。
这也可以实现更高级的功能,如文本查询的完全词组匹配和数字过滤,这对传统的redis搜索几乎是不可能或高效的。
开发环境
本机 Mac
服务器 CentOS 7.3
环境搭建(服务器)
1、安装 Redis, 最新版本 ->
$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
$ tar xzf redis-4.0.1.tar.gz
$ cd redis-4.0.1
$ make
小提示:
yum install -y gcc g++ gcc-c++ make
2、配置 Redis,编辑 redis.conf,修改三处
$ vi redis.conf
- 将 bind 127.0.0.1 改为 # bind 127.0.0.1
- 将 protected-mode yes 改为 protected-mode no
- 将 daemonize no 改为 daemonize yes
3、安装 Redisearch,在 redis-4.0.1 同级目录下执行,官网 ->
$ git clone https://github.com/RedisLabsModules/RediSearch.git
$ cd RediSearch/src
$ make all
4、启动 Redis
$ cd redis-4.0.1
$ src/redis-server redis.conf --loadmodule ../RediSearch/src/redisearch.so
环境搭建(本机)
1、使用 Maven 安装最新的 Jedis,方便依赖
$ git clone https://github.com/xetorthio/jedis.git
$ cd jedis
$ mvn clean install -DskipTests
2、使用 Maven 安装最新的 JRedisearch,方便依赖
$ git clone https://github.com/RedisLabs/JRediSearch.git
$ cd JRediSearch
$ mvn clean install -DskipTests
开始使用 Redisearch
1、在 IDEA 中新建 Maven 工程,添加如下依赖
<dependencies>
<!-- 取决于上述安装的version -->
<dependency>
<groupId>com.redislabs</groupId>
<artifactId>jredisearch</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 取决于上述安装的version -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
2、编写测试程序 Application.java
import io.redisearch.Document;
import io.redisearch.Query;
import io.redisearch.Schema;
import io.redisearch.SearchResult;
import io.redisearch.client.Client;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Application {
public static void main(String[] args) {
// 初始化客户端
Client client = new Client("any-search", "113.209.107.69", 6379);
// 定义一个索引模式
Schema schema = new Schema().addTextField("title", 5.0).addTextField("body", 1.0).addNumericField("star");
// 创建索引
client.dropIndex();
client.createIndex(schema, Client.IndexOptions.Default());
// 向索引中添加文档
Map<String, Object> fields1 = createDocument("any video", "视频", 1000);
Map<String, Object> fields2 = createDocument("any chat", "即时通信", 500);
Map<String, Object> fields3 = createDocument("any security", "安全", 10000);
Map<String, Object> fields4 = createDocument("any video github", "视频项目", 10000);
client.addDocument("doc1", fields1);
client.addDocument("doc2", fields2);
client.addDocument("doc3", fields3);
client.addDocument("doc4", fields4);
// 搜索 "any", 并且 star 数目在 0 ~ 2000 的
System.out.println("search 'any', result:");
Query query = new Query("any").addFilter(new Query.NumericFilter("star", 0, 2000)).setWithScores().limit(0, 10);
SearchResult result = client.search(query);
printResult(result);
// 搜索 "视频"
System.out.println("\r\nsearch '视频', result:");
Query other = new Query("视频").limitFields("body").limit(0, 10);
result = client.search(other);
printResult(result);
}
private static Map<String, Object> createDocument(String title, String body, Integer price){
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("title", title);
fields.put("body", body);
fields.put("star", price);
return fields;
}
private static void printResult(SearchResult result) {
List<Document> documentList = result.docs;
for (Document document : documentList) {
System.out.println(document.toString());
}
}
}
3、执行结果
总结
速度快,但是对中文搜索支持不好,如上所示,搜索“视频”时,field4中有“视频项目”却没有出现在结果中,需要将“视频项目”隔开成“视频 项目”才行。