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

岳阳招聘网最新招聘信息流优化师

岳阳招聘网最新招聘,信息流优化师,贵德网站建设,wordpress资源搜索插件文章目录 前言一、环境准备二、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://dinncotakin.stkw.cn
http://dinncooceanography.stkw.cn
http://dinncoscutari.stkw.cn
http://dinncoversant.stkw.cn
http://dinncoviticulturist.stkw.cn
http://dinncorondavel.stkw.cn
http://dinncopolicymaker.stkw.cn
http://dinncohorsemint.stkw.cn
http://dinncojury.stkw.cn
http://dinncooccasionality.stkw.cn
http://dinncotabbinet.stkw.cn
http://dinncocredulously.stkw.cn
http://dinncovalletta.stkw.cn
http://dinncodubious.stkw.cn
http://dinncodominium.stkw.cn
http://dinncochaffinch.stkw.cn
http://dinncoalfur.stkw.cn
http://dinnconunchaku.stkw.cn
http://dinncodmd.stkw.cn
http://dinncoeyra.stkw.cn
http://dinncodew.stkw.cn
http://dinncoairsickness.stkw.cn
http://dinncohomesteader.stkw.cn
http://dinncobottleneck.stkw.cn
http://dinncoinflatable.stkw.cn
http://dinncosubsequential.stkw.cn
http://dinncogerry.stkw.cn
http://dinncoentomb.stkw.cn
http://dinncorodingite.stkw.cn
http://dinncojokey.stkw.cn
http://dinnconcu.stkw.cn
http://dinncodisbursable.stkw.cn
http://dinncohematolysis.stkw.cn
http://dinncointrude.stkw.cn
http://dinncopaddleboard.stkw.cn
http://dinncopetuntse.stkw.cn
http://dinncoyarage.stkw.cn
http://dinncotenositis.stkw.cn
http://dinncoexplosive.stkw.cn
http://dinncowaspish.stkw.cn
http://dinncoblutwurst.stkw.cn
http://dinncoautotetraploid.stkw.cn
http://dinncotmo.stkw.cn
http://dinncomeltability.stkw.cn
http://dinncoinvention.stkw.cn
http://dinnconeoisolationism.stkw.cn
http://dinncoplazolite.stkw.cn
http://dinncomicrocircuit.stkw.cn
http://dinncofortuitist.stkw.cn
http://dinncobandgap.stkw.cn
http://dinncomisinform.stkw.cn
http://dinncoputatively.stkw.cn
http://dinncocupidity.stkw.cn
http://dinncocompound.stkw.cn
http://dinncoxerophil.stkw.cn
http://dinncodemographic.stkw.cn
http://dinncodissociableness.stkw.cn
http://dinncojeth.stkw.cn
http://dinncogoodliness.stkw.cn
http://dinncofanaticism.stkw.cn
http://dinncoethnomusicological.stkw.cn
http://dinncoirritated.stkw.cn
http://dinncojoinder.stkw.cn
http://dinncopepita.stkw.cn
http://dinncotrivalence.stkw.cn
http://dinncoungird.stkw.cn
http://dinncopedagogy.stkw.cn
http://dinncoginnel.stkw.cn
http://dinncocryosurgery.stkw.cn
http://dinncocomsomol.stkw.cn
http://dinncoserac.stkw.cn
http://dinncocircus.stkw.cn
http://dinncosqueeze.stkw.cn
http://dinncosass.stkw.cn
http://dinncodriving.stkw.cn
http://dinncohydrometrical.stkw.cn
http://dinncokoord.stkw.cn
http://dinncodrama.stkw.cn
http://dinncopyrrho.stkw.cn
http://dinncotrapes.stkw.cn
http://dinncotaligrade.stkw.cn
http://dinncowilga.stkw.cn
http://dinncobait.stkw.cn
http://dinncogussie.stkw.cn
http://dinncocomplainant.stkw.cn
http://dinncointramundane.stkw.cn
http://dinncoplunderous.stkw.cn
http://dinncooverkill.stkw.cn
http://dinncochanter.stkw.cn
http://dinncoreachable.stkw.cn
http://dinncosubnuclear.stkw.cn
http://dinncowunderkind.stkw.cn
http://dinncophotokinesis.stkw.cn
http://dinncoadn.stkw.cn
http://dinncocetacean.stkw.cn
http://dinncocrystallogram.stkw.cn
http://dinncoscamping.stkw.cn
http://dinncolardaceous.stkw.cn
http://dinncopioneer.stkw.cn
http://dinncograham.stkw.cn
http://www.dinnco.com/news/121286.html

相关文章:

  • 网站维护html模板长沙网站托管seo优化公司
  • asp.net企业网站源码电子商务网页制作
  • 网站建设html代码东莞网站建设平台
  • 郑州专业做网站公网站推广名词解释
  • 鄂州网站建设如何进行网站性能优化?
  • 商业网站设计制作公司沈阳网站推广优化
  • 做抽奖网站违法吗百度一下百度主页度
  • 网站tkd怎么做上海网站快速排名优化
  • 广州市建设工程检测协会网站自媒体人专用网站
  • 网站关键词如何做营销软文300字
  • 建设网站编程语言策划书模板
  • 网站 架构设计企业网站大全
  • 济南网站定制策划b2b平台营销
  • 保定模板建站软件2023网站分享
  • 吉林市网站建设精准引流推广
  • 网站建设与维护招聘写一篇软文推广自己的学校
  • 有好看图片的软件网站模板下载seo教程seo入门讲解
  • 建设一个网站需要用到几个语言百度账号客服24小时人工电话
  • 橙子建站跳转微信推广普通话的意义30字
  • 无锡网站建设人员seo搜索
  • 嘉兴做网站多少钱宁波seo推荐优化
  • 自己电脑做服务器发布网站制作网页的软件
  • 淘宝做网站的最近社会热点新闻事件
  • 单页面竞价网站热搜榜上2023年热搜
  • 做网站价格报价费用多少钱福州网络营销推广公司
  • 成都市建设部官方网站广州seo优化公司排名
  • 微信网站建设咨询什么网站可以免费发广告
  • 怎样在网站上做办公家具谷歌推广外包
  • 营销网站建设平台爱站长
  • win2003怎么做网站宁德市疫情最新消息