作者:df0134330 | 来源:互联网 | 2023-09-04 16:29
导入所需的jar
!-- ElasticSearch begin --
dependency
groupId com.xinyi.search /groupId
artifactId elasticsearch /artifactId
version 2.4.6 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId lucene-core /artifactId
version 5.5.4 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId lucene-analyzers-common /artifactId
version 5.5.4 /version
/dependency
dependency
groupId joda-time /groupId
artifactId joda-time /artifactId
version 2.9.4 /version
/dependency
dependency
groupId com.google.guava /groupId
artifactId guava /artifactId
version 18.0 /version
/dependency
dependency
groupId com.twitter /groupId
artifactId jsr166e /artifactId
version 1.1.0 /version
/dependency
dependency
groupId com.tdunning /groupId
artifactId t-digest /artifactId
version 3.0 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId netty /artifactId
version 3.10.6.Final /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId hppc /artifactId
version 0.7.1 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId jackson-core /artifactId
version 2.8.1 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId jackson-dataformat-smile /artifactId
version 2.8.1 /version
/dependency
dependency
groupId com.xinyi.search /groupId
artifactId compress-lzf /artifactId
version 1.0.2 /version
/dependency
2 java工具类
package com.xinyi.es.service.impl;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author: liang.he
* @Desc: ES-2.4.6相关测试
* @Date: Created in 15:58 2018/5/18
*/
@Service
public class EsServiceImpl {
@Value("${application.es.clusterName}")
private String clusterName;
@Value("${application.es.ip}")
private String esIp;
@Value("${application.es.port}")
private Integer esPort;
private TransportClient client;
@PostConstruct
public TransportClient getClient() {
if(client!=null){
return client;
}
Settings settings = Settings.settingsBuilder()
.put("cluster.name", clusterName).build();
try {
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esIp), esPort));
} catch (Exception e) {
System.out.println("clusterName or ip or port is Error!");
e.printStackTrace();
}
return client;
}
public List Map String, Object query(String indexName,String typeName,String filedName,String value,Integer pageIndex,Integer pageSize) {
QueryBuilder qb = QueryBuilders.matchPhraseQuery(filedName, value);
SearchResponse respOnse= client.prepareSearch(indexName)
.setTypes(typeName).setQuery(qb).setFrom(pageIndex).setSize(pageSize).execute().actionGet();
List Map String, Object result= new ArrayList Map String,Object ();
for (SearchHit hit : response.getHits().getHits()) {
result.add(hit.getSource());
}
return result;
}
public int insertEs(String indexName,String typeName,Map String,Object source) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex(indexName,typeName).setSource(source));
BulkResponse respOnse= bulkRequest.get();
if(response.hasFailures()){
System.out.println("数据插入失败"+source);
return 0;
}
return 1;
}
public int deleteById(String indexName,String typeName,String id){
try {
DeleteRequestBuilder deleteRequest = client.prepareDelete(indexName,typeName,id);
DeleteResponse respOnse= deleteRequest.execute().actionGet();
response.getId();
}catch (Exception e){
e.printStackTrace();
return 0;
}
return 1;
}
public void close(){
if (client!=null){
client.close();
}
}
}