spring boot + maven + idea jdk1.8以上 搭建
一、
pom.xml文件 ,如果不需要连接数据库,可以不引入数据库连接依赖,在程序入口类加上这句注解
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>ag.com.es</groupId><artifactId>ag-com-es</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><description>elasticsearch project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- json依赖--><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><!-- MYSQL --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- Swagger测试插件--><!-- 访问地址 http://ip:port/swagger-ui.html --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.6.1</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- oracle驱动,数据库是12c的驱动自己下载ojdbc.jar包导入maven库的--><dependency><groupId>oracle-java-jdbc-core</groupId><artifactId>oracle-java-jdbc-core</artifactId><version>1.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins></build>
</project>
二、创建一个controller类
1、创建索引 创建索引地址官网文档https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs.html
创建索引代码:
//获取客户端/*** 获取客户端* @return*/public static TransportClient getClient() {try {TransportClient client;Settings esSettings = Settings.builder().put("cluster.name", "elasticsearch") //设置ES实例的名称.put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中.put("client.transport.ignore_cluster_name", true) //如果集群名不对,也能连接.build();client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。//此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));return client;} catch (Exception e) {e.printStackTrace();return null;}}
//创建索引结构
//一、普通创建方式
IndexResponse response = client.prepareIndex("twitter", "tweet").get();
RestStatus status = response.status();//创建情况
//二、涉及到地理位置坐标系搜索的索引创建方式
client.admin().indices().prepareCreate(indexName).execute().actionGet();//创建索引结构XContentBuilder builder = null;builder = XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties").startObject("pin").startObject("properties").startObject("location").field("type","geo_point").endObject().endObject().endObject().endObject().endObject().endObject();PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(typeName).source(builder);getClient().admin().indices().putMapping(mapping).actionGet();
2、添加数据即初始化索引
//注意,你也可以索引你的文件为JSON字符串,你不必给一个ID:
IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()).get();//使用Map<String,Object>infoMap = new HashMap<>();方式
Map<String,Object>infoMap = new HashMap<>()
infoMap.put("key1","value1");
infoMap.put("key2","value2");
IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(infoMap).get();
3、查询数据
client = EsUtil.getClient();SearchResponse searchResponse = client.prepareSearch("twitter").setTypes("tweet").setSize(20)//返回条数 默认十条//指定匹配字段 fildNames 要匹配的字段名称用多个用逗号隔开.setQuery(QueryBuilders.multiMatchQuery(keyWord, fildNames))//所用字段//.setQuery(new QueryStringQueryBuilder(keyword)).get();long totalHits1 = searchResponse.getHits().totalHits;//命中个数SearchHit[] searchHits = searchResponse.getHits().getHits(); //执行检索List result = new ArrayList();for (int i = 0; i < searchHits.length; i++) {
String sourceAsString = searchHits[i].getSourceAsString();result.add(JSONObject.fromObject(sourceAsString));}return result.toString();