es6.4.2api
这是讲数据库的数据导入到萨尔瓦多里所有用到了mysql!
1.依赖
可扩展标记语言版本='1.0 '编码='UTF-8 '
项目xmlns=' http://aven。阿帕奇。org/POM/4。0 .0 ' xmlns : xsi=' http://www。w3。org/2001/XMLSchema-instance '
xsi : schema location=' http://aven。阿帕奇。org/POM/4。0 .0 http://maven.apache.org/xsd/maven-4.0.0.xsd'
模型版本4 .0 .0/模型版本
groupIdcn.thislx/groupId
artifactId启动-es/artifactId
版本0 .0 .1-快照/版本
包装罐/包装
名称-es/名称
描述Spring Boot的演示项目/描述
父母
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-parent/artifactId
version2.1.0.RELEASE/version
relativePath/!-从存储库中查找父项-
/家长
性能
项目。建造。sourceencodingutf-8/项目。建造。源编码
项目。报道。outputen coding TF-8/项目。报道。输出编码
java.version1.8/java.version
/properties
属国
属国
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-web/artifactId
/依赖性
属国
groupIdorg.springframework.boot/groupId
artifactId弹簧-启动-启动-测试/artifactId
示波器测试/示波器
/依赖性
!- ES6.4.2启动-
!-https://vnrepository。com/artifact/org。弹性搜索/弹性搜索-
属国
groupIdorg.elasticsearch/groupId
人工智能搜索/人工智能
6.4.2版/版本
/依赖性
!-https://vnrepository。com/artifact/org。弹性搜索。客户/运输-
属国
groupIdorg.elasticsearch.client/groupId
artifactIdtransport/artifactId
6.4.2版/版本
不包括的项目:如接受服务项目是由投保以前已患有的疾病或伤害引致的
排除
groupIdorg.elasticsearch/groupId
人工智能搜索/人工智能
/排除
/排除
/依赖性
!- ES6.4.2结束-
!法斯森
属国
groupIdcom.alibaba/groupId
artifactIdfa
stjson/artifactId
version1.2.39/version
/dependency
dependency
groupIdorg.apache.commons/groupId
artifactIdcommons-lang3/artifactId
version3.4/version
/dependency
!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --
dependency
groupIdorg.projectlombok/groupId
artifactIdlombok/artifactId
version1.16.20/version
/dependency
dependency
groupIdcommons-httpclient/groupId
artifactIdcommons-httpclient/artifactId
version3.1/version
/dependency
!--mybatis的依赖--
dependency
groupIdorg.mybatis.spring.boot/groupId
artifactIdmybatis-spring-boot-starter/artifactId
version2.1.0/version
/dependency
!--mysql--
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version5.1.26/version
/dependency
/dependencies
build
plugins
plugin
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-maven-plugin/artifactId
/plugin
/plugins
/build
/project
2.配置文件
# Elasticsearch
# 9200端口是用来让HTTP REST API来访问ElasticSearch,而9300端口是传输层监听的默认端口
#es地址
elasticsearch.ip=127.0.0.1
#es端口
elasticsearch.port=9300
#连接池数量
elasticsearch.pool=5
#集群名称
elasticsearch.cluster.name=elasticsearch
#server.port=8181
#mysql 连接地址与数据库名字
spring.datasource.url=jdbc:mysql:///es
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#帐号
spring.datasource.username=root
#密码
spring.datasource.password=root
#扫描mapper.xml文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#扫描实体类包
mybatis.type-aliases-package=com.xiaoteng.entity
3.ESConfig
package cn.thislx.springbootes.config;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
/**
* @Configuration用于定义配置类,可替换xml配置文件
*/
@Configuration
public class ElasticsearchConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);
/**
* elk集群地址
*/
@Value("${elasticsearch.ip}")
private String hostName;
/**
* 端口
*/
@Value("${elasticsearch.port}")
private String port;
/**
* 集群名称
*/
@Value("${elasticsearch.cluster.name}")
private String clusterName;
/**
* 连接池
*/
@Value("${elasticsearch.pool}")
private String poolSize;
/**
* Bean name default 函数名字
*
* @return
*/
@Bean(name = "transportClient")
public TransportClient transportClient() {
LOGGER.info("Elasticsearch初始化开始。。。。。");
TransportClient transportClient = null;
try {
// 配置信息
Settings esSetting = Settings.builder()
.put("cluster.name", clusterName) //集群名字
.put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
.put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
.build();
//配置信息Settings自定义
transportClient = new PreBuiltTransportClient(esSetting);
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
transportClient.addTransportAddresses(transportAddress);
} catch (Exception e) {
LOGGER.error("elasticsearch TransportClient create error!!", e);
}
LOGGER.info("Elasticsearch初始化结束。。。。。");
return transportClient;
}
}
4.查看集群信息
class test{
@Autowired
private TransportClient client;
@Test
public void test7(){
ListDiscoveryNode discoveryNodes = client.connectedNodes();
for (DiscoveryNode discoveryNode : discoveryNodes) {
System.out.println("discoveryNode = " + discoveryNode);
}
}
}
5.判断索引是否存在
@Test
public void test8(){
//判断索引是否存在
String index="goods";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
System.out.println(exists);
}
6.创建索引
@Test
public void test8(){
//判断索引是否存在
String index="goods";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
//如果不存在就创建
if (!exists){
CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(index)
.execute().actionGet();
//是否创建成功
boolean acknowledged = createIndexResponse.isAcknowledged();
}
}
7.删除索引
@Test
public void test8(){
//判断索引是否存在
String index="users";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
//如果存在就删除
if (exists){
DeleteIndexResponse deleteIndexResponse = client.admin().indices()
.prepareDelete(index).execute().actionGet();
boolean acknowledged = deleteIndexResponse.isAcknowledged();
//true 删除成功
System.out.println("acknowledged = " + acknowledged);
}
}
8.判断索引下的类型是否存在
@Test
public void test8(){
//有此索引的前提下 不然会报错
String index="goods";
//类型
String type="_doc";
//判断索引下type是否存在
boolean exists = client.admin().indices().prepareTypesExists(index).setTypes(type).
execute().actionGet().isExists();
System.out.println(exists);
}
9.往索引添加数据
@Test //index =users type=doc
public void test8(){
String index="users";
String type="doc";
//1.对象
EsModel model = new EsModel();
model.setId("2");
model.setName("小明");
model.setAge(20);
model.setDate(new Date());
//讲对象转为json
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(model);
//返回id
IndexResponse indexResponse = client.prepareIndex(index, type,
jsonObject.getString("id"))
.setSource(jsonObject).get();
//拿到id
String id1 = indexResponse.getId();
System.out.println(id1);
//2.jsonObject key:value 形式
JSONObject object=new JSONObject();
object.put("id",UUID.randomUUID().toString().replaceAll("-","").
toUpperCase());
object.put("name","小红");
object.put("age",25);
object.put("date",new Date());
IndexResponse response = client.prepareIndex(index, type,
object.getString("id")).setSource(object).get();
String id = response.getId();
System.out.println("id = " + id);
}
10.通过id获取数据 显示的字段 以及排除字段(不显示)
@Test //index =users type=doc
public void test8(){
String index="users";
String type="doc";
//通过id获取数据
GetRequestBuilder getRequestBuilder = client.prepareGet(index, type,
"0DC8ADD7F0184608BB7356441AC8B2A4");
String[] files={"name","age","id"};
//显示的字段和排除的字段 显示可数组可单个字符出
GetResponse documentFields = getRequestBuilder.setFetchSource(files,null).execute().actionGet();
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
MapString, Object source = documentFields.getSource();
System.out.println(source);
}
11.通过id更新数据
@Test //index =users type=doc
public void test8() {
String index = "users";
String type = "doc";
String id = "0DC8ADD7F0184608BB7356441AC8B2A4";
JSONObject object = new JSONObject();
object.put("id", id);
object.put("name", "小庄");
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index).type(type).id(id).doc(object);
client.update(updateRequest);
}
12.通过id删除数据
@Test //index =users type=doc
public void test8() {
String index = "users";
String type = "doc";
String id = "0DC8ADD7F0184608BB7356441AC8B2A4";
DeleteResponse deleteResponse = client.prepareDelete(index, type, id).execute().actionGet();
int status = deleteResponse.status().getStatus();
//200 successful
System.out.println("status = " + status);
}
13.分页查询 显示想显示的字段 排序 没有条件
@Test
public void test8() {
String index = "goods"; //索引
String type = "_doc"; //类型
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
//要显示的字段
String fields="brandName,price,saleNum,categoryName";
String[] split = fields.split(",");
// 需要显示的字段,逗号分隔(缺省为全部字段)
if (StringUtils.isNotEmpty(fields)) {
//第一个参数 可以数一个字符串 可以是一个数组 第二个参数是排除
searchRequestBuilder.setFetchSource(split, null);
}
//根据 price排序 倒叙
searchRequestBuilder.addSort("price", SortOrder.DESC);
// 分页应用 pageNum=(n-1)*m; pageSize=m; n为当前页,m为显示多少条
searchRequestBuilder.setFrom(1).setSize(11);
SearchResponse searchResponse = searchRequestBuilder.
execute().actionGet();
SearchHits hits = searchResponse.getHits();
//总共查多少条数据
long totalHits = hits.totalHits;
//处理了几条 展示的size 我这里是11条 对应上面11
int length = hits.getHits().length;
System.out.println("totalHits = " + totalHits);
System.out.println("length = " + length);
for (SearchHit hit : hits) {
MapString, Object sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
14.再加+++
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/146457.html