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();
}
}

}

原文地址:https://www.cnblogs.com/wangymd/p/11011694.html

时间: 2024-07-30 10:25:48

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

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

环境描述: 操作系统版本:CentOS release 6.5 (Final) 内核版本:2.6.32-431.el6.x86_64 phoenix版本:phoenix-4.10.0 hbase版本:hbase-1.2.6 表SYNC_BUSINESS_INFO_BYDAY数据库量:990万+ 问题描述: 通过phoenix客户端连接hbase数据库,创建二级索引时,报下面的错误: 0: jdbc:phoenix:host-10-191-5-226> create index SYNC_BUSI

java 创建线程

java创建线程有3种方式: (1)继承Thread(2)实现Runnable接口(3)实现Callable接口 1.继承Thead package com.java.thread; public class ThreadClient { public static void main(String[] args) { Print p1 = new Print(); Print p2 = new Print(); p1.start(); p2.start(); } } class Print e

Alibaba Java开发手册索引规约学习笔记

最近一段时间再看阿里巴巴 Java开发手册索引规约,写篇帖子总结一下,索引规约内容如下 索引规约1. [强制]业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引. 说明: 不要以为唯一索引影响了 insert 速度,这个速度损耗可以忽略,但提高查找速度是明显的: 另外,即使在应用层做了非常完善的校验控制,只要没有唯一索引,根据墨菲定律,必然有脏数据产生. 2. [强制]超过三个表禁止 join.需要 join 的字段,数据类型必须绝对一致: 多表关联查询时,保证被关联的字段需要有

Java创建二叉树

二叉树的值保存在数组中,以0作为分隔,数字0表示空节点,数组 public static final int[] TREE_VALUE = new int[]{1,2,3,0,4,5,0,0,6,0,0,7,0,0,8,0,9,10,0,0,0}; 表示的二叉树是: /** * 维护构建二叉树的值和值索引 */ public static class TreeValue { public static int index = 0; public static final int[] TREE_V

Java创建Timestamp的几种方式

1.java创建Timestamp的几种方式 Timestamp time1 = new Timestamp(System.currentTimeMillis()); Timestamp time2 = new Timestamp(new Date().getTime()); Timestamp time3 = new Timestamp(Calendar.getInstance().getTimeInMillis()); //不建议使用 Timestamp time4 = new Timest

MySQL创建复合索引

在MySQL数据库中,创建复合索引的时候,不知道在创建过程中哪个列在前面,哪个列该在后面,用以下方式即可: select count(distinct first_name)/count(*) as first_name_selectivity,count(distinct last_name)/count(*) as last_name_selectivity,count(*)from actor\G mysql> select count(distinct first_name)/count

SQL Server创建复合索引时,复合索引列顺序对查询的性能影响

说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因:一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗?二来觉得,索引是个非常大的话题,很难概括出所有的情况,你不整出点新意来,倒是有抄袭照搬的嫌疑 既然写了,就写一点稍微不一样的东西出来,好了,废话打住,开搞 /* 20160814备注:今天发现一个类似的文章:http://www.cnblogs.com/fly_zj/archive/2012/08/11/2633629.html : 可以

[ElasticSearch]Java API 之 索引管理

ElasticSearch为了便于处理索引管理(Indices administration)请求,提供了 org.elasticsearch.client.IndicesAdminClient接口.通过如下代码从 Client 对象中获得这个接口的实现: IndicesAdminClient indicesAdminClient = client.admin().indices(); IndicesAdminClient定义了好几种prepareXXX()方法作为创建请求的入口点. 1. 索引

java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1解决办法

java.sql.SQLException: 索引中丢失  IN 或 OUT 参数:: 1 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162) at oracle.jdbc.driver.OraclePreparedStatement.pr