Java创建ES索引实现
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索引实现的更多相关文章
- 创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false}
创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false} [2018-05-19T13: ...
- 创建es索引-格式化和非格式化
创建es索引-格式化和非格式化 学习了:https://www.imooc.com/video/15768 索引有结构化和非结构化的区分: 1, 先创建索引,然后POST修改mapping 首先创建索 ...
- Python创建ES索引
# pip install elasticsearch from datetime import datetime from elasticsearch import Elasticsearch es ...
- ES 记录之如何创建一个索引映射,以及一些设置
ElasticSearch 系列文章 1 ES 入门之一 安装ElasticSearcha 2 ES 记录之如何创建一个索引映射 3 ElasticSearch 学习记录之Text keyword 两 ...
- 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 ...
- es创建普通索引以及各种查询
创建索引 创建普通索引: PUT /my_index { "settings": { "index": { "number_of_shards&quo ...
- 基于java的ES开发
3.1 环境配置 Jdk 1.8及以上 Elasticsearch.client 5.5.2(与服务器版本一致) Log4j 2.7及以下 maven工程必要的jar包依赖 <project x ...
- Elasticsearch笔记五之java操作es
Java操作es集群步骤1:配置集群对象信息:2:创建客户端:3:查看集群信息 1:集群名称 默认集群名为elasticsearch,如果集群名称和指定的不一致则在使用节点资源时会报错. 2:嗅探功能 ...
- Elasticsearch ES索引
ES是一个基于RESTful web接口并且构建在Apache Lucene之上的开源分布式搜索引擎. 同时ES还是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,能够横向 ...
随机推荐
- helm使用
helm 0. helm安装 基本上到github上面,下载二进制就行.mac的话用brew安装. https://github.com/helm/helm brew: brew install ku ...
- Spring 自动装配 byName
自动装配 byName,这种模式由属性名称(方法名)指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName.然后,它尝试 ...
- D. Almost Acyclic Graph 判断减一条边能不能得到DAG
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Mybatis 强大的结果集映射器resultMap
1. 前言 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC ...
- 重学 Java 设计模式:实战抽象工厂模式
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!
- day18 迭代器
1,迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个Stoplteration异常,只能往后走不能往前退: 2,可迭代对象:实现了迭代器协议的对象(如何实现: ...
- 永久关闭windows更新步骤
在搜索“web和windows”框中输入“服务” 在搜索结果中点击第一个,那个图标像齿轮的那个!如下图. 在打开的“服务”窗口中,我们找到windows update 找到”windows upd ...
- pytest 使用
import pytestfrom web_ui_YXBI.test_datas.common_datas import Common_Datas as cfrom selenium import w ...
- Linux学习(二):makefile
编译命令: gcc -o exefile src.c (将src.c编译,链接为exefile可执行文件) gcc -o obj.o -c src.c (将src.c编译为obj.o目标文件) mak ...
- (板子)缩点 + DAG上的DP(深搜)luogu P3387
板子传送门 根据题目意思,我们只需要找出一条点权最大的路径就行了,不限制点的个数.那么考虑对于一个环上的点被选择了,一整条环是不是应该都被选择,这一定很优,能选干嘛不选.很关键的是题目还允许我们重复经 ...