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

需要手机注册网站谷歌搜索引擎下载

需要手机注册网站,谷歌搜索引擎下载,搜索引擎推广方法,常德网站网站建设前言 简介 在 Elasticsearch7.15版本之后,Elasticsearch官方将它的高级客户端 RestHighLevelClient标记为弃用状态。同时推出了全新的 Java API客户端 Elasticsearch Java API Client,该客户端也将在 Elasticsearch8.0及以后版本中成为官方推荐使用的客…

前言

简介

在 Elasticsearch7.15版本之后,Elasticsearch官方将它的高级客户端 RestHighLevelClient标记为弃用状态。同时推出了全新的 Java API客户端 Elasticsearch Java API Client,该客户端也将在 Elasticsearch8.0及以后版本中成为官方推荐使用的客户端。

Elasticsearch Java API Client 支持除 Vector tile search API 和 Find structure API 之外的所有 Elasticsearch API。且支持所有API数据类型,并且不再有原始JsonValue属性。它是针对Elasticsearch8.0及之后版本的客户端,所以我们需要学习新的Elasticsearch Java API Client的使用方法。

为什么要抛弃High Level Rest:

  • 客户端"too heavy",相关依赖超过 30 MB,且很多都是非必要相关的;api 暴露了很多服务器内部接口

  • 一致性差,仍需要大量的维护工作。

  • 客户端没有集成 json/object 类型映射,仍需要自己借助字节缓存区实现。

Java API Client最明显的特征:

  • 支持lambda表达式操作ES
  • 支持Builder建造者模式操作ES,链式代码具有较强可读性.
  • 应用程序类能够自动映射为Mapping.
  • 所有Elasticsearch API的强类型请求和响应。
  • 所有API的阻塞和异步版本
  • 将协议处理委托给http客户端(如Java低级REST客户端),该客户端负责处理所有传输级问题:HTTP连接池、重试、节点发现等。

官方地址

https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/indexing.html

简单使用

1:导包

这里记住你的elasticsearch-java必须对应你电脑上装的ES版本

	<dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>7.17.6</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version></dependency><dependency><groupId>jakarta.json</groupId><artifactId>jakarta.json-api</artifactId><version>2.0.1</version></dependency>

2:开启链接

        //创建一个低级的客户端final RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();//创建JSON对象映射器final RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());//创建API客户端final ElasticsearchClient client = new ElasticsearchClient(transport);

3:关闭链接

        client.shutdown();transport.close();restClient.close();

4:完整代码

public class Client {public static void main(String[] args) throws IOException {//创建一个低级的客户端final RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();//创建JSON对象映射器final RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());//创建API客户端final ElasticsearchClient client = new ElasticsearchClient(transport);//查询所有索引-------------------------------------------------------------------------------------final GetIndexResponse response = client.indices().get(query -> query.index("_all"));final IndexState products = response.result().get("products");System.out.println(products.toString());//关闭client.shutdown();transport.close();restClient.close();}
}

JsonData类

原始JSON值。可以使用JsonpMapper将其转换为JSON节点树或任意对象。 此类型在API类型中用于没有静态定义类型或无法表示为封闭数据结构的泛型参数的值。 API客户端返回的此类实例保留对客户端的JsonpMapper的引用,并且可以使用to(class)转换为任意类型,而不需要显式映射器

我们一般在ES的DSL范围查询中会使用到!
核心方法:

