1.添加pom依赖
org.elasticsearch
elasticsearch
org.elasticsearch.client
transport
org.elasticsearch
elasticsearch
2.添加配置
配置文件里的配置:
# es setting
cosmo.es.server-ip: 11.11.11.11
cosmo.es.server-port: 9309
cosmo.es.auto-sniff: true
cosmo.es.cluster-name: elastic-x
#es page size
cosmo.esquery.size: 10000
配置类对应配置文件的属性
@Configuration
@ConfigurationProperties(prefix = "cosmo.es")
public class EsProperties {
/**
* Es Server Ip
*/
private String serverIp;
/**
* Es Server Port
*/
private int serverPort;
/**
* 是否自动嗅探
*/
private boolean autoSniff;
/**
* 集群名称
*/
private String clusterName;
/**
* @return the serverIp
*/
public String getServerIp() {
return serverIp;
}
/**
* @param serverIp the serverIp to set
*/
public void setServerIp(String serverIp) {
this.serverIp = serverIp;
}
/**
* @return the serverPort
*/
public int getServerPort() {
return serverPort;
}
/**
* @param serverPort the serverPort to set
*/
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
/**
* @return the autoSniff
*/
public boolean isAutoSniff() {
return autoSniff;
}
/**
* @param autoSniff the autoSniff to set
*/
public void setAutoSniff(boolean autoSniff) {
this.autoSniff = autoSniff;
}
/**
* @return the clusterName
*/
public String getClusterName() {
return clusterName;
}
/**
* @param clusterName the clusterName to set
*/
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
}
es配置
@Configuration
public class EsConfig {
/**
* client
* @param esProperties Es 属性
* @return ES Client
* @throws UnknownHostException UnknownHostException
*/
@Bean
public TransportClient client(EsProperties esProperties) throws UnknownHostException {
// ES 服务地址
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName(esProperties.getServerIp()), esProperties.getServerPort());
// 是否自动嗅探,集群名字
Settings setttings = Settings.builder()
.put("client.transport.sniff", esProperties.isAutoSniff())
.put("cluster.name", esProperties.getClusterName()).build();
TransportClient client = new PreBuiltTransportClient(setttings);
client.addTransportAddress(node);
return client;
}
}
3.使用示例
类似于mybatis的Dao层,不过这里引入transportClient对es库做操作,没有相关的xml配置sql语句。
@Component
public class OsrTaskDao {
/**
* Logger
*/
private final Logger logger = LoggerFactory.getLogger(OsrTaskDao.class);
/**
* ES TransportClient
*/
@Autowired
private TransportClient transportClient;
/**
* 添加或更新任务
*
* @param osrTask osr任务
*/
public void addOrUpdateOsrTask(OsrTask osrTask) {
try {
String osrTaskJson = JSONObject.toJSONString(osrTask);
BulkRequestBuilder bulkRequest = transportClient.prepareBulk().setRefreshPolicy(RefreshPolicy.IMMEDIATE);
// ES_INDEX 为索引,类似mysql对应的库,ES_TYPE为数据类型,类似mysql对应的表,其他例如status、_id就类似mysql表里的属性
IndexRequestBuilder indexRequest = transportClient.prepareIndex(CommonConstant.ES_INDEX,
CommonConstant.ES_TYPE_OSRTASK, osrTask.getId()).setSource(osrTaskJson, XContentType.JSON);
bulkRequest.add(indexRequest);
bulkRequest.execute().actionGet();
} catch (Exception e) {
logger.error("es保存osr任务出现异常");
e.printStackTrace();
}
}
/**
* @description 获取osr任务
* @param id 任务id
* @return OsrTask
*/
public OsrTask getOsrTask(String id) {
OsrTask result = new OsrTask();
try {
// 根据id查询
SearchRequestBuilder searchRequest = transportClient
.prepareSearch(CommonConstant.ES_INDEX)
.setTypes(CommonConstant.ES_TYPE_OSRTASK)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(
QueryBuilders.boolQuery()
.mustNot(QueryBuilders.wildcardQuery("status", OsrTaskConstant.TASK_FILE_INVALID))
.must(QueryBuilders.termQuery("_id", StringUtils.trim(id))));
SearchResponse searchResponse = searchRequest.execute().actionGet();
SearchHit[] searchHists = searchResponse.getHits().getHits();
// 遍历es查询结果
if (searchHists != null && searchHists.length > 0) {
for (int i = 0; i < searchHists.length; i++) {
SearchHit hit = searchHists[i];
String json = hit.getSourceAsString();
OsrTask osrTask = JSONObject.parseObject(json, OsrTask.class);
if (osrTask != null) {
result = osrTask;
break;
}
}
}
} catch (Exception e) {
logger.error("es 获取osr任务出现异常");
e.printStackTrace();
}
return result;
}
/**
* 删除osr任务,这是逻辑删除
*
* @param id 文件id
* @return 是否删除
*/
public boolean deleteOsrTask(String id) {
OsrTask osrTask = getOsrTask(id);
osrTask.setStatus(OsrTaskConstant.TASK_FILE_INVALID);
addOrUpdateOsrTask(osrTask);
return true;
}
/**
* @description 根据名称获得任务列表
* @param name 名称
* @param currentPageNo 页数
* @param pageSize 页码大小
* @param userId 用户id
* @return PageVo>
*/
public PageVo> getTaskListByName(String name, int currentPageNo, int pageSize, String userId) {
SearchResponse searchResponse = null;
if (StringUtils.isBlank(name)) {
SearchRequestBuilder searchRequest = transportClient
.prepareSearch(CommonConstant.ES_INDEX)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(
QueryBuilders.boolQuery().must(QueryBuilders.termQuery("userId", userId))
.mustNot(QueryBuilders.wildcardQuery("status", OsrTaskConstant.TASK_FILE_INVALID)))
.addSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
.setFrom(pageSize * (currentPageNo - 1)).setSize(pageSize);
searchResponse = searchRequest.execute().actionGet();
} else {
SearchRequestBuilder searchRequest = transportClient
.prepareSearch(CommonConstant.ES_INDEX)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(
QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("userId", userId))
.mustNot(QueryBuilders.wildcardQuery("status", OsrTaskConstant.TASK_FILE_INVALID))
.must(QueryBuilders.wildcardQuery("name", "*"
+ StringUtils.trim(name).toLowerCase() + "*")))
.addSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
.setFrom(pageSize * (currentPageNo - 1)).setSize(pageSize);
searchResponse = searchRequest.execute().actionGet();
}
SearchHit[] searchHists = searchResponse.getHits().getHits();
List osrList = new ArrayList();
if (searchHists != null && searchHists.length > 0) {
for (int i = 0; i < searchHists.length; i++) {
SearchHit hit = searchHists[i];
String json = hit.getSourceAsString();
OsrTask osrTask = JSONObject.parseObject(json, OsrTask.class);
if (osrTask != null) {
osrList.add(osrTask);
}
}
}
PageVo> osrPageVo = new PageVo>();
osrPageVo.setCurrentPageNo(currentPageNo);
osrPageVo.setPageSize(pageSize);
osrPageVo.setRows(osrList);
osrPageVo.setTotal(searchResponse.getHits().getTotalHits());
return osrPageVo;
}
}
物理删除:
/**
* 删除文件记录
* @param id 文件id
* @return 是否删除
*/
public boolean deleteAudioFile(String id){
try {
DeleteRequestBuilder deleteRequest = transportClient.prepareDelete().setRefreshPolicy(RefreshPolicy.IMMEDIATE)
.setIndex(CommonConstant.ES_INDEX).setType(CommonConstant.ES_TYPE_AUDIOFILE).setId(id);
deleteRequest.execute().actionGet();
return true;
} catch (Exception e) {
logger.error("es 删除文件出现异常");
e.printStackTrace();
}
return false;
}
ps.关于es插件安装和es库创建
1、安装Elasticsearch-head插件
可以在服务器上安装,也可以在浏览器上安装,官网地址和说明如下:
http://mobz.github.io/elasticsearch-head/
2、访问查询页面
以浏览器head插件为例,输入Elasticsearch的http服务地址进行连接:
http://IP:9209/
3、进入复合查询标签卡
在查询区域输入内容,脚本如下,点击提交请求
输入请求路径:http://IP:9209/cosmo_osr, 选择PUT请求,在框内输入如下类似sql脚本,即可创建对应的es库表
{
"mappings": {
"notedoc": {
"properties": {
"id": {
"type": "keyword"
},
"taskId": {
"type": "keyword"
},
"content": {
"type": "text"
},
"createTime": {
"type": "long"
},
"updateTime": {
"type": "long"
}
}
},
"lattice": {
"properties": {
"lid": {
"type": "long"
},
"taskId": {
"type": "keyword"
},
"onebest": {
"type": "text"
},
"bg": {
"type": "long"
},
"ed": {
"type": "long"
},
"spk": {
"type": "long"
}
}
},
"osrtask": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"userId": {
"type": "keyword"
},
"latticeSize": {
"type": "long"
},
"createTime": {
"type": "long"
},
"updateTime": {
"type": "long"
}
}
},
"audiofile": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"enc": {
"type": "keyword"
},
"mime": {
"type": "keyword"
},
"md5": {
"type": "keyword"
},
"originalSize": {
"type": "long"
},
"size": {
"type": "long"
},
"originalUrl": {
"type": "keyword"
},
"wavUrl": {
"type": "keyword"
},
"timeLength": {
"type": "long"
},
"sampleSize": {
"type": "long"
},
"sampleRate": {
"type": "long"
},
"channels": {
"type": "long"
},
"createTime": {
"type": "long"
},
"updateTime": {
"type": "long"
}
}
}
}
}
创建成功后可在概览中看到对应的信息