当前位置: 首页 > wzjs >正文

贵州建设监理协会网站分销商城网站开发

贵州建设监理协会网站,分销商城网站开发,漯河高端网站建设,小加工厂怎么找客户文章目录 1.背景2.安装redisSearch3.添加Maven依赖4.配置类5.实体类和索引5.1 定义实体类5.2 创建索引(含地理字段) 6.数据操作服务7.搜索功能实现7.1 基础搜索7.2 高级搜索功能 8.测试 1.背景 Redis Search 是 Redis 官方提供的全文搜索引擎,它为Redis…

文章目录

  • 1.背景
  • 2.安装redisSearch
  • 3.添加Maven依赖
  • 4.配置类
  • 5.实体类和索引
    • 5.1 定义实体类
    • 5.2 创建索引(含地理字段)
  • 6.数据操作服务
  • 7.搜索功能实现
    • 7.1 基础搜索
    • 7.2 高级搜索功能
  • 8.测试

1.背景

Redis Search 是 Redis 官方提供的全文搜索引擎,它为Redis 提供全文搜索、索引和复杂查询功能。它基于内存存储,结合了 Redis 的高性能和倒排索引技术,支持实时搜索、聚合分析、模糊匹配等场景。RedisSearch 适用于需要快速检索结构化或非结构化数据的应用,如电商搜索、日志分析、实时推荐等。更多详细的介绍可参见:《一文了解亿级数据检索:RedisSearch》

2.安装redisSearch

本文不在赘述,详情可参见:《docker安装redisSearch》

3.添加Maven依赖

<dependencies><!-- SpringBoot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- RedisSearch 核心依赖 --><dependency><groupId>com.redislabs</groupId><artifactId>jredisearch</artifactId><version>2.2.0</version><exclusions><exclusion><groupId>redis.clients</groupId><artifactId>jedis</artifactId></exclusion></exclusions></dependency><!-- 适配 Redis 7.x 的 Jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>
</dependencies>

4.配置类

