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
文章來源:http://www.zghlxwxcb.cn/news/detail-563327.html
使用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();//先清除索引
// 創(chuàng)建索引
client.createIndex(schema, Client.IndexOptions.Default());
// 向索引中添加文檔
Map<String, Object> fields1 = createDocument("任何視頻", "新文檔中不存在的內(nèi)容將被保留。兩者中存在的字段都會被覆蓋", 1000);
Map<String, Object> fields2 = createDocument("任何通信", "新文檔中不存在的內(nèi)容將丟失", 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("內(nèi)容").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;
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-563327.html
到了這里,關(guān)于Redis實現(xiàn)高性能的全文搜索引擎---RediSearch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!