简介:ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例。像Solr4一样,是基于Lucene构建的。支持时间时间索引和全文检索。官网:http://www.elasticsearch.org
它对外提供一系列基于java和http的api,用于索引、检索、修改大多数配置。下载地址 http://www.elasticsearch.org/download
elasticsearch 索引创建:
本项目环境:eclipse + JDK 7 + elasticsearch1.7.0
项目截图:
源代码:
package com.elasticsearch.entity;
/**
* 实体数据类
* @author Administrator
*
*/
public class Medicine {
private Integer id;
private String name;
private String function;
public Medicine() {
super();
// TODO Auto-generated constructor stub
}
public Medicine(Integer id, String name, String function) {
super();
this.id = id;
this.name = name;
this.function = function;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
}
package com.elasticsearch.date;
import java.util.ArrayList;
import java.util.List;
import com.elasticsearch.entity.Medicine;
import com.elasticsearch.util.JsonUtil;
/**
* 实体数据集合工厂
* @author Administrator
*
*/
public class DataFactory {
public static DataFactory dataFactory = null;
private DataFactory(){
}
public DataFactory getInstance(){
if(dataFactory == null){
dataFactory = new DataFactory();
}
return dataFactory;
}
public static List<String> getInitJsonData(){
List<String> list = new ArrayList<String>();
String data1 = JsonUtil.obj2JsonData(new Medicine(1,"银花 感冒 颗粒","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。"));
String data2 = JsonUtil.obj2JsonData(new Medicine(2,"感冒 止咳糖浆","功能主治:感冒止咳糖浆,解表清热,止咳化痰。"));
String data3 = JsonUtil.obj2JsonData(new Medicine(3,"感冒灵颗粒","功能主治:解热镇痛。头痛 ,清热。"));
String data4 = JsonUtil.obj2JsonData(new Medicine(4,"感冒 灵胶囊","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。"));
String data5 = JsonUtil.obj2JsonData(new Medicine(5,"仁和 感冒 颗粒","功能主治:疏风清热,宣肺止咳,解表清热,止咳化痰。"));
list.add(data1);
list.add(data2);
list.add(data3);
list.add(data4);
list.add(data5);
return list;
}
}
package com.elasticsearch.util;
import java.io.IOException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import com.elasticsearch.entity.Medicine;
public class JsonUtil {
/**
* 实现将实体对象转换成json对象
* @param medicine Medicine对象
* @return
*/
public static String obj2JsonData(Medicine medicine){
String jsonData = null;
try {
//使用XContentBuilder创建json数据
XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
jsonBuild.startObject()
.field("id",medicine.getId())
.field("name", medicine.getName())
.field("funciton",medicine.getFunction())
.endObject();
jsonData = jsonBuild.string();
System.out.println(jsonData);
} catch (IOException e) {
e.printStackTrace();
}
return jsonData;
}
}
package com.elasticsearch.index;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import com.elasticsearch.entity.Medicine;
/**
* ElasticSearch 搜索引擎封装
* @author Administrator
*
*/
public class ElasticSearchHandler {
private Client client;
public ElasticSearchHandler(){
//使用本机做为节点
this("127.0.0.1");
}
public ElasticSearchHandler(String ipAddress){
//集群连接超时设置
/*
Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ping_timeout", "10s").build();
client = new TransportClient(settings);
*/
client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, 9300));
}
/**
* 建立索引,索引建立好之后,会在elasticsearch-0.20.6\data\elasticsearch\nodes\0创建所以你看
* @param indexName 为索引库名,一个es集群中可以有多个索引库。 名称必须为小写
* @param indexType Type为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。
* @param jsondata json格式的数据集合
*
* @return
*/
public void createIndexResponse(String indexname, String type, List<String> jsondata){
//创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据
IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type).setRefresh(true);
for(int i=0; i<jsondata.size(); i++){
requestBuilder.setSource(jsondata.get(i)).execute().actionGet();
}
}
/**
* 创建索引
* @param client
* @param jsondata
* @return
*/
public IndexResponse createIndexResponse(String indexname, String type,String jsondata){
IndexResponse response = client.prepareIndex(indexname, type)
.setSource(jsondata)
.execute()
.actionGet();
return response;
}
/**
* 执行搜索
* @param queryBuilder
* @param indexname
* @param type
* @return
*/
public List<Medicine> searcher(QueryBuilder queryBuilder, String indexname, String type){
List<Medicine> list = new ArrayList<Medicine>();
SearchResponse searchResponse = client.prepareSearch(indexname).setTypes(type)
.setQuery(queryBuilder)
.execute()
.actionGet();
SearchHits hits = searchResponse.getHits();
System.out.println("查询到记录数=" + hits.getTotalHits());
SearchHit[] searchHists = hits.getHits();
if(searchHists.length>0){
for(SearchHit hit:searchHists){
Integer id = (Integer)hit.getSource().get("id");
String name = (String) hit.getSource().get("name");
String function = (String) hit.getSource().get("funciton");
list.add(new Medicine(id, name, function));
}
}
return list;
}
}
创建ElasticSearch 索引代码:
package com.elasticsearch.main;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import com.elasticsearch.date.DataFactory;
import com.elasticsearch.entity.Medicine;
import com.elasticsearch.index.ElasticSearchHandler;
public class ElasticsearchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ElasticSearchHandler esHandler = new ElasticSearchHandler();
List<String> jsondata = DataFactory.getInitJsonData();
// String indexname = "indexdemo";
String indexname = "eindexdemo";
// String type = "typedemo";
String type = "etypedemo";
//创建索引
esHandler.createIndexResponse(indexname, type, jsondata);
}
}
访问:http://localhost:9200/_plugin/head/,检查相关ElasticSearch 的相关索引是否创建成功?