package com.example.controller;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
@RequestMapping("/es")
public class ElasticsearchController {
@Autowired
private TransportClient client;
protected String index="movies";
protected String indexType="movie";
// 获取单条记录
@RequestMapping("/get")
public GetResponse hello() {
GetResponse response = client.prepareGet(index, indexType, "2").get();
return response;
}
// 插入数据
@RequestMapping("/save")
public String save() {
Map map = new HashMap<String, Object>();
map.put("id", "2");
map.put("name", "hehe");
map.put("age", 18);
IndexResponse response = client.prepareIndex("movies", "movie", map.get("id").toString())
.setSource(map).execute().actionGet();
return response.getId();
}
// 获取单条记录
@RequestMapping("/del")
public String del() {
DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete("test").execute().actionGet();
System.out.println("是否删除成功:"+deleteIndexResponse.isAcknowledged());
return "成功";
}
@RequestMapping("/addAll")
public String addAll() throws IOException{
BulkRequestBuilder bulkRequest = client.prepareBulk();
Date time = new Date();
index="schools";
indexType="getSchools";
// either use client#prepare, or use Requests# to directly build index/delete requests
for(int i=0;i<10000;i++){
bulkRequest.add(client.prepareIndex(index, indexType, i+"")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("id",i+"")
.field("author","EGDRE"+i)
.field("title","EGB"+i)
.field("content","这是EGB"+i+""+"有关的书籍")
.field("price","40"+i)
.field("age","40")
.field("view","300")
.field("tag","e,c"+i)
.field("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
.endObject()
)
);
}
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
// process failures by iterating through each bulk response item
System.out.println(bulkResponse.getTook());
}
return "成功";
}
// 查询搜索
@RequestMapping("/search")
public List<String> search() {
SearchResponse response = client.prepareSearch("movies")
.setTypes("movie")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
//.setQuery(QueryBuilders.termQuery("tag", "a,b,c14")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(0).to(188)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();
// SearchResponse response =client.prepareSearch().execute().actionGet();
SearchHits searchHits = response.getHits();
System.err.println("总数:" + searchHits.getTotalHits());
List<String> data = new ArrayList<String>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
data.add(json);
System.err.println(json);
}
return data;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot+elasticsearch,入门最详细的项目
共100个文件
xml:86个
properties:3个
java:3个
需积分: 50 137 下载量 31 浏览量
2018-11-08
16:58:18
上传
评论 3
收藏 114KB ZIP 举报
温馨提示
这是一个入门级别的springboot+elasticsearch的项目,内涵增删改查,值得刚入门的大兄弟们学习
资源推荐
资源详情
资源评论
收起资源包目录
springboot+elasticsearch,入门最详细的项目 (100个子文件)
ElasticsearchController.class 8KB
ElasticsearchConfiguration.class 5KB
ElasticsearchDemoApplication.class 733B
mvnw.cmd 6KB
.gitignore 268B
elasticsearchDemo.iml 9KB
maven-wrapper.jar 47KB
ElasticsearchController.java 5KB
ElasticsearchConfiguration.java 3KB
ElasticsearchDemoApplication.java 325B
mvnw 9KB
application.properties 142B
application.properties 142B
maven-wrapper.properties 116B
workspace.xml 51KB
pom.xml 2KB
Project_Default.xml 1KB
misc.xml 1KB
Maven__org_springframework_boot_spring_boot_starter_logging_1_5_9_RELEASE.xml 748B
Maven__org_springframework_boot_spring_boot_starter_tomcat_1_5_9_RELEASE.xml 741B
Maven__org_springframework_boot_spring_boot_autoconfigure_1_5_9_RELEASE.xml 734B
Maven__org_springframework_boot_spring_boot_starter_web_1_5_9_RELEASE.xml 720B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_smile_2_8_10.xml 710B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_cbor_2_8_10.xml 703B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_yaml_2_8_10.xml 703B
Maven__org_springframework_boot_spring_boot_starter_1_5_9_RELEASE.xml 692B
Maven__org_elasticsearch_client_elasticsearch_rest_client_5_6_1.xml 678B
Maven__org_springframework_spring_expression_4_3_13_RELEASE.xml 665B
Maven__org_elasticsearch_plugin_transport_netty3_client_5_6_1.xml 664B
Maven__org_elasticsearch_plugin_transport_netty4_client_5_6_1.xml 664B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_5_23.xml 660B
compiler.xml 646B
Maven__org_springframework_spring_context_4_3_13_RELEASE.xml 644B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_8_0.xml 644B
Maven__org_elasticsearch_plugin_lang_mustache_client_5_6_1.xml 643B
Maven__org_springframework_spring_webmvc_4_3_13_RELEASE.xml 637B
Maven__org_apache_tomcat_tomcat_annotations_api_8_5_23.xml 636B
Maven__org_apache_lucene_lucene_analyzers_common_6_6_1.xml 636B
Maven__org_springframework_boot_spring_boot_1_5_9_RELEASE.xml 636B
Maven__com_fasterxml_jackson_core_jackson_databind_2_8_10.xml 630B
Maven__org_springframework_spring_beans_4_3_13_RELEASE.xml 630B
Maven__org_elasticsearch_plugin_parent_join_client_5_6_1.xml 629B
Maven__org_apache_lucene_lucene_backward_codecs_6_6_1.xml 629B
Maven__org_apache_tomcat_embed_tomcat_embed_core_8_5_23.xml 625B
Maven__org_springframework_spring_core_4_3_13_RELEASE.xml 623B
Maven__org_elasticsearch_plugin_percolator_client_5_6_1.xml 622B
Maven__org_apache_lucene_lucene_spatial_extras_6_6_1.xml 622B
Maven__org_springframework_spring_aop_4_3_13_RELEASE.xml 616B
Maven__org_springframework_spring_web_4_3_13_RELEASE.xml 616B
Maven__org_apache_httpcomponents_httpasyncclient_4_1_3.xml 612B
Maven__org_apache_tomcat_embed_tomcat_embed_el_8_5_23.xml 611B
Maven__com_fasterxml_jackson_core_jackson_core_2_8_10.xml 602B
Maven__org_elasticsearch_plugin_reindex_client_5_6_1.xml 601B
Maven__org_apache_lucene_lucene_queryparser_6_6_1.xml 601B
Maven__org_apache_lucene_lucene_highlighter_6_6_1.xml 601B
Maven__io_netty_netty_codec_http_4_1_13_Final.xml 600B
Maven__com_github_spullara_mustache_java_compiler_0_9_3.xml 595B
Maven__io_netty_netty_transport_4_1_13_Final.xml 593B
Maven__org_apache_httpcomponents_httpcore_nio_4_4_5.xml 591B
Maven__org_apache_lucene_lucene_spatial3d_6_6_1.xml 587B
Maven__io_netty_netty_resolver_4_1_13_Final.xml 586B
Maven__org_apache_lucene_lucene_grouping_6_6_1.xml 580B
Maven__io_netty_netty_handler_4_1_13_Final.xml 579B
Maven__org_apache_httpcomponents_httpclient_4_5_3.xml 577B
Maven__ch_qos_logback_logback_classic_1_1_11.xml 575B
Maven__org_apache_lucene_lucene_queries_6_6_1.xml 573B
Maven__org_apache_lucene_lucene_suggest_6_6_1.xml 573B
Maven__org_apache_logging_log4j_log4j_core_2_8_2.xml 573B
Maven__org_apache_lucene_lucene_sandbox_6_6_1.xml 573B
Maven__org_apache_lucene_lucene_spatial_6_6_1.xml 573B
Maven__commons_logging_commons_logging_1_1_3.xml 572B
Maven__io_netty_netty_common_4_1_13_Final.xml 572B
Maven__io_netty_netty_buffer_4_1_13_Final.xml 572B
Maven__org_apache_logging_log4j_log4j_api_2_8_2.xml 566B
Maven__org_apache_lucene_lucene_memory_6_6_1.xml 566B
Maven__org_elasticsearch_elasticsearch_5_6_1.xml 566B
Maven__org_elasticsearch_client_transport_5_6_1.xml 566B
Maven__io_netty_netty_codec_4_1_13_Final.xml 565B
Maven__org_apache_httpcomponents_httpcore_4_4_8.xml 563B
Maven__org_slf4j_log4j_over_slf4j_1_7_25.xml 562B
Maven__net_sf_jopt_simple_jopt_simple_5_0_2.xml 556B
Maven__org_hdrhistogram_HdrHistogram_2_1_9.xml 555B
Maven__ch_qos_logback_logback_core_1_1_11.xml 554B
Maven__org_apache_lucene_lucene_join_6_6_1.xml 552B
Maven__org_apache_lucene_lucene_core_6_6_1.xml 552B
Maven__org_apache_lucene_lucene_misc_6_6_1.xml 552B
Maven__org_slf4j_jcl_over_slf4j_1_7_25.xml 548B
Maven__commons_codec_commons_codec_1_10.xml 543B
Maven__org_slf4j_jul_to_slf4j_1_7_25.xml 534B
Maven__io_netty_netty_3_10_6_Final.xml 523B
Maven__org_elasticsearch_securesm_1_1.xml 517B
Maven__org_slf4j_slf4j_api_1_7_25.xml 513B
Maven__org_elasticsearch_jna_4_4_0_1.xml 510B
Maven__joda_time_joda_time_2_9_9.xml 506B
Maven__com_carrotsearch_hppc_0_7_1.xml 499B
Maven__com_tdunning_t_digest_3_0.xml 497B
Maven__org_yaml_snakeyaml_1_17.xml 495B
modules.xml 274B
encodings.xml 256B
bainuo.xml 87B
共 100 条
- 1
资源评论
- liuyugyan2021-09-15啥也没有,白瞎了积分。。。
帅气的CXY
- 粉丝: 7
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功