当前位置: 首页 > 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://www.dtcms.com/wzjs/571677.html

相关文章:

  • 网站代码开发方式沈阳网站 房小二
  • 北京企业网站开发费用快速排名网站系统
  • 网站模版 下载工具建筑八大员报考时间和条件
  • 代理网站官网如何看小程序是哪家公司做的
  • 内蒙古建设厅官网站东莞seo网站建设公司
  • 做网站用了别人公司的图片可以吗聚名网官网登录入口
  • 林业建设协会网站关于网站备案的公告
  • 外贸网站做开关的哪个好wordpress无法编辑页面
  • 建网站网络公司广州cms模板建站
  • 推广网站怎么做能增加咨询网站优化的推广
  • 网站建设要注意什么射阳网站开发
  • 外贸企业网站推广方案wordpress模板使用
  • 开发网站制作wordpress v2pai
  • 什么是响应式的网站网页公司制作
  • 外贸网站 建设游戏下载网站 wordpress
  • 渭南做网站用php做一网站
  • 19楼网站模板宁波网络公司在哪里
  • 建设企业网站有什么好处新手学做网站txt
  • 网站流量用什么表示企业名录查询软件
  • 郑州市建设教育协会网站规划一个电子商务网站
  • 做科技汽车的视频网站微网站如何做宣传
  • 青岛网络建站公司wordpress游记主题
  • 吉林集安市建设局网站江苏省交通建设局网站首页
  • 扬州公司做网站公司磁力猫引擎入口
  • 网站 二维码的作用阳江市建设网站
  • 14年网站开发经验有字库wordpress
  • 网站备案 企业网站建设专家 金石下拉
  • 购物网站开发流程公司网站制做
  • 访问不了服务器网站吗做众筹网站怎么赚钱吗
  • 成都服装网站建设wordpress app插件下载