当前位置:网站首页>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;
}
}

边栏推荐
- One question per day - replace spaces
- Example 006: Fibonacci series
- Classification of plastic surgery: short in long long long
- Basic number theory -- Euler function
- Guess riddles (4)
- How to write cover letter?
- Agile project management of project management
- 实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
- Typical low code apaas manufacturer cases
- 猜谜语啦(6)
猜你喜欢
随机推荐
Xrosstools tool installation for X-Series
每日一题——输入一个日期,输出它是该年的第几天
Guess riddles (8)
Arduino+a4988 control stepper motor
Lori remote control commissioning record
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Infix expression evaluation
319. Bulb switch
Guess riddles (9)
Explore the authentication mechanism of StarUML
[noi simulation] juice tree (tree DP)
Affected tree (tree DP)
How can fresh students write resumes to attract HR and interviewers
剑指 Offer 09. 用两个栈实现队列
關於線性穩壓器的五個設計細節
Business modeling of software model | vision
猜谜语啦(5)
暑假第一周
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Arrangement of some library files







