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

专业的外贸网站建设公司价格cnzz统计

专业的外贸网站建设公司价格,cnzz统计,佛山官网建设,怎么做网站frontpage一、没有使用Elasticsearch的查询速度698ms 1.数据库模糊查询不走索引,在数据量较大的时候,查询性能很差。需要注意的是,数据库模糊查询随着表数据量的增多,查询性能的下降会非常明显,而搜索引擎的性能则不会随着数据增…

一、没有使用Elasticsearch的查询速度698ms

 1.数据库模糊查询不走索引,在数据量较大的时候,查询性能很差。需要注意的是,数据库模糊查询随着表数据量的增多,查询性能的下降会非常明显,而搜索引擎的性能则不会随着数据增多而下降太多。目前仅10万不到的数据量差距就如此明显,如果数据量达到百万、千万、甚至上亿级别,这个性能差距会非常夸张。

2.功能单一

数据库的模糊搜索功能单一,匹配条件非常苛刻,必须恰好包含用户搜索的关键字。而在搜索引擎中,用户输入出现个别错字,或者用拼音搜索、同义词搜索都能正确匹配到数据。

综上,在面临海量数据的搜索,或者有一些复杂搜索需求的时候,推荐使用专门的搜索引擎来实现搜索功能。

二、 Elasticsearch使用

1.依赖

版本:因为SpringBoot默认的ES版本是7.17.10,所以我们需要覆盖默认的ES版本:

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
  <properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><elasticsearch.version>7.12.1</elasticsearch.version></properties>

启动es服务---测试是否连接es成功:

package com.itfly;import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import java.io.IOException;public class IndexTest {private RestHighLevelClient client;@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));}@Testvoid testConnect() {System.out.println(client);}@AfterEachvoid tearDown() throws IOException {this.client.close();}
}

成功连接

2、创建索引库 

实现搜索功能需要的字段包括三大部分:

  • 搜索过滤字段

    • 分类

    • 品牌

    • 价格

  • 排序字段

    • 默认:按照更新时间降序排序

    • 销量

    • 价格

  • 展示字段

    • 商品id:用于点击后跳转

    • 图片地址

    • 是否是广告推广商品

    • 名称

    • 价格

    • 评价数量

    • 销量

代码分为三步:

  • 1)创建Request对象。

    • 因为是创建索引库的操作,因此Request是CreateIndexRequest

  • 2)添加请求参数

    • 其实就是Json格式的Mapping映射参数。因为json字符串很长,这里是定义了静态字符串常量MAPPING_TEMPLATE,让代码看起来更加优雅。

  • 3)发送请求

    • client.indices()方法的返回值是IndicesClient类型,封装了所有与索引库操作有关的方法。例如创建索引、删除索引、判断索引是否存在等

 

    @Testvoid testCreateIndex() throws IOException {// 1.创建Request对象CreateIndexRequest request = new CreateIndexRequest("goods");// 2.准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3.发送请求client.indices().create(request, RequestOptions.DEFAULT);}static final String MAPPING_TEMPLATE = "{\n" +"  \"mappings\": {\n" +"    \"properties\": {\n" +"      \"id\": { \"type\": \"long\" },\n" +"      \"name\": { \"type\": \"text\", \"analyzer\": \"ik_smart\" },\n" +"      \"description\": { \"type\": \"text\", \"analyzer\": \"ik_smart\" },\n" +"      \"price\": { \"type\": \"scaled_float\", \"scaling_factor\": 100 },\n" +"      \"stock\": { \"type\": \"integer\" },\n" +"      \"status\": { \"type\": \"integer\" },\n" +"      \"image\": { \"type\": \"keyword\", \"index\": false },\n" +"      \"created_at\": { \"type\": \"date\", \"format\": \"yyyy-MM-dd HH:mm:ss\" },\n" +"      \"updated_at\": { \"type\": \"date\", \"format\": \"yyyy-MM-dd HH:mm:ss\" }\n" +"    }\n" +"  }\n" +"}";

 判断索引库是否存在

@Test
void testExistsIndex() throws IOException {// 1.创建Request对象GetIndexRequest request = new GetIndexRequest("goods");// 2.发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3.输出System.err.println(exists ? "索引库已经存在!" : "索引库不存在!");
}

 

RestClient操作文档

索引库准备好以后,就可以操作文档了。为了与索引库操作分离,我们再次创建一个测试类,做两件事情:

package com.itfly;import com.itfly.service.IProductsService;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.io.IOException;@SpringBootTest(properties = "spring.profiles.active=local")
public class DocumentTest {private RestHighLevelClient client;@Autowiredprivate IProductsService productsService;@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}
}

新增文档

我们需要将数据库中的商品信息导入elasticsearch中,而不是造假数据了。

语法:
 

POST /{索引库名}/_doc/1
{"name": "Jack","age": 21
}

可以看到与索引库操作的API非常类似,同样是三步走:

  • 1)创建Request对象,这里是IndexRequest,因为添加文档就是创建倒排索引的过程

  • 2)准备请求参数,本例中就是Json文档

  • 3)发送请求

