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

个人网站html源码营销网络推广方式有哪些

个人网站html源码,营销网络推广方式有哪些,上海做運動网站的公司,wordpress注册教程一、没有使用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.dtcms.com/wzjs/49946.html

相关文章:

  • 政府网站建设情况交流汇报互联网营销平台
  • 公司网站的开发福州seo推广公司
  • 广州知名网站建设有哪些百度网站关键词排名助手
  • 石家庄网站建设案例郑州网络营销哪个好
  • 灌南网站定制大地资源网在线观看免费
  • 网站开发需要解决难题快速整站优化
  • 南宁免费建站模板seo是什么姓
  • asp.net做网站后台国家免费技能培训官网
  • 北京最大做网站的公司十大职业资格培训机构
  • 重庆便宜做网站的新乡百度网站优化排名
  • 复兴企业做网站推广关键词优化需要从哪些方面开展
  • 网站建设广东sem推广竞价托管公司
  • 企业网站建设规划方案产品设计
  • 页面设计时说法正确的是seo公司软件
  • 网站效果图制作产品销售推广方案
  • 做网站广告多少钱百度推广关键词越多越好吗
  • 网站建设设计 飞沐社群运营
  • 天工网官方网站互联网营销案例
  • 如何在服务器上关闭网站关键词搜索神器
  • 网站做以后怎么修改网站内容自动点击器怎么用
  • flash是怎么做网站的seo 页面
  • 网站后台这么做视频教程b站视频推广网站400
  • 网站服务器停止响应怎么办地推接单正规平台
  • 泰国做网站百度网站排名优化价格
  • 西安十大网站制作公司电商平台如何推广运营
  • 杭州有哪些网站建设今天军事新闻最新消息
  • 醴陵网站开发宁波seo快速优化公司
  • 跨境电商单页网站的详情页怎么做的厦门百度关键词优化
  • 做游戏门户网站要注意什么阿里域名购买网站
  • 17一起做网站客服百度seo公司报价