当前位置:网站首页>Redis实现高性能的全文搜索引擎---RediSearch
Redis实现高性能的全文搜索引擎---RediSearch
2022-07-05 08:40:00 【抹香鲸之海】
RediSearch是一个Redis模块,为Redis提供查询、二次索引和全文搜索,他的性能甚至比es还要高。
安装:
docker pull redislabs/redismod:preview
启动容器:
注意端口号不要和redis冲突了:
docker run -p 6399:6379 --name redismod -v /mydata/redismod/data:/data -d redislabs/redismod:preview
使用springboot 集成一下:
pom
<dependency>
<groupId>com.redislabs</groupId>
<artifactId>jredisearch</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.8</version>
</dependency>
代码
package com.example.demo.Utils.RedisearchUtills;
import io.redisearch.*;
import io.redisearch.client.AddOptions;
import io.redisearch.client.Client;
import java.util.HashMap;
import java.util.Map;
public class redisearchMain {
public static void main(String[] args) {
Client client = new Client("student", "120.48.54.67", 6399);
// 定义一个索引模式
Schema schema = new Schema().addTextField("title", 5.0).addTextField("body", 1.0).addNumericField("star");
// 首次run 可以先注释掉
client.dropIndex();//先清除索引
// 创建索引
client.createIndex(schema, Client.IndexOptions.Default());
// 向索引中添加文档
Map<String, Object> fields1 = createDocument("任何视频", "新文档中不存在的内容将被保留。两者中存在的字段都会被覆盖", 1000);
Map<String, Object> fields2 = createDocument("任何通信", "新文档中不存在的内容将丢失", 500);
// client.addDocument("doc1", fields1);
// client.addDocument("doc2", fields2);
Document doc1 = new Document("doc1", fields1, 1.0, null);
Document doc2 = new Document("doc2", fields2, 1.0, null);
AddOptions options = new AddOptions().setNosave(false);
options.setLanguage("chinese");
client.addDocument(doc1, options);
client.addDocument(doc2, options);
Query query = new Query("内容").addFilter(new Query.NumericFilter("star", 0, 1500)).setWithScores().limit(0, 10);
SearchResult result = client.search(query);
result.docs.stream().forEach(docs->
System.out.println("====="+docs)
);
}
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;
}
}
边栏推荐
- Example 007: copy data from one list to another list.
- Basic number theory - factors
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
- L298N module use
- 整形的分类:short in long longlong
- Digital analog 2: integer programming
- Speech recognition learning summary
- [noi simulation] juice tree (tree DP)
- Is the security account given by Yixue school safe? Where can I open an account
猜你喜欢
Guess riddles (8)
Example 007: copy data from one list to another list.
Run菜单解析
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Lori remote control commissioning record
[three tier architecture]
STM32 single chip microcomputer -- volatile keyword
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
Guess riddles (6)
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
随机推荐
Speech recognition learning summary
696. 计数二进制子串
Business modeling of software model | overview
实例010:给人看的时间
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Agile project management of project management
猜谜语啦(3)
猜谜语啦(9)
Business modeling of software model | object modeling
Bit operation related operations
C language data type replacement
Lori remote control commissioning record
Sword finger offer 09 Implementing queues with two stacks
Guess riddles (2)
【日常训练】1200. 最小绝对差
Lori remote control LEGO motor
MATLAB小技巧(28)模糊綜合評價
猜谜语啦(2)
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
亿学学堂给的证券账户安不安全?哪里可以开户