  • to:将此对象转换为目标类。必须在创建时提供映射器
  • from:从读取器创建原始JSON值
  • of:从现有对象创建原始JSON值,以及用于进一步转换的映射器
  • deserialize:使用反序列化程序转换此对象。必须在创建时提供映射器

高阶使用

1:ES配置类

// 配置的前缀
@ConfigurationProperties(prefix = "elasticsearch") 
@Configuration
public class ESClientConfig {/*** 多个IP逗号隔开*/@Setterprivate String hosts;/*** 同步方式* * @return*/@Beanpublic ElasticsearchClient elasticsearchClient() {HttpHost[] httpHosts = toHttpHost();// Create the RestClient //RestClient restClient = RestClient.builder(httpHosts).build();RestClient restClient = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder->httpClientBuilder.setDefaultHeaders(listOf(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()))).addInterceptorLast((HttpResponseInterceptor) (response, context)-> response.addHeader("X-Elastic-Product", "Elasticsearch"))).build();// Create the transport with a Jackson mapperRestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// create the API clientreturn new ElasticsearchClient(transport);}/*** 异步方式* * @return*/@Beanpublic ElasticsearchAsyncClient elasticsearchAsyncClient() {HttpHost[] httpHosts = toHttpHost();RestClient restClient = RestClient.builder(httpHosts).build();RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());return new ElasticsearchAsyncClient(transport);}/*** 解析配置的字符串hosts,转为HttpHost对象数组** @return*/private HttpHost[] toHttpHost() {if (!StringUtils.hasLength(hosts)) {throw new RuntimeException("invalid elasticsearch configuration. elasticsearch.hosts不能为空!");}// 多个IP逗号隔开String[] hostArray = hosts.split(",");HttpHost[] httpHosts = new HttpHost[hostArray.length];HttpHost httpHost;for (int i = 0; i < hostArray.length; i++) {String[] strings = hostArray[i].split(":");httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "http");httpHosts[i] = httpHost;}return httpHosts;}}

2:查询所有索引

//省略连接...final GetIndexResponse all = client.indices().get(query -> query.index("_all"));System.out.println(all.toString());
//省略关闭...

3:查询某个索引

        //查询某个索引final GetIndexResponse products = client.indices().get(query -> query.index("products"));System.err.println(products.toString());

4:创建索引

		//查询某个索引是否存在boolean exists = client.indices().exists(query -> query.index("products")).value();System.out.println(exists);if (exists) {System.err.println("索引已存在");} else {final CreateIndexResponse products = client.indices().create(builder -> builder.index("products"));System.err.println(products.acknowledged());}

5:删除指定索引

        //删除指定索引boolean exists = client.indices().exists(query -> query.index("products")).value();System.out.println(exists);if (exists) {DeleteIndexResponse response = client.indices().delete(query -> query.index("products"));System.err.println(response.acknowledged());} else {System.err.println("索引不存在");}

6:查询索引的映射

        //查询映射信息final GetIndexResponse response = client.indices().get(builder -> builder.index("produces"));System.err.println(response.result().get("produces").mappings());

7:创建索引指定映射

numberOfReplicas(“1”):设置副本
numberOfShards(“1”):设置分片

        //创建索引指定映射,分片和副本信息final CreateIndexResponse response = client.indices().create(builder ->builder.settings(indexSetting -> indexSetting.numberOfReplicas("1").numberOfShards("1")).mappings(map -> map.properties("name", propertyBuilder -> propertyBuilder.keyword(keywordProperty -> keywordProperty)).properties("price", propertyBuilder -> propertyBuilder.double_(doubleNumProperty -> doubleNumProperty)).properties("des", propertyBuilder -> propertyBuilder.text(textProperty -> textProperty.analyzer("ik_smart").searchAnalyzer("ik_smart")))).index("produces"));

8:创建文档

使用HashMap作为数据存储容器

        //创建文档//1.创建HashMap进行存储数据,文档要对应映射final HashMap<String, Object> doc = new HashMap<>();doc.put("name","辣条");doc.put("age",12);doc.put("id","11111");//2.将文档存入索引中final IndexResponse response = client.index(builder -> builder.index("produces").id(doc.get("id")).document(doc));System.err.println(response.version());

使用自定义类作为数据存储容器

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Produce {private String id;private String name;private double age;
}
        //创建文档final Produce produce = new Produce("123", "小明", 18);final IndexResponse response = client.index(builder -> builder.index("produces").id(produce.getId()).document(produce));System.err.println(response.version());

使用外部JSON数据创建

这里要注意我们需要使用StringReader进行读取时使用replace函数将设置的’改为",当然这在真实的业务中肯定不会有,因为真实业务中一定是标准的JSON数据,无需使用replace进行替换了

        //创建文档final StringReader input = new StringReader("{'name':'农夫三拳','price':3.00,'des':'农夫三拳有点甜'}".replace('\'', '"'));final IndexResponse response = client.index(builder -> builder.index("produces").id("44514").withJson(input));System.err.println(response.version());

9: 查询所有文档

 final SearchResponse<Object> response = client.search(builder -> builder.index("produces"), Object.class);final List<Hit<Object>> hits = response.hits().hits();hits.forEach(x-> System.err.println(x));

10:根据ID查询文档

使用HashMap对应查询

