elasticSearch(一)java連結elasticsearch
阿新 • • 發佈:2018-11-13
1.pom配置
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.1</version>
</ dependency>
2.連結程式碼
public class patternBean {
private Long flowId;
private String pattern;
private String question;
private Date createDate;
public Long getFlowId() {
return flowId;
}
public void setFlowId(Long flowId) {
this.flowId = flowId;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "patternBean{" +
"flowId=" + flowId +
", pattern='" + pattern + '\'' +
", question='" + question + '\'' +
", createDate=" + createDate +
'}';
}
}
public class esClientUtils {
public static final String indexName="patternrepos";
public static final String typeName="patterns";
private static TransportClient client = null;
static{
try {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 插入文件
* @param indexName
* @param type
* @param json
*/
public static void addDocument(String indexName,String type,JSONObject json){
client.prepareIndex(indexName,type).setSource(json, XContentType.JSON).get();
}
/**
* 查詢文件
* @param indexName
* @param indexType
* @param query
* @return
*/
public static SearchHits queryDocument(String indexName,String indexType,QueryBuilder query){
//1.get the serachEngine
SearchRequestBuilder searchBuilder=client.prepareSearch(indexName).setTypes(indexType);
//2.set query condition
//3.excute the query
SearchResponse result=searchBuilder.setQuery(query)
// .setFrom(0).setSize(20)
// .addSort("time",SortOrder.ASC)
// .setExplain(true)
.execute().actionGet();
//4.get the result
SearchHits hits=result.getHits();
return hits;
}
public static void deleteDocument(String indexName,String indexType,String id){
client.prepareDelete(indexName,indexType,id).execute().actionGet();
}
public static void main(String[] args){
//1.測試插入
patternBean pb=new patternBean();
pb.setFlowId(101L);
pb.setPattern("今天.*(天氣|溫度)");
pb.setQuestion("天氣很晴朗");
pb.setCreateDate(new Date());
//esClientUtils.addDocument(indexName,typeName,(JSONObject) JSONObject.toJSON(pb));
//2.測試查詢
MatchPhraseQueryBuilder query1= QueryBuilders.matchPhraseQuery("flowId",101L);
SearchHits result=esClientUtils.queryDocument(indexName,typeName,query1);
for(int i=0;i<result.getHits().length;i++){
Map<String,Object> itemMap=result.getHits()[i].getSourceAsMap();
String pattern=(String)itemMap.get("pattern");
String question=(String)itemMap.get("question");
System.out.println(pattern);
System.out.println(question);
}
//3.測試刪除
esClientUtils.deleteDocument(indexName,typeName,"6s437GYBthV9DQCpGEg5");
}