package com.itheima.day2;
import com.alibaba.fastjson.JSON;
import com.itheima.day2.domain.Goods;
import com.itheima.day2.mapper.GoodsMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Day2Application.class)
public class Day2ApplicationTests {
@Autowired
private RestHighLevelClient client;
@Autowired
private GoodsMapper goodsMapper;
/*
* 批量操作
* */
@Test
public void testBulk() throws IOException {
//创建bulkrequest对象,整合所有操作
BulkRequest bulkRequest = new BulkRequest();
//删除1号记录
DeleteRequest deleteRequest = new DeleteRequest();
bulkRequest.add(deleteRequest);
// 添加6号记录
Map map = new HashMap();
map.put("name", "六号");
IndexRequest indexRequest = new IndexRequest("person");
bulkRequest.add(indexRequest);
Map map2 = new HashMap();
map2.put("name", "三号");
//3. 修改3号记录 名称为 “三号”
UpdateRequest updateReqeust = new UpdateRequest("person", "3").doc(map2);
bulkRequest.add(updateReqeust);
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
RestStatus status = response.status();
System.out.println(status);
}
@Test
public void getSql() {
List<Goods> goodsList = goodsMapper.findAll();
System.out.println(goodsList.toString());
}
//导入
@Test
public void importData() throws IOException {
//1.查询所有数据,mysql
List<Goods> goodsList = goodsMapper.findAll();
//System.out.println(goodsList.size());
//2.bulk导入
BulkRequest bulkRequest = new BulkRequest();
//2.1 循环goodsList,创建IndexRequest添加数据
for (Goods goods : goodsList) {
//2.2 设置spec规格信息 Map的数据 specStr:{}
goods.setSpec(JSON.parseObject(goods.getSpecStr(), Map.class));
//将goods对象转换为json字符串
String data = JSON.toJSONString(goods);//map --> {}
IndexRequest indexRequest = new IndexRequest("goods");
indexRequest.id(goods.getId() + "").source(data, XContentType.JSON);
bulkRequest.add(indexRequest);
}
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.status());
}
//查询所有 matchAll
@Test
public void testMatchAll() throws IOException {
//2.构建查询请求对象,指定查询的索引名称
SearchRequest searchRequest = new SearchRequest("goods");
//4.创建查询条件构造器builder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//6.查询条件
QueryBuilder query = QueryBuilders.matchAllQuery();//查询所有
//5,指定查询
sourceBuilder.query(query);
//3.添加查询条件构造器
searchRequest.source(sourceBuilder);
//8.添加分页信息
sourceBuilder.from(0);
sourceBuilder.size(100);
//1.查询,获取查询结果
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//7.获取命中对象
SearchHits hits = response.getHits();
//7.1获取总记录数
long value = hits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Goods> goodsList = new ArrayList<>();
//7.2获取hits数据 数组
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1) {
//获取json字符串格式的数据
String sourceAsString = hit.getSourceAsString();
//转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
goodsList.add(goods);
}
for (Goods goods : goodsList) {
System.out.println(goods.toString());
}
}
//查询所有 term查询
@Test
public void testTerm() throws IOException {
//2.构建查询请求对象,指定查询的索引名称
SearchRequest searchRequest = new SearchRequest("goods");
//4.创建查询条件构造器builder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//6.查询条件,使用term查询!
QueryBuilder query = QueryBuilders.termQuery("title", "华为");
//5,指定查询
sourceBuilder.query(query);
//3.添加查询条件构造器
searchRequest.source(sourceBuilder);
//8.添加分页信息
sourceBuilder.from(0);
sourceBuilder.size(100);
//1.查询,获取查询结果
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//7.获取命中对象
SearchHits hits = response.getHits();
//7.1获取总记录数
long value = hits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Goods> goodsList = new ArrayList<>();
//7.2获取hits数据 数组
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1) {
//获取json字符串格式的数据
String sourceAsString = hit.getSourceAsString();
//转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
goodsList.add(goods);
}
for (Goods goods : goodsList) {
System.out.println(goods.toString());
}
}
//matchQuery分词查询
@Test
public void matchQuery() throws IOException {
//2.构建查询请求对象,指定查询的索引名称
SearchRequest searchRequest = new SearchRequest("goods");
//4.创建查询条件构造器builder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//6.查询条件,使用term查询!
MatchQueryBuilder query = QueryBuilders.matchQuery("title", "华为手机");
query.operator(Operator.AND);
//5,指定查询
sourceBuilder.query(query);
//3.添加查询条件构造器
searchRequest.source(sourceBuilder);
//8.添加分页信息
sourceBuilder.from(0);
没有合适的资源?快使用搜索试试~ 我知道了~
ElasticSearch整合JavaApi代码
共146个文件
xml:103个
java:11个
class:9个
需积分: 0 0 下载量 127 浏览量
2023-03-09
09:27:16
上传
评论
收藏 196KB ZIP 举报
温馨提示
day1为基础使用,day2为高级操作
资源推荐
资源详情
资源评论
收起资源包目录
ElasticSearch整合JavaApi代码 (146个子文件)
Day2ApplicationTests.class 17KB
Day1ApplicationTests.class 8KB
Goods.class 3KB
Person.class 2KB
ElasticSearchConfig.class 2KB
ELasticSearchConfig.class 2KB
Day2Application.class 733B
Day1Application.class 733B
GoodsMapper.class 557B
mvnw.cmd 6KB
mvnw.cmd 6KB
.gitignore 395B
.gitignore 395B
.gitignore 184B
day2.iml 11KB
day1.iml 10KB
maven-wrapper.jar 50KB
maven-wrapper.jar 50KB
Day2ApplicationTests.java 20KB
Day1ApplicationTests.java 7KB
MavenWrapperDownloader.java 5KB
MavenWrapperDownloader.java 5KB
Goods.java 2KB
ELasticSearchConfig.java 1KB
ElasticSearchConfig.java 1KB
Person.java 1KB
GoodsMapper.java 481B
Day2Application.java 317B
Day1Application.java 317B
HELP.md 429B
HELP.md 429B
mvnw 10KB
mvnw 10KB
maven-wrapper.properties 218B
maven-wrapper.properties 218B
application.properties 1B
application.properties 1B
application.properties 1B
application.properties 1B
workspace.xml 16KB
uiDesigner.xml 9KB
pom.xml 3KB
pom.xml 2KB
Project_Default.xml 2KB
compiler.xml 930B
jarRepositories.xml 880B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_8_RELEASE.xml 775B
Maven__org_elasticsearch_client_elasticsearch_rest_high_level_client_7_4_0.xml 761B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_8_RELEASE.xml 754B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_8_RELEASE.xml 740B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_autoconfigure_2_1_0.xml 736B
Maven__org_springframework_boot_spring_boot_starter_jdbc_2_1_8_RELEASE.xml 733B
Maven__org_springframework_boot_spring_boot_starter_test_2_1_8_RELEASE.xml 733B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_smile_2_9_9.xml 709B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 706B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_cbor_2_9_9.xml 702B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_yaml_2_9_9.xml 702B
Maven__org_springframework_boot_spring_boot_starter_2_1_8_RELEASE.xml 698B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_starter_2_1_0.xml 694B
Maven__org_elasticsearch_client_elasticsearch_rest_client_7_4_0.xml 684B
Maven__org_elasticsearch_plugin_aggs_matrix_stats_client_7_4_0.xml 677B
Maven__org_springframework_boot_spring_boot_test_2_1_8_RELEASE.xml 677B
Maven__org_springframework_spring_expression_5_1_9_RELEASE.xml 664B
Maven__org_elasticsearch_plugin_lang_mustache_client_7_4_0.xml 649B
Maven__org_springframework_spring_context_5_1_9_RELEASE.xml 643B
Maven__org_apache_lucene_lucene_analyzers_common_8_2_0.xml 642B
Maven__org_elasticsearch_elasticsearch_x_content_7_4_0.xml 642B
Maven__org_elasticsearch_elasticsearch_secure_sm_7_4_0.xml 642B
Maven__org_springframework_boot_spring_boot_2_1_8_RELEASE.xml 642B
Maven__org_apache_lucene_lucene_backward_codecs_8_2_0.xml 635B
Maven__org_elasticsearch_plugin_parent_join_client_7_4_0.xml 635B
Maven__org_springframework_spring_beans_5_1_9_RELEASE.xml 629B
Maven__org_apache_lucene_lucene_spatial_extras_8_2_0.xml 628B
Maven__org_springframework_spring_test_5_1_9_RELEASE.xml 622B
Maven__org_springframework_spring_core_5_1_9_RELEASE.xml 622B
Maven__org_springframework_spring_jdbc_5_1_9_RELEASE.xml 622B
Maven__org_elasticsearch_plugin_rank_eval_client_7_4_0.xml 621B
Maven__org_apache_httpcomponents_httpasyncclient_4_1_4.xml 618B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 617B
Maven__org_springframework_spring_aop_5_1_9_RELEASE.xml 615B
Maven__org_springframework_spring_jcl_5_1_9_RELEASE.xml 615B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_11_2.xml 614B
Maven__javax_activation_javax_activation_api_1_2_0.xml 611B
Maven__org_springframework_spring_tx_5_1_9_RELEASE.xml 608B
Maven__org_elasticsearch_elasticsearch_core_7_4_0.xml 607B
Maven__org_apache_lucene_lucene_queryparser_8_2_0.xml 607B
Maven__org_apache_lucene_lucene_highlighter_8_2_0.xml 607B
Maven__org_apache_httpcomponents_httpcore_nio_4_4_12.xml 604B
Maven__com_fasterxml_jackson_core_jackson_core_2_9_9.xml 601B
Maven__com_github_spullara_mustache_java_compiler_0_9_3.xml 601B
Maven__org_elasticsearch_plugin_mapper_extras_7_4_0.xml 600B
Maven__org_elasticsearch_elasticsearch_geo_7_4_0.xml 600B
Maven__org_elasticsearch_elasticsearch_cli_7_4_0.xml 600B
Maven__org_apache_lucene_lucene_spatial3d_8_2_0.xml 593B
Maven__org_apache_lucene_lucene_grouping_8_2_0.xml 586B
Maven__net_bytebuddy_byte_buddy_agent_1_9_16.xml 584B
Maven__org_apache_httpcomponents_httpclient_4_5_9.xml 583B
Maven__mysql_mysql_connector_java_8_0_17.xml 580B
Maven__org_apache_lucene_lucene_suggest_8_2_0.xml 579B
Maven__org_apache_lucene_lucene_sandbox_8_2_0.xml 579B
共 146 条
- 1
- 2
资源评论
张牧之z
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
- 基于物理的非视域成像(NLOS)算法,利用了nerf+python源码+文档说明
- yuluer知更鸟.7z(1).001
- python课程设计-基于tensorflow实现的图文生成程序,数据集flickr30k-images+源代码+文档说明+截图
- python作业-基于Flickr30k数据集实现图像文本跨模态搜索python源码+数据集+测试界面+项目说明(高分课程设计)
- 基于Qt实现医院信息管理系统c++源码+文档说明+数据库(期末大作业)
- 基于python实现的医院信息管理系统完整源码+sql数据库+详细注释(高分课程设计)
- 基于python的眼底图像视杯视盘分割项目源码+文档说明+截图演示+详细注释(高分课程设计)
- ImageBasedModellingEdu-贰壹贰叁零
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功