        //查询文档final GetResponse<Map> response = client.get(builder -> builder.index("produces").id("116677"), Map.class);final Map source = response.source();source.forEach((x,y)->{System.err.println(x+":"+y);});

使用自定义类对应查询

        final GetResponse<Produce> response1 = client.get(builder -> builder.index("produces").id("aabbcc123"), Produce.class);final Produce source1 = response1.source();System.err.println(source1.toString());

11:删除文档

        final GetResponse<Produce> response1 = client.get(builder -> builder.index("produces").id("aabbcc123"), Produce.class);final Produce source1 = response1.source();System.err.println(source1.toString());

12:修改文档

全覆盖

        //修改文档(覆盖)final Produce produce = new Produce("ccaabb123", "旺仔摇滚洞", "旺仔摇滚洞乱摇乱滚", 10.23D);final UpdateResponse<Produce> response = client.update(builder -> builder.index("produces").id("aabbcc123").doc(produce), Produce.class);System.err.println(response.shards().successful());

修改部分文档

区别在于我们需要设置.docAsUpsert(true)表明是修改部分而不是覆盖

        //修改文档(部分修改)
//        final Produce produce = new Produce("ccaabb123", "旺仔摇滚洞", "旺仔摇滚洞乱摇乱滚", 10.23D);final Produce produce = new Produce();produce.setName("旺仔摇不动");final UpdateResponse<Produce> response = client.update(builder -> builder.index("produces").id("aabbcc123").doc(produce).docAsUpsert(true), Produce.class);System.err.println(response.shards().successful());

13:批量操作

批量新增

		produceList.add(produce1);produceList.add(produce2);produceList.add(produce3);//构建BulkRequestfinal BulkRequest.Builder br = new BulkRequest.Builder();for (Produce produce : produceList) {br.operations(op->op.index(idx->idx.index("produces").id(produce.getSku()).document(produce)));}final BulkResponse response = client.bulk(br.build());

批量删除

List<BulkOperation> bulkOperations = new ArrayList<>();// 向集合中添加需要删除的文档id信息for (int i = 0; i < dto.getIds().size(); i++) {int finalI = i;bulkOperations.add(BulkOperation.of(b -> b.delete((d -> d.index(dto.getIndex()).id(dto.getIds().get(finalI))))));}// 调用客户端的bulk方法,并获取批量操作响应结果BulkResponse response = client.bulk(e -> e.index(dto.getIndex()).operations(bulkOperations));

DSL查询

1:matchAll查询所有文档

        //matchAllfinal SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.matchAll(v->v)), Produce.class);System.err.println(response.hits().hits());

2:match 根据字段查询

        //简单query方式查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.match(t ->t.field("name").query("龙虎万精油"))), Produce.class);System.err.println(response.hits().hits());

3:多id查询

        //多ID查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.ids(sid->sid.values("1000","1001"))), Produce.class);System.err.println(response.hits().hits());

4:term 不分词查询

        //term不分词条件查询final SearchResponse<Produce> response = client.search(builder -> builder.index("produces").query(q -> q.term(t -> t.field("name").value("风油精"))), Produce.class);System.err.println(response.hits().hits());

5:范围查询

        //范围查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.range(r ->r.field("price").gt(JsonData.of(5D)).lt(JsonData.of(15D)))),Produce.class);System.err.println(response.hits().hits());

6: 前缀查询

  final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.prefix(p->p.field("name").value("六"))),Produce.class);System.err.println(response.hits().hits());

7:匹配查询

//匹配查询

  final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.wildcard(w->w.field("name").value("风*"))),Produce.class);System.err.println(response.hits().hits());

?单字符匹配

        //匹配查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.wildcard(w->w.field("name").value("风?精"))),Produce.class);System.err.println(response.hits().hits());

8:模糊查询

        //模糊查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.fuzzy(f->f.field("name").value("六仙花露水"))),Produce.class);System.err.println(response.hits().hits());

9:多条件查询

使用bool关键字配合must,should,must_not

  • must:所有条件必须同时成立
  • must_not:所有条件必须同时不成立
  • should:所有条件中成立一个即可
        //多条件final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q ->q.bool(b ->b.must(t ->t.term(v ->v.field("name").value("旺仔摇不动"))).must(t2 ->t2.term(v2 ->v2.field("price").value(0.0D))))),Produce.class);System.err.println(response.hits().hits());

