点击查看在Centos7中安装elasticsearch6.3
点击查看elasticsearch安装head插件
put http://192.168.1.112:9200/test2/
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"index.analysis.analyzer.default.type": "ik_max_word"
},
"mappings": {
"my": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}
}
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.0</version>
</dependency>
public class EsIndex {
private static String host="192.168.1.112";
private static int port=9300;
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
// state为1 :已经根据地址和提取码生成信息更新到数据库中了 is_index=false:尚未生成索引
String sql="select id,name,content from t_article where state=1 and is_index=false limit 0,100";
Connection con=null;
TransportClient client=null;
try{
con=dbUtil.getCon();
//创建客户端 链接使用tcp协议即9300
client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName(host),port));
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
String content = rs.getString("content");
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",id);
map.put("name",name);
map.put("content",content);
//通过prepareIndex增加文档 参数为Map
IndexResponse indexResponse = client.prepareIndex("test2", "my")
.setSource(map)
.get();
int status = indexResponse.status().getStatus();
if(status==201){
System.out.println("【执行成功】=="+name);
}else{
System.out.println("【执行失败】==id:"+id);
}
String updateSql="update t_article set is_index=true where id="+id;
PreparedStatement updatePstmt= con.prepareStatement(updateSql);
updatePstmt.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
client.close();
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
执行前,这几条数据的状态是 尚未生成索引,如下图:
执行以上代码后:
再看elasticsearch这边,也有了四条新的数据:
Controller:
/**
* 删除帖子信息
* @param id
* @return
* @throws Exception
*/
@RequestMapping("/delete")
public Map<String,Object> delete(Integer id)throws Exception{
articleService.delete(id);
// 对应的索引信息也要删除
articleService.deleteIndex(String.valueOf(id));
Map<String,Object> resultMap=new HashMap<String,Object>();
resultMap.put("success",true);
return resultMap;
}
/**
* 多选删除
* @param ids
* @return
* @throws Exception
*/
@RequestMapping("/deleteSelected")
public Map<String,Object> deleteSelected(String ids)throws Exception{
String[] idsStr = ids.split(",");
for(int i=0;i<idsStr.length;i++){
String articleId=idsStr[i];
articleService.delete(Integer.parseInt(articleId));
articleService.deleteIndex(String.valueOf(articleId));
}
Map<String,Object> resultMap=new HashMap<String,Object>();
resultMap.put("success",true);
return resultMap;
}
ServiceImpl:
@Override
public void deleteIndex(String id) {
QueryBuilder queryBuilder=QueryBuilders.termQuery("id",id); //注意 这里的id不是_id,而是我们自己的id(业务上的)
DeleteQuery deleteQuery=new DeleteQuery(); //
deleteQuery.setIndex("test2");
deleteQuery.setType("my");
deleteQuery.setQuery(queryBuilder);
elasticsearchTemplate.delete(deleteQuery);
}
下一篇:SpringBoot集成Es实现分词检索