一個使用中文分詞的完整Demo
阿新 • • 發佈:2019-01-10
package elasticsearch;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
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.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class ESHandler {
private TransportClient client;
public ESHandler() {
init();
}
private void init() {
//建立TransportClient類的例項來建立和ES的連線
Settings settings = Settings.settingsBuilder() .put("cluster.name", "DMall_ES").build();
try {
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*將實體類Medicine轉換成JSON字串
* @param medicine
* @return
* @throws IOException
*/
private String buildJSONString(Medicine medicine) throws IOException {
XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
contentBuilder.startObject()
.field("id", medicine.getId())
.field("name", medicine.getName())
.field("function", medicine.getFunction())
.endObject();
return contentBuilder.string();
}
/**
* 向ES中插入資料,使用BULK的模式一次插入多條資料
* @throws IOException
*/
public void insertData() throws IOException {
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(1,"銀花感冒顆粒","功能主治:銀花感冒顆粒 ,頭痛,清熱,解表,利咽。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(2,"感冒止咳糖漿","功能主治:感冒止咳糖漿,解表清熱,止咳化痰。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(3,"感冒靈顆粒","功能主治:解熱鎮痛。頭痛 ,清熱。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(4,"感冒靈膠囊","功能主治:銀花感冒顆粒 ,頭痛,清熱,解表,利咽。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(5,"仁和感冒顆粒","功能主治:疏風清熱,宣肺止咳,解表清熱,止咳化痰。"))));
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("Insert Data Fail......");
}
}
/**
* 實現Query的搜尋功能
* @param field
* @param value
* @return
*/
public List<Medicine> search(String field, String value) {
List<Medicine> list = new ArrayList<Medicine>();
//在指定的Index(myindex)和type(medicine)上進行一個Match型別的Query查詢
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, value);
SearchResponse searchResponse = client.prepareSearch("myindex")
.setTypes("medicine")
.setQuery(matchQueryBuilder)
.execute().actionGet();
//展示查詢結果
SearchHits hits = searchResponse.getHits();
System.out.println("查詢到記錄數=" + hits.getTotalHits());
SearchHit[] hitArray = hits.getHits();
for(SearchHit hit:hitArray){
Integer id = (Integer)hit.getSource().get("id");
String name = (String) hit.getSource().get("name");
String function = (String) hit.getSource().get("function");
list.add(new Medicine(id, name, function));
}
return list;
}
}
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
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.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class ESHandler {
private TransportClient client;
public ESHandler() {
init();
}
private void init() {
//建立TransportClient類的例項來建立和ES的連線
try {
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*將實體類Medicine轉換成JSON字串
* @param medicine
* @return
* @throws IOException
*/
private String buildJSONString(Medicine medicine) throws IOException {
XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
contentBuilder.startObject()
.field("id", medicine.getId())
.field("name", medicine.getName())
.field("function", medicine.getFunction())
.endObject();
return contentBuilder.string();
}
/**
* 向ES中插入資料,使用BULK的模式一次插入多條資料
* @throws IOException
*/
public void insertData() throws IOException {
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(1,"銀花感冒顆粒","功能主治:銀花感冒顆粒 ,頭痛,清熱,解表,利咽。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(2,"感冒止咳糖漿","功能主治:感冒止咳糖漿,解表清熱,止咳化痰。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(3,"感冒靈顆粒","功能主治:解熱鎮痛。頭痛 ,清熱。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(4,"感冒靈膠囊","功能主治:銀花感冒顆粒 ,頭痛,清熱,解表,利咽。"))));
bulkRequest.add(client.prepareIndex("myindex", "medicine")
.setSource(buildJSONString(new Medicine(5,"仁和感冒顆粒","功能主治:疏風清熱,宣肺止咳,解表清熱,止咳化痰。"))));
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("Insert Data Fail......");
}
}
/**
* 實現Query的搜尋功能
* @param field
* @param value
* @return
*/
public List<Medicine> search(String field, String value) {
List<Medicine> list = new ArrayList<Medicine>();
//在指定的Index(myindex)和type(medicine)上進行一個Match型別的Query查詢
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, value);
SearchResponse searchResponse = client.prepareSearch("myindex")
.setTypes("medicine")
.setQuery(matchQueryBuilder)
.execute().actionGet();
//展示查詢結果
SearchHits hits = searchResponse.getHits();
System.out.println("查詢到記錄數=" + hits.getTotalHits());
SearchHit[] hitArray = hits.getHits();
for(SearchHit hit:hitArray){
Integer id = (Integer)hit.getSource().get("id");
String name = (String) hit.getSource().get("name");
String function = (String) hit.getSource().get("function");
list.add(new Medicine(id, name, function));
}
return list;
}
}