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

国内免费网站服务器推荐长沙网站优化排名推广

国内免费网站服务器推荐,长沙网站优化排名推广,wordpress线下安装教程视频,哪里做外贸网站文章目录 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/261599.html

相关文章:

  • 微网站定制多久百度提升优化
  • 游戏网站建设方案书莱阳seo排名
  • 充值选建设银行打不开网站搭建网站步骤
  • 店面设计布局苏州seo网站公司
  • 手机开网站seo教程 seo之家
  • 酒店网站建设的构思网站制作方案
  • 网站建设潍坊厦门网站外包
  • 网站建设目标分析百度问问首页
  • 便宜的网站建设灰色行业seo大神
  • 保健品手机网站模板论坛seo招聘
  • 做网站怎么给客户打电话优化大师免费下载
  • 上海企业网站建设服务百度seo关键词排名优化工具
  • 做网站的html代码格式上海网站排名优化公司
  • 网站首页介绍产品推广软文范文
  • 电商视觉设计网站电子商务seo
  • 做网站开发的薪酬怎么样网站推广的四个阶段
  • 微商自己做网站竞价sem托管公司
  • 怎样做网站平叿软文是什么文章
  • 湘潭做网站价格找磐石网络一流网店运营入门基础知识
  • 做一个网站成本多少免费google账号注册入口
  • 江苏泗阳今天新增病例多少google seo是什么意思
  • 软件技术专业介绍网站快速排名优化价格
  • 网站建设与管理课后作业答案销售管理系统
  • 济南企业建设网站怎么优化一个网站关键词
  • 青岛本地招聘网站无代码建站
  • 有什么网站可以下做闭软件郑州百度seo排名公司
  • 公司建设网站需要去哪报备中国第一营销网
  • 河北邯郸市简介seo优化软件大全
  • 简述商业网站建设的流程网络推广渠道公司
  • 建一个自己的网站有什么用百度竞价推广费用