package com.atguigu.springmvc.service.Impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.springmvc.entities.Client;
import com.atguigu.springmvc.entities.Student;
import com.atguigu.springmvc.service.ElSerarchService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service
public class ElSerarchServiceImpl implements ElSerarchService {
@Autowired
private Client client;
@Override
public String get(String index, String type, String id) {
GetResponse getResponse=client.getClient().prepareGet(index,type,id).get();
return getResponse.getSourceAsString();
}
@Override
public String update(String name, String value, String id) {
long result = 0;
try {
XContentBuilder source=XContentFactory.jsonBuilder().startObject().field(name,value)
.endObject();
UpdateResponse updateResponse=client.getClient().prepareUpdate("test", "emp",id).setDoc(source).get();
result=updateResponse.getVersion();
} catch (IOException e) {
e.printStackTrace();
}
return Long.toString(result);
}
@Override
public String add(String index, String type, String id,Map<String,Object> map) {
IndexResponse response=client.getClient().prepareIndex(index,type).setSource(map).get();
long result=response.getVersion();
return Long.toString(result);
}
@Override
public String addBean(String index, String type, String id, Student stu) {
ObjectMapper mapper=new ObjectMapper();
IndexResponse indexResponse = null;
try {
indexResponse = client.getClient().prepareIndex(index, type,id)
.setSource(mapper.writeValueAsString(stu)).get();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return Long.toString(indexResponse.getVersion());
}
@Override
public String deleteById(String index,String type,String id) {
DeleteResponse deleteResponse=client.getClient().prepareDelete(index,type,id).get();
return deleteResponse.getVersion()+"";
}
@Override
public String deleteAll(String index) {
client.getClient().prepareDelete().setIndex(index);
return null;
}
@Override
public String DeleteeIndex(String index, String type) {
DeleteIndexResponse deleteIndex= client.getClient().admin().indices().prepareDelete(index,type).get();
return deleteIndex.toString();
}
@Override
public String bluk(String index,String type,String id) throws IOException {
//1:生成bulk
BulkRequestBuilder bulk = client.getClient().prepareBulk();
//2:新增
IndexRequest add = new IndexRequest(index, type, "10");
add.source(XContentFactory.jsonBuilder()
.startObject()
.field("name", "Henrry").field("age", 30)
.endObject());
//3:删除
DeleteRequest del = new DeleteRequest(index, type, "AV1VF_IKFfAXVe6cyga3");
//4:修改
XContentBuilder source = XContentFactory.jsonBuilder().startObject().field("name", "jack_1").endObject();
UpdateRequest update = new UpdateRequest(index, type, "10");
update.doc(source);
/* bulk.add(del);
*/
bulk.add(add);
bulk.add(update);
//5:执行批处理
BulkResponse bulkResponse = bulk.get();
if(bulkResponse.hasFailures())
{
BulkItemResponse[] items = bulkResponse.getItems();
for(BulkItemResponse item : items)
{
System.out.println(item.getFailureMessage());
}
}
else
{
System.out.println("全部执行成功!");
}
return id;
}
@Override
public List<String> search(String index, String type) {
SearchResponse searchResponse=client.getClient().prepareSearch(index).setTypes(type)
//.setQuery(QueryBuilders.matchAllQuery())
//.setQuery(QueryBuilders.matchQuery("name","lijixiang"))
.setQuery(QueryBuilders.multiMatchQuery("lijxiang","name"))
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0).setSize(10)//分页
.addSort("age",SortOrder.DESC).get();
SearchHits hits=searchResponse.getHits();
long total=hits.getTotalHits();
System.out.println(total);
SearchHit[] searchHits=hits.hits();
List<String> list=new ArrayList<String>();
for(SearchHit s:searchHits){
list.add(s.getSourceAsString());
}
return list;
}
@Override
public List<String> searchHight(String index, String type) throws Exception {
QueryBuilder matchQuery = QueryBuilders.matchQuery("name", "lijixiang");
HighlightBuilder hibuilder=new HighlightBuilder();
hibuilder.preTags("<h2>");
hibuilder.postTags("</h2>");
hibuilder.field("name");
List<String> list=new ArrayList<String>();
SearchResponse response=client.getClient().prepareSearch(index).setTypes(type)
.setQuery(matchQuery).highlighter(hibuilder).execute().get();
SearchHits searchHits=response.getHits();
for(SearchHit hit:searchHits){
Text [] text=hit.getHighlightFields().get("name").getFragments();
for(Text str:text){
list.add(str.toString());
}
}
return list;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springmvc-1.rar (69个子文件)
springmvc-1
pom.xml 4KB
.springBeans 453B
target
test-classes
m2e-wtp
web-resources
META-INF
maven
springmvc-1
springmvc-1
pom.xml 4KB
pom.properties 224B
MANIFEST.MF 115B
classes
i18n_en_US.properties 46B
i18n.properties 46B
i18n_zh_CN.properties 60B
com
atguigu
springmvc
handlers
HelloWorld.class 4KB
views
HelloView.class 1KB
service
Impl
ElSerarchServiceImpl.class 13KB
ElSerarchService.class 1KB
entities
Student.class 928B
Client.class 6KB
applicationContext.xml 619B
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
views
success.jsp 980B
dispatcherServlet-servlet.xml 2KB
lib
spring-beans-4.0.0.RELEASE.jar 654KB
spring-core-4.0.0.RELEASE.jar 933KB
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 1.57MB
spring-jdbc-4.0.0.RELEASE.jar 410KB
standard.jar 384KB
spring-context-4.0.0.RELEASE.jar 928KB
spring-webmvc-4.0.0.RELEASE.jar 645KB
spring-aop-4.0.0.RELEASE.jar 343KB
commons-logging-1.1.1.jar 59KB
com.springsource.org.aopalliance-1.0.0.jar 5KB
jstl.jar 20KB
spring-expression-4.0.0.RELEASE.jar 202KB
commons-logging-1.1.3.jar 61KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
spring-orm-4.0.0.RELEASE.jar 309KB
spring-aspects-4.0.0.RELEASE.jar 50KB
spring-tx-4.0.0.RELEASE.jar 242KB
spring-web-4.0.0.RELEASE.jar 646KB
index.jsp 3KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.common.component 569B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 736B
src
i18n_en_US.properties 46B
i18n.properties 46B
i18n_zh_CN.properties 60B
com
atguigu
springmvc
handlers
HelloWorld.java 4KB
views
HelloView.java 626B
service
Impl
ElSerarchServiceImpl.java 7KB
ElSerarchService.java 949B
entities
Student.java 470B
Client.java 5KB
applicationContext.xml 619B
.project 1KB
.classpath 4KB
build
classes
i18n_en_US.properties 46B
i18n.properties 46B
i18n_zh_CN.properties 60B
com
atguigu
springmvc
handlers
SpringMVCTest.class 7KB
HelloWorld.class 757B
views
HelloView.class 1KB
entities
User.class 3KB
ESTransportClient.class 338B
Address.class 1016B
applicationContext.xml 508B
共 69 条
- 1
江南之恋love
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页