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

购物商城平台开发南昌搜索引擎优化

购物商城平台开发,南昌搜索引擎优化,网站开发使用什么运行软件,用开源吗做的网站可以用吗文章目录 前言一、环境准备二、RsetAPI操作索引库1.创建索引库2.判断索引库是否存在3.删除索引库 二、RsetAPI操作文档1.新增文档2.单条查询3.删除文档4.增量修改5.批量导入6.自定义响应解析方法 四、常用的查询方法1.MatchAll():查询所有2.matchQuery():单字段查询3.multiMatc…

文章目录

  • 前言
  • 一、环境准备
  • 二、RsetAPI操作索引库
    • 1.创建索引库
    • 2.判断索引库是否存在
    • 3.删除索引库
  • 二、RsetAPI操作文档
    • 1.新增文档
    • 2.单条查询
    • 3.删除文档
    • 4.增量修改
    • 5.批量导入
    • 6.自定义响应解析方法
  • 四、常用的查询方法
    • 1.MatchAll():查询所有
    • 2.matchQuery():单字段查询
    • 3.multiMatchQuery():多字段查询
    • 4.termQuery():词条精确值查询
    • 5.rangeQuery():范围查询
    • 6.bool复合查询
    • 7.分页查询


前言

ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES,其中的Java Rest Client又包括两种:

  • Java Low Level Rest Client
  • Java High Level Rest Client

本文介绍的是Java HighLevel Rest Client客户端API;


一、环境准备

在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类
中,必须先完成这个对象的初始化,建立与elasticsearch的连接。
1)引入es的RestHighLevelClient依赖:

dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2)初始化RestHighLevelClient:
这里为了单元测试方便,我们创建一个测试类HotelIndexTest,然后将初始化的代码编写在
@BeforeEach方法中:

/*** @author 杨树林* @version 1.0* @since 12/8/2023*/@SpringBootTest
class HotelIndexTest{private RestHighLevelClient client;@BeforeEachvoid setUp(){this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}}

3)创建HotelConstants类,定义mapping映射的JSON字符串常量

public class HotelConstants {public static final String MAPPING_TEMPLATE = "{\n" +"  \"mappings\": {\n" +"    \"properties\": {\n" +"      \"id\": {\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"name\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"address\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"price\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"score\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"brand\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"city\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"starName\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"business\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"location\":{\n" +"        \"type\": \"geo_point\"\n" +"      },\n" +"      \"pic\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"all\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\"\n" +"      }\n" +"    }\n" +"  }\n" +"}";
}

二、RsetAPI操作索引库

编写单元测试,实现一下功能:

