·
·
· 原因:idea卡死或者异常关闭
· GetQuery getQuery = new GetQuery(); getQuery.setId(String.valueOf(userId)); UserLocation userLocation = this.elasticsearchTemplate.queryForObject(getQuery, UserLocation.class);
新增· IndexQuery indexQuery = new IndexQueryBuilder().withObject(userLocation).build(); //保存数据到ES中 this.elasticsearchTemplate.index(indexQuery);
修改· UpdateRequest updateRequest = new UpdateRequest(); updateRequest.doc(map); UpdateQuery updateQuery = new UpdateQueryBuilder() .withId(String.valueOf(userId)) .withClass(UserLocation.class) .withUpdateRequest(updateRequest).build(); //更新数据 this.elasticsearchTemplate.update(updateQuery);
esApi回顾 JavaAPI· 添加索引
• 1. 获取索引对象
• client.indices()
• 2. 创建添加索引的请求
• new CreateIndexRequest("itheima")
• 3. 发起创建请求
• indicesClient.create(createRequest,
• RequestOptions.DEFAULT)
· 添加索引+映射mapping
• createRequest.mapping(mapping, XContentType.JSON)
· 了解:查询、删除、判断索引
· 添加文档
• 1. 构建文档数据new HashMap()
• JSON.toJSONString(person)
• 2. new IndexRequest("itcast").id("1").source(data)
• new IndexRequest("itcast").id(p.getId()).source(data, XContentType.JSON)
• 3, client.index(request, RequestOptions.DEFAULT)
· 修改文档
• 添加文档时,如果id存在则修改,id不存在则添加
· 根据ID查询
• 1. new GetRequest("itcast", "1")
• 2. client.get(getReqeust, RequestOptions.DEFAULT)
• 3. response.getSourceAsString()
· 根据ID删除
• 1. new DeleteRequest("itcast", "1")
• 2. client.delete(deleteRequest, RequestOptions.DEFAULT)