对于es的一些基本概念还不是很清楚的同学可以移步,es概念。
在了解相关概念后并且已经安装好es,我们就可以开始下一步的api学习了。 还未装好环境的可以参考 ElasticSearch(一)--安装与配置以及Head插件的安装
在es中,允许将类型化的JSON文档编入特定的索引并使其可搜索。并且提供了一下四种的实现方式来生成这样json格式。
es中json的生成方式:
1. 手动编写json
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
2. 通过map
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
3. 通过第三方库,如gson,jackson等,
jackson
import com.fasterxml.jackson.databind.*;
// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
// generate json
byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
Gson
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "java编程思想");
jsonObject.addProperty("publicdate", "2018-05-09");
jsonObject.addProperty("price", "100");
String json = jsonObject.toString();
4. es中提供的XContentFactory.jsonBuilder()
import static org.elasticsearch.common.xcontent.XContentFactory.*;
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject(); String json = builder.string();
XContentBuileder除了能够自己手动添加属性外,还能够将map直接转换成json。
Map<String, Object> map = new HashMap<String, Object>();
map.put("user","kimchy");
map.put("postDate",new Date());
map.put("message","trying out Elasticsearch");
XContentBuilder object = XContentFactory.jsonBuilder()
.map(map);
String json = object.string();
ES索引的API
操作es都是通过TransportClient对象来操作的,
获取TransportClient对象的api
private static String address = "192.168.17.137";
private static int port = 9300;
private static TransportClient client = null;
client = new PreBuiltTransportClient(Settings.EMPTY) // 如果是集群版 配置需要进行修改
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), port));
1. 创建索引:
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(json) // 类型可以为MAP 集合,Byte数组,json 字符串,但如果是JSON字符串的话,需要在后面加一个参数XContentType.JSON
.get();
prepareIndex有三个方法,如下
IndexRequestBuilder requestBuilder = client.prepareIndex();
IndexRequestBuilder requestBuilder = client.prepareIndex(index, type); // 指定索引名称,与索引下的类型
IndexRequestBuilder requestBuilder = client.prepareIndex(index, type, id); //指定索引名称,索引下的类型,以及id
此外,值得一提的是,setSource方法,有多个重构方法,可以使用多种不同的类型,如map,byte数组等多种,具体可以查看api。创建索引后,可以通过返回的resonse对象,获取状态信息:
System.out.println("索引名称"+response.getIndex());
System.out.println("索引类型"+response.getType());
System.out.println("结果"+response.getResult());
System.out.println("id"+response.getId());
System.out.println("状态"+response.status());
2. 修改索引:
修改索引,主要是通过id获取到索引,在对索引进行修改。
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "java非常牛");
jsonObject.addProperty("publicdate", "2018-05-09");
jsonObject.addProperty("price", "101");
UpdateResponse response = client.prepareUpdate("book", "java","1")
.setDoc(jsonObject.toString(),XContentType.JSON)
.get();
还有另外一种写法,其实与上面的实现基本一致。
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject());
client.update(updateRequest).get();
3. 获取索引,通过id获取索引,主要是通过prepareGet方法,根据id来获取。
GetResponse response = client.prepareGet("book", "java","1").get();
System.out.println(response.getSourceAsString());
关于文档的搜索,后面在做详解。
4. 删除索引
DeleteResponse response = client.prepareDelete("book", "java","1").get();
另外一种方式,根据查询删除对应的数据
BulkByScrollResponse response =
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("gender", "male"))
.source("persons")
.get();
long deleted = response.getDeleted();
以上就是关于索引的基本操作了