或者创建BoolQuery.Builder,以便进行业务判断是否增加查询条件

 List<FieldValue> fieldValues = new ArrayList<>();fieldValues.add(FieldValue.of(10));fieldValues.add(FieldValue.of(100));BoolQuery.Builder boolQuery = new BoolQuery.Builder();boolQuery.must(t->t.terms(v->v.field("label").terms(term->term.value(fieldValues))));boolQuery.must(t->t.match(f->f.field("name").query("旺仔")));SearchResponse<Object> search = elasticsearchClient.search(builder -> builder.index("my_test_index").query(q->q.bool(boolQuery.build())),Object.class);

10:多字段查询-multiMatch

        //多字段查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q->q.multiMatch(qs->qs.query("蚊虫叮咬 辣眼睛").fields("name","des"))),Produce.class);System.err.println(response.hits().hits());

11:高亮显示

我们注意要设置前缀和后缀

        //高亮显示final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q -> q.match(v -> v.field("name").query("风油精"))).highlight(h -> h.preTags("<span>").postTags("<span>").fields("name", hf -> hf)),Produce.class);System.err.println(response.toString());

12:分页查询

我们使用match_all进行全部搜索的时候使用size关键字设置每一页的大小,使用from关键字设置页码
from的计算公式:(页码-1)*size

        //分页查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q->q.matchAll(v->v)).size(2).from(0),Produce.class);System.err.println(response.hits().hits());

13:排序

使用sort关键字指定需要进行排序的字段设置排序类型即可,我们这里会使用到SortOrder枚举类来进行指定排序方式

desc:降序
asc:升序

        //排序final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q->q.matchAll(v->v)).sort(builder1 -> builder1.field(f->f.field("price").order(SortOrder.Asc))),Produce.class);System.err.println(response.hits().hits());

14:指定字段查询

使用_source关键字在数组中设置需要展示的字段
值得注意的是在source方法中需要我们写filter去指定是include包含或是exclude去除xx字段

        //指定字段查询final SearchResponse<Produce> response = client.search(builder ->builder.index("produces").query(q->q.matchAll(v->v)).source(s->s.filter(v->v.includes("price","des"))),Produce.class);System.err.println(response.hits().hits());

15:聚合查询-求最大值

SearchResponse<Object> search = elasticsearchClient.search(builder ->builder.index("my_test_index").from(0).size(1).aggregations("aa", t ->t.max(f->f.field("type"))), Object.class);EsResult esResult = EsUtils.searchAnalysis(search);Aggregate aa = esResult.getAggregations().get("aa");LongTermsAggregate lterms = aa.lterms();Buckets<LongTermsBucket> buckets = lterms.buckets();List<LongTermsBucket> array = buckets.array();

16:桶聚合查询-劣势 group by

SearchResponse<JSONObject> search = elasticsearchClient.search(builder ->builder.index(EsIndexConstants.article_info).query(t->t.range(f->f.field("create_time").gte(JsonData.of(startDate)).lte(JsonData.of(endDate)))).from(0).size(1).aggregations("countValue", t ->t.terms(f -> f.field("ata_type.keyword"))), JSONObject.class);
Aggregate countValue = search .getAggregations().get("countValue");
List<StringTermsBucket> array = countValue.sterms().buckets().array();

