1.下载elasticsearch-1.2.1的zip包,解压之后 双击bin目录中的 elasticsearch.bat(针对windows系统) 启动服务器(默认监听9200端口)

访问 http://localhost:9200 出现下边的json数据则说明启动成功

elasticsearch-1.2.1客户端连接DEMO-LMLPHP

2.客户端连接的代码示例:

package com.jiaoyiping.othersimple;

/**
* Created with IntelliJ IDEA.
* User: 焦一平
* Date: 14-7-8
* Time: 下午11:57
* To change this template use File | Settings | File Templates.
*/
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.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ElasticSearchHandler { Logger logger = LoggerFactory.getLogger(ElasticSearchHandler.class);
private Client client; public ElasticSearchHandler(){
//使用本机做为节点
this("127.0.0.1");
} public ElasticSearchHandler(String ipAddress){
client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, 9300));
} /**
* @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));
}
}
client.close();//关闭连接(如果在这里不关闭的话会报:远程主机强制关闭了一个连接)
return list;
} public static void main(String[] args) {
ElasticSearchHandler esHandler = new ElasticSearchHandler();
List<String> jsondata = DataFactory.getInitJsonData();
String indexname = "indexdemo";
String type = "typedemo";
//添加到索引
// esHandler.createIndexResponse(indexname, type, jsondata);
//查询条件
QueryBuilder queryBuilder = QueryBuilders.queryString("name:姓名") ;
List<Medicine> result = esHandler.searcher(queryBuilder, indexname, type);
for(int i=0; i<result.size(); i++){
Medicine medicine = result.get(i);
System.out.println("(" + medicine.getId() + ")名称:" +medicine.getName() + "\t\t" + medicine.getFunction());
}
}
}
05-11 14:01