1、pom.xml文件

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.3</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>

2、es配置(案例使用SpringCloud)

es.cluster-name=es
es.ip=127.0.0.1
es.port=9300
es.pool=5

3、es初始化

@Configuration
public class EsConfig {

@Autowired
private Environment env;

@SuppressWarnings("resource")
@Bean
public TransportClient transportClient() throws UnknownHostException{
Settings settings = Settings.builder()
.put("cluster.name", env.getProperty("es.cluster-name"))
.put("client.transport.sniff", true)
.put("thread_pool.search.size", Integer.valueOf(env.getProperty("es.pool")))
.build();
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(env.getProperty("es.ip")), Integer.valueOf(env.getProperty("es.port")));
TransportClient esClient = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
return esClient;
}

}

4、es索引Controller

@RestController
public class EsController {

@Autowired
EsService esService;

/**
* 初始化es索引
*
* @param userCode
* @return
*/
@RequestMapping(value = "/es/initIndex", method = { RequestMethod.GET, RequestMethod.POST })
public void initIndex() {
esService.initIndex();
}}

5、es索引接口

public interface EsService {

/**
* es索引
* @return
*/
void initIndex();

}

6、es索引接口实现

@Service
public class EsServiceImpl implements EsService {

Log log = LogFactory.getLog(EsServiceImpl.class);

@Autowired
TransportClient client;

public void initIndex(){
try {
XContentBuilder builder = XContentFactory
.jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("id")
.field("type", "integer")
.endObject()
.startObject("um")
.field("type", "keyword")
.endObject()
.startObject("question")
.field("type", "text")
.field("analyzer", "ik")
.field("search_analyzer","ik")
.endObject()
.startObject("questionType")
.field("type", "integer")
.endObject()
.startObject("nlp")
.field("type", "keyword")
.endObject()
.startObject("isHit")
.field("type", "integer")
.endObject()
.startObject("isSatisfied")
.field("type", "integer")
.endObject()
.startObject("opinion")
.field("type", "keyword")
.endObject()
.startObject("label")
.field("type", "keyword")
.endObject()
.startObject("askTimes")
.field("type", "integer")
.endObject()
.startObject("createdBy")
.field("type", "keyword")
.endObject()
.startObject("updatedBy")
.field("type", "keyword")
.endObject()
.startObject("createdDate")
.field("type", "date")
//.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("updatedDate")
.field("type", "date")
//.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.endObject()
.endObject()
.endObject();

//副本、分片

//定义中文分词+停词
String settingsJson = "{"
+ "\"number_of_replicas\": 1, "
+ "\"number_of_shards\": 5, "
+ "\"analysis\": { "
+ "\"analyzer\": { "
+ "\"ik\": { "
+ "\"tokenizer\": \"ik_max_word\", "
+ "\"type\": \"standard\", \"stopwords\": [\"也\",\"了\",\"仍\",\"从\",\"以\",\"使\",\"则\",\"却\",\"又\",\"及\",\"对\",\"就\",\"并\",\"很\",\"或\",\"把\",\"是\",\"的\",\"着\",\"给\",\"而\",\"被\",\"让\",\"在\",\"还\",\"比\",\"等\",\"当\",\"与\",\"于\",\"但\"] "
+ "} "
+ "} "
+ "} "
+ "}";

// Builder settings = Settings.builder()
// .put("number_of_replicas","1")
// .put("number_of_shards","5");

String mappingStr = builder.string();

//判断索引是否存在
client.admin().indices().prepareCreate("test_index").setSettings(settingsJson, XContentType.JSON).execute().actionGet();
client.admin().indices().preparePutMapping("test_index").setType("test").setSource(mappingStr,XContentType.JSON).execute().actionGet();
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
}
}

}