文章转载自:
http://wanjiasearcher.przc.cn
http://wanjiabridesmaid.przc.cn
http://wanjialankester.przc.cn
http://wanjiaencipher.przc.cn
http://wanjiaalow.przc.cn
http://wanjiadistract.przc.cn
http://wanjiainquisitive.przc.cn
http://wanjiastowage.przc.cn
http://wanjiatandour.przc.cn
http://wanjialemonwood.przc.cn
http://wanjiasunspecs.przc.cn
http://wanjiabushwalking.przc.cn
http://wanjiabiafra.przc.cn
http://wanjiabfc.przc.cn
http://wanjiadepressomotor.przc.cn
http://wanjiasublimate.przc.cn
http://wanjiainterproximal.przc.cn
http://wanjiaheulandite.przc.cn
http://wanjiaprominence.przc.cn
http://wanjiaaeon.przc.cn
http://wanjiaantidiabetic.przc.cn
http://wanjiathermoelement.przc.cn
http://wanjiakerflop.przc.cn
http://wanjiaplacid.przc.cn
http://wanjiabeery.przc.cn
http://wanjianeurular.przc.cn
http://wanjiamicroencapsulate.przc.cn
http://wanjiaglyptograph.przc.cn
http://wanjiajasey.przc.cn
http://wanjiagilolo.przc.cn
http://wanjiabandy.przc.cn
http://wanjiagramercy.przc.cn
http://wanjiainvidiousness.przc.cn
http://wanjiapowys.przc.cn
http://wanjiapukka.przc.cn
http://wanjiajocasta.przc.cn
http://wanjiaabide.przc.cn
http://wanjiaend.przc.cn
http://wanjiaswinge.przc.cn
http://wanjiahydrotaxis.przc.cn
http://wanjiafanner.przc.cn
http://wanjiadeactivate.przc.cn
http://wanjiainsulative.przc.cn
http://wanjiavocationally.przc.cn
http://wanjiademulsification.przc.cn
http://wanjiaheaddress.przc.cn
http://wanjiaamnioscopy.przc.cn
http://wanjiadifferently.przc.cn
http://wanjiaimputative.przc.cn
http://wanjiatartarian.przc.cn
http://wanjiarehash.przc.cn
http://wanjiajucar.przc.cn
http://wanjiaesperanto.przc.cn
http://wanjiafulminatory.przc.cn
http://wanjiamassagist.przc.cn
http://wanjiacleg.przc.cn
http://wanjiasclerotized.przc.cn
http://wanjiacopycat.przc.cn
http://wanjiarobotism.przc.cn
http://wanjialuffa.przc.cn
http://wanjiarumpus.przc.cn
http://wanjiaorangery.przc.cn
http://wanjiamzee.przc.cn
http://wanjiaindecorous.przc.cn
http://wanjiaunsaid.przc.cn
http://wanjianorthwesternmost.przc.cn
http://wanjiatransitivize.przc.cn
http://wanjiaunbarbered.przc.cn
http://wanjiaichnography.przc.cn
http://wanjiacirculator.przc.cn
http://wanjiacheeseburger.przc.cn
http://wanjiabicornuate.przc.cn
http://wanjiacubhunting.przc.cn
http://wanjiapye.przc.cn
http://wanjiaeyeshot.przc.cn
http://wanjiaexarticulation.przc.cn
http://wanjiahackberry.przc.cn
http://wanjiawoad.przc.cn
http://wanjiacrafty.przc.cn
http://wanjialoxodromics.przc.cn
http://www.15wanjia.com/news/106690.html

相关文章:

  • 贵阳专业网站建设公司域名注册腾讯云
  • 巩义做网站xd seo代写1000字多少钱
  • 做众筹网站seo 优化公司
  • 安徽和县住房城乡建设局网站网络营销方式包括哪些
  • php响应式网站模板合肥网站推广公司哪家好
  • 公司手机网站建设价格搜索引擎营销的实现方法有
  • 中山币做网站公司seo一键优化
  • 做网站怎么维护电商关键词查询工具
  • 效果好的徐州网站建设深圳网站建设三把火科技
  • 个人网站更换域名广告宣传方式有哪些
  • 网站怎么做本地映射新闻源软文推广平台
  • 网站首页轮播图怎么做济南网络推广公司
  • linux网站架构外贸谷歌推广
  • 荥阳网站建设百度seo排名公司
  • 专门做稀有产品的网站查收录
  • 中小企业网站建设短视频新媒体推广
  • 做网站需要哪些栏目网络营销好学吗
  • 淘宝做网站可靠吗百度搜索排行
  • 建模网站友链外链app
  • 谁能给个网址免费的网站推广优化
  • 公司网站建设设计广州做seo的公司
  • 哪个网站可以专门做产品推广网络推广是什么专业
  • 做网站后怎样让其他人交互百度如何收录网站
  • 家具网站开发目的深圳互联网营销
  • cms网站管理系统源码搜索引擎最佳化
  • 建设网站最强专业黑帽seo
  • 个人站长做网站需要多少钱比百度好用的搜索引擎
  • 网站怎么做推广百度推广电话销售好做吗
  • wap网站建设兴田德润实惠如何创建一个网页
  • 网站开发后端做什么百度人工服务24小时电话