1.7.1: 创建索引

/**
* 创建索引
* */
@Test
public void createIndex(){
// 创建索引
CreateIndexResponse blog2 = client.admin().indices().prepareCreate("blog2").get();
System.out.println(blog2.toString()); }

默认创建好索引,mappings为空

1.7.2: 删除索引

/**
* 删除索引
* */
@Test
public void deleteIndex(){
// 删除索引
client.admin().indices().prepareDelete("blog2").get();
}

1.7.3:索引的映射操作

为什么要进行手动的映射?

在实际生产中经常会出现精度损失的现象,往往就是因为没有进行正确的索引映射或者压根就没进行索引映射
Elasticsearch最开始索引文档A,其中的一个字段是一个数字,是整数;通过自动类型猜测,并设置类型为整型(integer)或者长整型;
然后在索引另一个文档B,B文档在同一个字段中存储的是浮点型;那么这个时候elasticsearch就会把B文档中的小数删除,保留整数部分;
这样就会导致数据的不准确!

如果你习惯SQL数据库,或许知道,在存入数据前,需要创建模式来描述数据(schmal);尽管elasticsearch是一个无模式的搜索引擎,可以即时算出数据结构;

但是我们仍然认为由自己控制并定义结构是更好的;而且在实际的生产中,我们也是自己创建映射;

注意:注意创建mapping的时候,索引必须提前存在

{
"settings":{
"nshards":3,
"number_of_repli umber_of_cas":1
},
"mappings":{
"dahan":{
"dynamic":"strict",
"properties":{
"studentNo":{"type": "string", "store": true},
"name":{"type": "string","store": true,"index" : "analyzed","analyzer": "ik_max_word"},
"male":{"type": "string","store": true},
"age":{"type": "integer","store": true},
"birthday":{"type": "string","store": true},
"classNo":{"type": "string","store": true},
"address":{"type": "string","store": true,"index" : "analyzed","analyzer": "ik_max_word"},
"isLeader": {"type": "boolean", "index": "not_analyzed"}
}
}
}

1):创建索引

/**
* 创建索引
* */
@Test
public void createIndex(){
   
   CreateIndexResponse blog2 = client.admin().indices().prepareCreate("sanguo").get();
   System.out.println(blog2.toString());

}

2):通过代码创建索引配置信息(有错的情况)