import io.redisearch.client.Client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RedisSearchConfig {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Beanpublic Client redisSearchClient() {// 创建 RediSearch 客户端return new Client("productIndex", host, port);}
}

application.properties

spring.redis.host=127.0.0.1
spring.redis.port=6379

5.实体类和索引

5.1 定义实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import redis.clients.jedis.GeoCoordinate;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {private String id;private String title;private Double price;private String category;private GeoCoordinate location; // 地理位置坐标
}

5.2 创建索引(含地理字段)

import io.redisearch.Schema;
import io.redisearch.client.Client;
import io.redisearch.client.IndexDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class IndexService {@Autowiredprivate Client client;public void createProductIndex() {Schema schema = new Schema().addTextField("title", 5.0)   // 文本字段,权重5.0.addNumericField("price")     // 数值字段.addTagField("category")      // 标签字段.addGeoField("location");     // 地理字段IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.HASH).setPrefixes("product:");    // 自动索引以 product: 开头的键client.createIndex(schema, Client.IndexOptions.defaultOptions().setDefinition(rule));}
}

6.数据操作服务

import com.example.mapstuct.dto.Product;
import io.redisearch.Document;
import io.redisearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Map;@Service
public class ProductService {@Autowiredprivate Client client;// 添加文档public void addProduct(Product product) {Document doc = new Document("product:" + product.getId()).set("title", product.getTitle()).set("price", product.getPrice()).set("category", product.getCategory()).set("location", product.getLocation().getLongitude() + "," + product.getLocation().getLatitude());client.addDocument(doc);}// 更新文档public void updateProduct(String id, Double score, Map<String, Object> fields) {client.updateDocument("product:" + id, score, fields);}// 删除文档public void deleteProduct(String id) {client.deleteDocument("product:" + id);}public SearchResult searchProducts(String query) {return client.search(new Query(query).limit(0, 10)                // 分页.setWithScores()              // 返回相关性评分);}public SearchResult searchProductsByQuery(Query query) {return client.search(query);}
}

7.搜索功能实现

7.1 基础搜索

public SearchResult searchProducts(String query) {return client.search(new Query(query).limit(0, 10)                // 分页.setWithScores()              // 返回相关性评分);
}

7.2 高级搜索功能

// 高亮显示
Query query = new Query("蓝牙耳机").highlight("title", "<b>", "</b>");  // 高亮标题字段中的匹配词[1](@ref)// 通配符搜索
Query wildcardQuery = new Query("蓝*");    // 匹配“蓝牙”“蓝色”等[1](@ref)// 布尔逻辑
Query boolQuery = new Query("耳机 AND -入耳式"); // 包含“耳机”且不包含“入耳式”[1](@ref)// 范围查询
Query rangeQuery = new Query("@price:[200 500]"); // 价格区间过滤[1](@ref)// 地理位置检索(100公里内)
Query geoQuery = new Query("@location:[经度 纬度 100 km]"); // 基于坐标的范围查询[23,24](@ref)

8.测试

@SpringBootTest
public class RedisSearchDemoTest {@Autowiredprivate IndexService indexService;@Autowiredprivate ProductService productService;@Testvoid fullDemoTest() {// 1. 创建索引indexService.createProductIndex();// 2. 添加数据Product p1 = new Product("1", "无线蓝牙耳机", 299.0, "电子产品", new GeoCoordinate(116.397469, 39.908821));productService.addProduct(p1);// 3. 执行高级搜索SearchResult result = productService.searchProducts("@category:{电子产品} @price:[200 300] @location:[116.397469 39.908821 10 km]");// 4. 输出高亮结果result.getDocuments().forEach(doc -> System.out.println("高亮标题:" + doc.get("title")));}
}

在这里插入图片描述


文章转载自:

http://ZEhkquF8.dtjwm.cn
http://MPEuQut1.dtjwm.cn
http://mgwkPimN.dtjwm.cn
http://2uOI13To.dtjwm.cn
http://s4AJDsRn.dtjwm.cn
http://7pRIPC8s.dtjwm.cn
http://PZ67k0RK.dtjwm.cn
http://qfwFn76q.dtjwm.cn
http://YeqtGBpH.dtjwm.cn
http://m82THWCR.dtjwm.cn
http://wvmPrwKo.dtjwm.cn
http://TJSezfmC.dtjwm.cn
http://Y42vVsGz.dtjwm.cn
http://HE8tEO5Q.dtjwm.cn
http://brIYZXmk.dtjwm.cn
http://ZEs4Xd6J.dtjwm.cn
http://i9B6Wkvu.dtjwm.cn
http://M03bluF7.dtjwm.cn
http://JOCCIbky.dtjwm.cn
http://XniE311y.dtjwm.cn
http://tbbTRL0u.dtjwm.cn
http://VuZHUkVS.dtjwm.cn
http://93QU4TWM.dtjwm.cn
http://3wBai1PI.dtjwm.cn
http://bxmXzzeC.dtjwm.cn
http://qTLeLMwH.dtjwm.cn
http://TuY4mK8D.dtjwm.cn
http://izXuj91O.dtjwm.cn
http://FSNBHoef.dtjwm.cn
http://wnNh5XcT.dtjwm.cn
http://www.dtcms.com/wzjs/731645.html

相关文章:

  • 做网站可以临摹吗学校网站免费建设
  • 建网站的经历网络服务机构的域名是什么
  • 做网站的技术员提供深圳网站制作公司
  • 网站开发设计合同美丽乡村 村级网站建设
  • 广州网站搭建多少钱网站数据修改
  • 手机网站 设置青州网站建设
  • 新网站建设哪家好厦门有没有做网站的
  • 中化建工北京建设投资有限公司网站google官网入口
  • 怎么当网站站长浙江建设职业技术学校网站登录
  • 营销网站模板wordpress getfooter
  • pc网站开发制作个人网站起个名字
  • 企业网站建设讲解制作企业网站得多长时间
  • 一个电商网站开发周期是多久做网站月入7000
  • 网站建设时间规划建设部网站 43号文件
  • 做网站开发使用百分比的好处南宁网站建设策划方案
  • 网站流量的重要性wordpress图文主题
  • 网站开发 开题报告国产前端框架 做网站
  • 网站用单页面框架做河南电商网站设计
  • 正规的大连网站建设网站seo排名优化软件
  • 昆明做网站优化公司百度网站验证怎么做
  • 企业如何进行网站推广芜湖那里帮人做销售网站
  • o2o网站制作中文企业网站模板html
  • 网站建设及使用商务网站建设规划心得
  • 大良营销网站建设行情个人建站需要多少钱
  • 东莞网站设计开发杭州有专业做网站小型服装厂吗
  • 高端网站建设设计公司哪家好广东建设集团有限公司官网
  • 天津高端网站建设制作茂名模板建站哪家好
  • 曲靖企业网站建设上街免费网站建设
  • 胡歌做的穿衣搭配的网站ups国际快递网站建设模块分析
  • 美食怎么做的小视频网站湘潭sem优化价格