1.创建索引库

	@Testvoid creatHotelIndex() throws IOException {//1、创建Requset对象CreateIndexRequest request = new CreateIndexRequest("hotels");//2、准备请求的参数:DEL语句request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);//3、发起请求client.indices().create(request,RequestOptions.DEFAULT);}

2.判断索引库是否存在

	@Testvoid testExistsHotelIndex() throws IOException {//1、创建Requset对象GetIndexRequest  request = new GetIndexRequest("hotels");//2、发起请求boolean isExists = client.indices().exists(request,RequestOptions.DEFAULT);System.err.println(isExists ? "索引库已经存在!" : "索引库不存在!");}

3.删除索引库

	@Testvoid delHotelIndex() throws IOException {//1、创建Requset对象DeleteIndexRequest request = new DeleteIndexRequest("hotels");//2、发起请求client.indices().delete(request,RequestOptions.DEFAULT);}

二、RsetAPI操作文档

1.新增文档

	@AutowiredHotelServiceImpl service;@Testvoid addDocument() throws IOException {// 1.根据id查询酒店数据Hotel hotel = service.getById("36934");// 2.转换为文档类型HotelDoc hotelDoc = new HotelDoc(hotel);// 3.将HotelDoc转jsonString json = JSON.toJSONString(hotelDoc);IndexRequest request = new IndexRequest("hotels").id(hotelDoc.getId().toString());request.source(json, XContentType.JSON);client.index(request, RequestOptions.DEFAULT);}

2.单条查询

    @Testvoid getDocument() throws IOException {GetRequest request = new GetRequest("hotels","36934");GetResponse response =  client.get(request, RequestOptions.DEFAULT);String json = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);System.out.println(hotelDoc);}

3.删除文档

    @Testvoid delDocument() throws IOException {DeleteRequest request  = new DeleteRequest("hotels","36934");client.delete(request,RequestOptions.DEFAULT);}

4.增量修改

api中全局修改与新增一致

    @Testvoid UpdateDocument() throws IOException {UpdateRequest request = new UpdateRequest("hotels", "36934");request.doc("name","XX酒店","city","西安","price", "200000","starName", "八星级");client.update(request, RequestOptions.DEFAULT);}

5.批量导入

 	@Testvoid addBulkRequest() throws IOException {//查询所有酒店信息List<Hotel> hotels = service.list();//1.创建requestBulkRequest request = new BulkRequest();for (Hotel hotel : hotels) {HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotels").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));}client.bulk(request,RequestOptions.DEFAULT);}

6.自定义响应解析方法

void show(SearchResponse response){//解析响应SearchHits searchHits =response.getHits();//获取总条数Long total = searchHits.getTotalHits().value;System.out.println("共搜到"+total+"条数据");//文档数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {String json = hit.getSourceAsString();System.err.println(json);HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);System.out.println(hotelDoc);}}

四、常用的查询方法

1.MatchAll():查询所有

	@Testvoid testMatchAll() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotels");//2、准备DEl,QueryBuilders构造查询条件request.source().query(QueryBuilders.matchAllQuery());//3.执行查询,返回响应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析响应show(response);}

2.matchQuery():单字段查询

	@Testvoid testMatch() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotels");// 2.准备DSL 参数1:字段  参数2:数据request.source().query(QueryBuilders.matchQuery("all","如家"));//3.执行查询,返回响应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析响应show(response);}

3.multiMatchQuery():多字段查询

	@Testvoid testMultiMatch() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotels");// 2.准备DSLrequest.source().query(QueryBuilders.multiMatchQuery("如家","name","business"));//3.执行查询,返回响应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析响应show(response);}

4.termQuery():词条精确值查询

@Testvoid testTermQuery() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotels");// 2.准备DSLrequest.source().query(QueryBuilders.termQuery("city","上海"));//3.执行查询,返回响应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析响应show(response);}

5.rangeQuery():范围查询

	@Testvoid testRangeQuery() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotels");// 2.准备DSLrequest.source().query(QueryBuilders.rangeQuery("pirce").gte(100).lte(200));//3.执行查询,返回响应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析响应show(response);}

6.bool复合查询

布尔查询是一个或多个查询子句的组合,子查询的组合方式有:
must:必须匹配每个子查询,类似“与”;
should:选择性匹配子查询,类似“或”;
must_not:必须不匹配,不参与算分,类似“非”;
filter:必须匹配,类似“与”,不参与算分一般搜索框用must,选择条件使用filter;

@Testvoid testBool() throws IOException {SearchRequest request = new SearchRequest("hotels");//方式1
//        BoolQueryBuilder boolQuery = new BoolQueryBuilder();
//        boolQuery.must(QueryBuilders.termQuery("city","上海"));
//        boolQuery.filter(QueryBuilders.rangeQuery("price").gte(100).lte(200));
//        request.source().query(boolQuery);//方式2request.source().query(new BoolQueryBuilder().must(QueryBuilders.termQuery("city","上海")).filter(QueryBuilders.rangeQuery("price").gte(100).lte(200)));SearchResponse response = client.search(request, RequestOptions.DEFAULT);show(response);}

7.分页查询

	 @Testvoid testPageAndSort() throws IOException {int page = 1, size = 5;String searchName = "如家";SearchRequest request = new SearchRequest("hotels");// 2.1.queryif(searchName == null){request.source().query(QueryBuilders.matchAllQuery());}else{request.source().query(QueryBuilders.matchQuery("name", searchName));}// 2.2.分页 from、sizerequest.source().from((page - 1) * size).size(size);//2.3.排序request.source().sort("price", SortOrder.DESC);SearchResponse response = client.search(request, RequestOptions.DEFAULT);show(response);}

http://www.mnyf.cn/news/43890.html

相关文章:

  • 北京智能网站建设系统加盟百度一下网页
  • 厦门公司网站开发免费发布信息平台有哪些
  • 做卖衣服网站源代码seo公司后付费
  • ps做任务挣钱的网站江西优化中心
  • 网站开发和程序开发百度浏览器网页版入口
  • 做网站关键词搜狗网址大全
  • 外贸独立站建站平台360关键词指数查询
  • 免费建立网站软件北京优化seo排名
  • 个人网站设计与实现结论百度空间登录
  • c 做网站怎么居中文案代写
  • 用凡科做的网站保存不了推广策略包括哪些内容
  • 肥城市网站建设百度搜索高级搜索技巧
  • 做专业的热转印材料门户网站广告优化师是做什么的
  • 爱妮微如何做网站链接的网址seo资讯
  • 建设部网站从何时可以查询工程师证免费的个人主页网页制作网站
  • 网站制作系统哪个好企业邮箱查询
  • 主营网站建设品牌学it需要什么学历基础
  • 某鲜花网站的数据库建设外贸接单十大网站
  • 网站设计专业公司价格企业网络
  • 如何做网站防劫持seo整站排名
  • 邢台规划局网站建设如何免费制作自己的网站
  • 网站做多个语言有什么好处域名是什么意思呢
  • 厦门做企业网站多少钱怎么自己做网站推广
  • 永康做企业网站的公司成都百度推广开户公司
  • 西宁高端网站建设公司搜索竞价托管
  • 电子商务网站cms自动引流推广app
  • 毕业设计做网站要求第三波疫情将全面大爆发
  • 深圳做h5网站公司网址域名大全2345网址
  • 毕业设计做 什么网站好深圳网站优化推广方案
  • 红色大气宽屏企业网站源码 带后台中英文双语外贸企业网站源码今日新闻最新头条10条摘抄