变化的地方在于,这里直接使用client.xxx()的API,不再需要client.indices()了。

    @Testvoid testAddDocument() throws IOException {// 1.根据id查询商品数据,批量新增文档productsService.list().forEach(item -> {IndexRequest indexRequest = new IndexRequest("goods");indexRequest.id(item.getId().toString());indexRequest.source(JSONUtil.parseObj(item), XContentType.JSON);try {client.index(indexRequest, RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();}System.out.println("添加成功");});//}

 把所有的数据加入索引库,并创建文档

RestClient查询 



  • 第一步,创建SearchRequest对象,指定索引库名

  • 第二步,利用request.source()构建DSL,DSL中可以包含查询、分页、排序、高亮等

    • query():代表查询条件,利用QueryBuilders.matchAllQuery()构建一个match_all查询的DSL

  • 第三步,利用client.search()发送请求,得到响应

@Test
void testMatchAll() throws IOException {// 1.创建RequestSearchRequest request = new SearchRequest("goods");// 2.组织请求参数request.source().query(QueryBuilders.matchAllQuery());// 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应handleResponse(response);
}private void handleResponse(SearchResponse response) {SearchHits searchHits = response.getHits();// 1.获取总条数long total = searchHits.getTotalHits().value;System.out.println("共搜索到" + total + "条数据");// 2.遍历结果数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {// 3.得到_source,也就是原始json文档String source = hit.getSourceAsString();// 4.反序列化并打印ItemDoc item = JSONUtil.toBean(source, ItemDoc.class);System.out.println(item);}
}

 查询

package com.itfly.controller;import com.itfly.DTO.ProductsSearchDTO;
import com.itfly.resp.ResultData;
import com.itfly.service.IProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.util.Map;/*** @author yyf* description* @date 2025/3/27 18:33*/
@RestController
@RequestMapping("/es")
@Api(tags = "商品")
public class SearchController {@Autowiredprivate IProductsService productsService;@PostMapping("/list")@ApiOperation(value = "查询所有商品")public ResultData list(@RequestBody ProductsSearchDTO productsSearchDTO) {// 创建客户端RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200)));try {// 构建查询请求SearchRequest searchRequest = new SearchRequest("goods");SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.query(QueryBuilders.matchQuery("name",productsSearchDTO.getName() ));sourceBuilder.size(10);searchRequest.source(sourceBuilder);// 执行查询SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);// 处理结果for (SearchHit hit : response.getHits().getHits()) {// 获取文档内容,返回ResultDataMap<String, Object> sourceAsMap = hit.getSourceAsMap();return ResultData.success(sourceAsMap);}} catch (IOException e) {e.printStackTrace();} finally {try {client.close(); // 关闭客户端} catch (IOException e) {e.printStackTrace();}}return null;}}

总结:

 数据库模糊查询不走索引,在数据量较大的时候,查询性能很差。需要注意的是,数据库模糊查询随着表数据量的增多,查询性能的下降会非常明显,而搜索引擎的性能则不会随着数据增多而下降太多。目前仅10万不到的数据量差距就如此明显,如果数据量达到百万、千万、甚至上亿级别,这个性能差距会非常夸张。

其次,功能单一

数据库的模糊搜索功能单一,匹配条件非常苛刻,必须恰好包含用户搜索的关键字。而在搜索引擎中,用户输入出现个别错字,或者用拼音搜索、同义词搜索都能正确匹配到数据。

综上,在面临海量数据的搜索,或者有一些复杂搜索需求的时候,推荐使用专门的搜索引擎来实现搜索功能。

http://www.cadmedia.cn/news/11918.html

相关文章:

  • 做网站推广那家好代理广告投放平台
  • 域名及密码登录域名管理网站app推广兼职是诈骗吗
  • 免费开发个人小程序的平台武汉久都seo
  • 商贸公司寮步网站建设爱链在线
  • 北京品牌网站设计中国最权威的网站排名
  • 网站建设流程方案微信营销怎么做
  • 无极ip爱站网站长seo综合查询工具
  • 成都手机网站开发网站模板哪家好
  • 礼品定制山西seo顾问
  • 海南省建设人力资源网站常见的网站推广方法有哪些
  • 汕头做网站设计移动端排名优化软件
  • 案例 网站最快的新闻发布平台
  • 网络公司门头照片seo站
  • 烟台网站建设找三硕科技seo学校培训课程
  • 网站建设 锋云科技公司网站设计制作一条龙
  • 电商公司的网站设计书网络营销模式
  • 怎么做网站营销策划电商运营公司
  • 个人怎么成立公司南宁百度快速优化
  • 做网站优化郑州网站推广公司排名
  • 网站建设帝国百度关键字排名软件
  • seo工具软件手机优化管家
  • 单页营销型网站网店运营推广平台
  • 深圳龙华新区属于什么区网站关键词快速优化
  • 嘉兴高端建站公司sem工具是什么
  • 保定制作公司网站发帖推广百度首页
  • 网站改版阿里云怎么做网站301定向竞价关键词排名软件
  • 网站建设的岗位职责网站制作软件
  • 网站建设费 科研 类百度做网站需要多少钱
  • 杨家坪网站建设线上营销公司
  • 西安俄语网站建设手机上制作网页