/**
* Created by angel;
*/
public class CreateMappings {
   public static void main(String[] args) throws UnknownHostException {
       TransportClient client = null;
       Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("number_of_shards", 3);
       map.put("number_of_replicas", 1);
       Settings settings = Settings.builder()
              .put("cluster.name", "cluster")
              .build();
       client = new PreBuiltTransportClient(settings)
              .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop01"), 9300));
       System.out.println("========连接成功=============");


       XContentBuilder builder = null;
       try {
           builder = jsonBuilder()
                  .startObject()
                  .startObject("dahan").field("dynamic", "true")
                  .startObject("properties")
                  .startObject("studentNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("name").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()//.field("analyzer", "ik")
                  .startObject("male").field("type", "string").field("store", "yes").endObject()//.field("analyzer", "ik")
                  .startObject("age").field("type", "integer").field("store", "yes").endObject()
                  .startObject("birthday").field("type", "string").field("store", "yes").endObject()
                  .startObject("classNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("address").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()
                  .startObject("isLeader").field("type", "boolean").field("store", "yes").field("index", "not_analyzed").endObject()
                  .endObject()
                  .endObject()
                  .endObject();
           PutMappingRequest mapping = Requests.putMappingRequest("sanguo")
                  .type("dahan")
                  .source(builder);
           UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest();
           updateSettingsRequest.settings(map);
           client.admin().indices().updateSettings(updateSettingsRequest).actionGet();
           client.admin().indices().putMapping(mapping).get();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
}

如果有同学学习的比较扎实,那么会记住以下内容:

当索引不存在的时候,可以指定副本数和分片数

当索引存在的时候,只能指定副本数

所以,我们在构建索引的时候就要指定确定的分片和副本:

1):创建索引的时候直接指定索引的shard数和副本数

/**
* 创建索引
* */
@Test
public void createIndex(){
   // 创建索引
   Map<String, Integer> map = new HashMap<String, Integer>();
   map.put("number_of_shards", 3);
   map.put("number_of_replicas", 1);
   CreateIndexResponse blog2 = client.admin().indices().prepareCreate("sanguo").setSettings(map).get();
   System.out.println(blog2.toString());

}

2):然后在创建映射信息的时候,就可以忽略掉分片数和副本数了。直接进行索引的映射:

/**
* Created by angel;
*/
public class CreateMappings {
   public static void main(String[] args) throws UnknownHostException {
       TransportClient client = null;
       Settings settings = Settings.builder()
              .put("cluster.name", "cluster")
              .build();
       client = new PreBuiltTransportClient(settings)
              .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop01"), 9300));
       System.out.println("========连接成功=============");


       XContentBuilder builder = null;
       try {
           builder = jsonBuilder()
                  .startObject()
                  .startObject("dahan").field("dynamic", "true")
                  .startObject("properties")
                  .startObject("studentNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("name").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()//.field("analyzer", "ik")
                  .startObject("male").field("type", "string").field("store", "yes").endObject()//.field("analyzer", "ik")
                  .startObject("age").field("type", "integer").field("store", "yes").endObject()
                  .startObject("birthday").field("type", "string").field("store", "yes").endObject()
                  .startObject("classNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("address").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()
                  .startObject("isLeader").field("type", "boolean").field("store", "yes").field("index", "not_analyzed").endObject()
                  .endObject()
                  .endObject()
                  .endObject();
           PutMappingRequest mapping = Requests.putMappingRequest("sanguo")
                  .type("dahan")
                  .source(builder);
           client.admin().indices().putMapping(mapping).get();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
}

es之java索引操作的更多相关文章

  1. ES入门三部曲:索引操作,映射操作,文档操作

    ES入门三部曲:索引操作,映射操作,文档操作 一.索引操作 1.创建索引库 #语法 PUT /索引名称 { "settings": { "属性名": " ...

  2. 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】

    一.ES使用,以及客户端 1.pom引用 <dependency> <groupId>org.elasticsearch.client</groupId> < ...

  3. es之java分页操作

    按照一般的查询流程来说,如果我想查询前10条数据: · 1 客户端请求发给某个节点 · 2 节点转发给个个分片,查询每个分片上的前10条 · 3 结果返回给节点,整合数据,提取前10条 · 4 返回给 ...

  4. elasticsearch java 索引操作

    1.添加maven依赖 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>el ...

  5. ElasticSearch 获取es信息以及索引操作

    检查集群的健康情况 GET /_cat/health?v green:每个索引的primary shard和replica shard都是active状态的yellow:每个索引的primary sh ...

  6. Elasticsearch必知必会的干货知识二:ES索引操作技巧

    该系列上一篇文章<Elasticsearch必知必会的干货知识一:ES索引文档的CRUD> 讲了如何进行index的增删改查,本篇则侧重讲解说明如何对index进行创建.更改.迁移.查询配 ...

  7. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  8. ES系列十五、ES常用Java Client API

    一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...

  9. 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作

    spring boot 2.X集成ES 进行CRUD操作  完整版 内容包括: ============================================================ ...

随机推荐

  1. Springboot2.x集成单节点Redis

    Springboot2.x集成单节点Redis 说明 在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客 ...

  2. 最全的DevOps自动化工具集合

    版本控制&协作开发:GitHub.GitLab.BitBucket.SubVersion.Coding.Bazaar 自动化构建和测试:Apache Ant.Maven .Selenium.P ...

  3. 【C语言--数据结构】线性表链式存储结构

    直接贴代码 头文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__ typedef void LinkList; typedef struct _tag_L ...

  4. MySQL索引,备份和还原

    1.索引  1.索引是占硬盘空间 ,也是按页存放的 . 思考题:一个索引页,(数据页)  占用多少个字节  .SQL Server 8192个字节 2.索引:是一种有效组合数据的方式,为了快速查找指定 ...

  5. TestCase维护和思维导图

    在软件整个生命周期中,测试应该尽早地开始,因为测试对象不只是程序,还有文档和数据,所以针对需求分析说明书.概要设计和详细设计说明书,测试如何快速理解项目需求,进行下一步的工作呢? 本人觉得,如果只是看 ...

  6. anconda下安装opencv

    提示:如果你安装了python其它版本,或者在anaconda中创建了虚拟环境,应该先激活你的环境,然后再在命令窗口执行下面的操作 1.首先下载所需镜像文件: 我们需要上网站去下载我们需要的版本. 官 ...

  7. HNUSTOJ-1009 格雷码

    1009: 格雷码 时间限制: 1 Sec  内存限制: 128 MB提交: 90  解决: 78[提交][状态][讨论版] 题目描述 对于给定的正整数n,格雷码为满足如下条件的一个编码序列:(1) ...

  8. Redis数据类型及基本命令

    一.基础命令 提示:Redis不区分命令大小写 1.获得符合规则的键名列表 keys pattern    //pattern支持glob风格通配符格式 2.判断一个键是否存在 exists key  ...

  9. Linux文档整理之【Mysql安装与配置】

    最近公司让整理一个Linux安装Mysql的文档.所以就整理了一下,这里将自己整理的详细文档做个笔记. 1.下载Mysql. https://dev.mysql.com/downloads/mysql ...

  10. Optional接口简记

    @Data public class Employee { private String name; } @Data public class Company { private String nam ...