本文包含的内容
1.安装elasticsearch2.3.3
2.配置ik中文分词器
3.使用java api 对document进行CRUD
curl -L -O https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.3/elasticsearch-2.3.3.tar.gz
解压
tar -xvf elasticsearch-2.3.3.tar.gz
进入bin目录
cd elasticsearch-2.3.3/bin
启动
./elasticsearch
curl 'localhost:9200/_cat/health?v'
./elasticsearch
junit
junit
4.11
test
org.elasticsearch
elasticsearch
2.3.3
com.fasterxml.jackson.core
jackson-databind
2.6.6
package cn.lzg.esdemo;
public class Goods {
private Long id;
private String name;
private String[] regionIds;
public Goods() {
super();
}
public Goods(Long id, String name, String[] regionIds) {
super();
this.id = id;
this.name = name;
this.regionIds = regionIds;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getRegionIds() {
return regionIds;
}
public void setRegionIds(String[] regionIds) {
this.regionIds = regionIds;
}
@Override
public String toString() {
return id+" : " + name + " : "+regionIds;
}
}
package cn.lzg.esdemo;
/**
* 用于es查询的dto
*
* @author lzg
* @date 2016年6月12日
*/
public class GoodsFilter2ES {
private String regionId; // 园区UUID
private String queryStr; // 条件
public String getRegionId() {
return regionId;
}
public void setRegionId(String regionId) {
this.regionId = regionId;
}
public String getQueryStr() {
return queryStr;
}
public void setQueryStr(String queryStr) {
this.queryStr = queryStr;
}
}
package cn.lzg.esdemo;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
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.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* elasticsearch 相关操作工具类
*
* @author lzg
* @date 2016年6月12日
*/
public class ESUtils {
/**
* es服务器的host
*/
private static final String host = "192.168.1.88";
/**
* es服务器暴露给client的port
*/
private static final int port = 9300;
/**
* jackson用于序列化操作的mapper
*/
private static final ObjectMapper mapper = new ObjectMapper();
/**
* 获得连接
*
* @return
* @throws UnknownHostException
*/
private static Client getClient() throws UnknownHostException {
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
return client;
}
/**
* 创建商品索引
*
* @param goodsList
* 商品dto的列表
* @throws UnknownHostException
* @throws JsonProcessingException
*/
public static void createIndex(List goodsList) throws UnknownHostException, JsonProcessingException {
Client client = getClient();
// 如果存在就先删除索引
if (client.admin().indices().prepareExists("test_index").get().isExists()) {
client.admin().indices().prepareDelete("test_index").get();
}
// 创建索引,并设置mapping.
String mappingStr = "{ \"goods\" : { \"properties\": { \"id\": { \"type\": \"long\" }, \"name\": {\"type\": \"string\", \"analyzer\": \"ik_max_word\"}, \"regionIds\": {\"type\": \"string\",\"index\": \"not_analyzed\"}}}}";
client.admin().indices().prepareCreate("test_index").addMapping("goods", mappingStr).get();
// 批量处理request
BulkRequestBuilder bulkRequest = client.prepareBulk();
byte[] json;
for (Goods goods : goodsList) {
json = mapper.writeValueAsBytes(goods);
bulkRequest.add(new IndexRequest("test_index", "goods", goods.getId() + "").source(json));
}
// 执行批量处理request
BulkResponse bulkResponse = bulkRequest.get();
// 处理错误信息
if (bulkResponse.hasFailures()) {
System.out.println("====================批量创建索引过程中出现错误 下面是错误信息==========================");
long count = 0L;
for (BulkItemResponse bulkItemResponse : bulkResponse) {
System.out.println("发生错误的 索引id为 : "+bulkItemResponse.getId()+" ,错误信息为:"+ bulkItemResponse.getFailureMessage());
count++;
}
System.out.println("====================批量创建索引过程中出现错误 上面是错误信息 共有: "+count+" 条记录==========================");
}
client.close();
}
/**
* 查询商品
*
* @param filter
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static List search(GoodsFilter2ES filter)
throws JsonParseException, JsonMappingException, IOException {
Client client = getClient();
QueryBuilder qb = new BoolQueryBuilder()
.must(QueryBuilders.matchQuery("name",filter.getQueryStr()))
.must(QueryBuilders.termQuery("regionIds", filter.getRegionId()));
SearchResponse response = client.prepareSearch("test_index").setTypes("goods").setQuery(qb).execute()
.actionGet();
SearchHit[] hits = response.getHits().getHits();
List goodsIds = new ArrayList<>();
for (SearchHit hit : hits) {
Goods goods = mapper.readValue(hit.getSourceAsString(), Goods.class);
goodsIds.add(goods);
}
client.close();
return goodsIds;
}
/**
* 新增document
*
* @param index
* 索引名称
* @param type
* 类型名称
* @param goods
* 商品dto
* @throws UnknownHostException
* @throws JsonProcessingException
*/
public static void addDocument(String index, String type, Goods goods)
throws UnknownHostException, JsonProcessingException {
Client client = getClient();
byte[] json = mapper.writeValueAsBytes(goods);
client.prepareIndex(index, type, goods.getId() + "").setSource(json).get();
client.close();
}
/**
* 删除document
*
* @param index
* 索引名称
* @param type
* 类型名称
* @param goodsId
* 要删除的商品id
* @throws UnknownHostException
*/
public static void deleteDocument(String index, String type, Long goodsId) throws UnknownHostException {
Client client = getClient();
client.prepareDelete(index, type, goodsId+"").get();
client.close();
}
/**
* 更新document
*
* @param index
* 索引名称
* @param type
* 类型名称
* @param goods
* 商品dto
* @throws JsonProcessingException
* @throws UnknownHostException
*/
public static void updateDocument(String index, String type, Goods goods)
throws UnknownHostException, JsonProcessingException {
//如果新增的时候index存在,就是更新操作
addDocument(index, type, goods);
}
}
@Test
public void testCreatIndex() throws UnknownHostException, JsonProcessingException{
List goodsList = new ArrayList<>();
String[] r123 = {"r1","r2","r3"};
String[] r23 = {"r2","r3"};
goodsList.add(new Goods(1L, "雀巢咖啡", r123));
goodsList.add(new Goods(2L, "雀巢咖啡", r23));
goodsList.add(new Goods(3L, "星巴克咖啡", r123));
goodsList.add(new Goods(4L, "可口可乐", r123));
ESUtils.createIndex(goodsList);
}
上面的方法是 添加4个数据,然后生成索引
@Test
public void testSearch() throws JsonParseException, JsonMappingException, IOException{
GoodsFilter2ES filter = new GoodsFilter2ES();
filter.setQueryStr("咖啡");
filter.setRegionId("r2");
List result = ESUtils.search(filter);
for (Goods goods : result) {
System.out.println(goods);
}
}
@Test
public void testAddDoc() throws UnknownHostException, JsonProcessingException{
//test_index 和 goods 在创建索引的时候写死了 所以这样 就传这两个值
String[] r = {"r2","r3"};
Goods goods = new Goods(5L, "新增的咖啡", r);
ESUtils.addDocument("test_index", "goods", goods);
}
junit执行成功后,在执行上面的查询结果如下
package cn.lzg.esdemo;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
public class MyTest {
/**
* 生成索引
* @throws UnknownHostException
* @throws JsonProcessingException
*/
@Test
public void testCreatIndex() throws UnknownHostException, JsonProcessingException{
List goodsList = new ArrayList<>();
String[] r123 = {"r1","r2","r3"};
String[] r23 = {"r2","r3"};
goodsList.add(new Goods(1L, "雀巢咖啡", r123));
goodsList.add(new Goods(2L, "雅哈咖啡", r23));
goodsList.add(new Goods(3L, "星巴克咖啡", r123));
goodsList.add(new Goods(4L, "可口可乐", r123));
ESUtils.createIndex(goodsList);
}
/**
* 测试search
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
@Test
public void testSearch() throws JsonParseException, JsonMappingException, IOException{
GoodsFilter2ES filter = new GoodsFilter2ES();
filter.setQueryStr("咖啡");
filter.setRegionId("r2");
List result = ESUtils.search(filter);
for (Goods goods : result) {
System.out.println(goods);
}
}
/**
* 测试新增doc
* @throws UnknownHostException
* @throws JsonProcessingException
*/
@Test
public void testAddDoc() throws UnknownHostException, JsonProcessingException{
//test_index 和 goods 在创建索引的时候写死了 所以这样 就传这两个值
String[] r = {"r2","r3"};
Goods goods = new Goods(5L, "新增的咖啡", r);
ESUtils.addDocument("test_index", "goods", goods);
}
/**
* 测试修改doc
* @throws UnknownHostException
* @throws JsonProcessingException
*/
@Test
public void testUpdateDoc() throws UnknownHostException, JsonProcessingException{
String[] r = {"r2","r3"};
Goods goods = new Goods(5L, "修改啦的咖啡", r);
ESUtils.updateDocument("test_index", "goods", goods);
}
/**
* 测试删除doc
* @throws UnknownHostException
* @throws JsonProcessingException
*/
@Test
public void testDelDoc() throws UnknownHostException, JsonProcessingException{
ESUtils.deleteDocument("test_index", "goods", 5L);
}
}