Java创建ES索引实现的更多相关文章

  1. 创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false}

    创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false} [2018-05-19T13: ...

  2. 创建es索引-格式化和非格式化

    创建es索引-格式化和非格式化 学习了:https://www.imooc.com/video/15768 索引有结构化和非结构化的区分: 1, 先创建索引,然后POST修改mapping 首先创建索 ...

  3. Python创建ES索引

    # pip install elasticsearch from datetime import datetime from elasticsearch import Elasticsearch es ...

  4. ES 记录之如何创建一个索引映射,以及一些设置

    ElasticSearch 系列文章 1 ES 入门之一 安装ElasticSearcha 2 ES 记录之如何创建一个索引映射 3 ElasticSearch 学习记录之Text keyword 两 ...

  5. phoenix连接hbase数据库,创建二级索引报错:Error: org.apache.phoenix.exception.PhoenixIOException: Failed after attempts=36, exceptions: Tue Mar 06 10:32:02 CST 2018, null, java.net.SocketTimeoutException: callTimeou

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. es创建普通索引以及各种查询

    创建索引 创建普通索引: PUT /my_index { "settings": { "index": { "number_of_shards&quo ...

  7. 基于java的ES开发

    3.1 环境配置 Jdk 1.8及以上 Elasticsearch.client 5.5.2(与服务器版本一致) Log4j 2.7及以下 maven工程必要的jar包依赖 <project x ...

  8. Elasticsearch笔记五之java操作es

    Java操作es集群步骤1:配置集群对象信息:2:创建客户端:3:查看集群信息 1:集群名称 默认集群名为elasticsearch,如果集群名称和指定的不一致则在使用节点资源时会报错. 2:嗅探功能 ...

  9. Elasticsearch ES索引

    ES是一个基于RESTful web接口并且构建在Apache Lucene之上的开源分布式搜索引擎. 同时ES还是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,能够横向 ...

随机推荐

  1. python 使用xlsxwriter 写入数据时,当数据中链接的后面包含空格时(如:"http://*** "),导出问题打开报错

    python 在使用 xlsxwriter组件写入数据时,当数据包含类似“http://*** /”数据时,导出的excel,打开时会提示如下错误: 没有查到相关的资料处理这个问题,可能原因为exce ...

  2. pyhton中的深浅copy

    深浅拷贝:数据分离情况 1. =赋值:数据完全共享(指向内存中的同一个对象)被赋值的变量指向的数据和原变量的数据都是指向内存中的同一个地址: (1)如果是不可变数据类型(数字.字符串等),修改其中的一 ...

  3. unity-消息的注册,监听,回调

    最近在空闲时间准备做个小游戏,先把一些基本框架搭建好,本次记录的是消息的注册,监听和回调等 其实这些就是基于C#的委托(delegate) 第一步:定义一些委托 namespace Common.Me ...

  4. 手写一个简版 asp.net core

    手写一个简版 asp.net core Intro 之前看到过蒋金楠老师的一篇 200 行代码带你了解 asp.net core 框架,最近参考蒋老师和 Edison 的文章和代码,结合自己对 asp ...

  5. 201771010120 苏浪浪 《面向对象程序设计(java)》第二周学习总结

    理论知识总结 第三章Java基本程序设计结构 1.基本知识:(1)标识符:是由字母.下划线.美元符号和数字组成,且第一个符号不能为数字.(2)关键字:剧啊语言中被赋予特定意义的一些单词.(3)注释 2 ...

  6. poj1679最小生成树是否唯一

    http://www.cnblogs.com/kuangbin/p/3147329.html #include<cstdio> #include<cstring> #inclu ...

  7. Text Reverse(hdu1062)

    输入方式:先输入整数,再循环输入字符串. 思考:字符串中有空格.那么要在字符串大循环输入前,首先,用"getchar()"函数读取scanf_s()函数缓冲区的空格或者空行或者换行 ...

  8. Python小技巧:如何批量更新已安装的库?

    众所周知,升级某个库(假设为 xxx),可以用pip install --upgrade xxx 命令,或者简写成pip install -U xxx . 如果有多个库,可以依次写在 xxx 后面,以 ...

  9. jmeter之cookies管理器的使用

    作用: 1 发送请求,经常要校验cookies信息  2 针对有的cookie是用的sessionid来进行校验的,这个就需要自己去配置cookie管理器里面的信息,而且sessionid是有时效性的 ...

  10. sqlmap基本使用

    sqlmap 使用 需要安装python2的环境 使用方法: 用最基础的get型注入开始 1.当我们发现注入点的时候, python sqlmap.py -u "http://192.168 ...