elasticsearch 基本概念

创建索引库

   就是存放索引的数据库, 在索引中创建 映射 

   put http://localhost:9200/索引库名称                          请求体:

{
  "settings":{
  "index":{
      "number_of_shards":1,
      "number_of_replicas":0
   }    
  }
}

number_of_shards:设置分片的数量,在集群中通常设置多个分片,表示一个索引库将拆分成多片分别存储不同
                                   的结点,提高了ES的处理能力和高可用性,入门程序使用单机环境,这里设置为1。
number_of_replicas:设置副本的数量,设置副本是为了提高ES的高可靠性,单机环境设置为0.
                                   如下是创建的例子,创建xc_course索引库,共1个分片,0个副本:

创建映射

就是创建表在6.0版本中一个索引库只能有一个表,设置索引中 字段的属性 一旦创建只能增加,不能修改已经创建的 

   post 请求:http://localhost:9200/xc_course/doc/_mapping

 {
  "properties": {   
           "name": {
              "type": "text"
           },
           "description": {
              "type": "text"
           },
           "studymodel": {
              "type": "keyword"
           }
        }
}

创建文档 

ES中的文档相当于MySQL数据库表中的记录。         就是mysql中的一行
发送:put 或Post http://localhost:9200/xc_course/doc/id值   (如果不指定id值ES会自动生成ID)
 http://localhost:9200/xc_course/doc/4028e58161bcf7f40161bcf8b77c0000             请求体:
   对应映射设置的属性值

{
  "name":"Bootstrap开发框架",
  "description":"Bootstrap是由Twitter推出的一个前台页面开发框架,在行业之中使用较为广泛。此开发框架包
含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松的实现一个不受浏览器限制的
精美界面效果。",
  "studymodel":"201001"
}

常用映射类型

   text        表示文本       

  analyzer  表示创建索引使用的分词库  ik_max_word表示使用 分词尽量细,方便搜索时更多

  search_analyzer 表示 搜索时使用的分词库  ik_smart表示分词尽量长

"name": {
                  "type": "text",
                  "analyzer":"ik_max_word",
                  "search_analyzer":"ik_smart"
   }

index   表示要不要进行索引  不会创建索引搜索就不会搜索到  列如存放图片的路径就不需要      默认true 

 "pic": {
  "type": "text",           
              "index":false
           }

store 是否在source之外存储,每个文档索引后会在 ES中保存一份原始文档,存放在"_source"中,一般情况下不需要设置
store为true,因为在_source中已经有一份原始文档了。

keyword关键字字段

  对关键字不会进行分词   表示精准搜索才会搜索到

{
  "properties": {   
           "studymodel":{
             "type":"keyword"
           },
            "name":{
             "type":"keyword"
           }
  }   
}

date日期类型

f通过format设置日期格式下边的设置允许date字段存储年月日时分秒、年月日及毫秒三种格式。

{
"properties": {    
        "timestamp": {
          "type":   "date",
          "format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd"
        }
      }
}

java 操作的APi

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.2.1</version>
        </dependency>

 操作的两种客户端 

SpringBootTest
@RunWith(SpringRunner.class)
public class TestIndex {
    @Autowired
    RestHighLevelClient client;
    
    @Autowired
    RestClient restClient;
}

更新文档

  顺序是:先检索到文档、将原来的文档标记为删除、创建新文档、删除旧文档,创建新文档就会重建索引。

  通过请求Url有两种方法:
      1、完全替换  Post:http://localhost:9200/xc_test/doc/3

 {
 "name":"spring cloud实战",
 "description":"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
 "studymodel":"201001"
 "price":5.6
 }

    2.局部更新   下边的例子是只更新price字段。
    post: http://localhost:9200/xc_test/doc/3/_update

{
"doc":{"price":66.6}
}
    //更新文档
    @Test
    public void updateDoc() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("xc_course", "doc", 
"4028e581617f945f01617f9dabc40000");
        Map<String, String> map = new HashMap<>();
        map.put("name", "spring cloud实战");
        updateRequest.doc(map);
        UpdateResponse update = client.update(updateRequest);
        RestStatus status = update.status();
        System.out.println(status);
    }

删除索引库

    //删除索引库
    @Test
    public void testDeleteIndex() throws IOException {
        //删除索引对象
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("wyc_course");
        //操作索引的客户端
        IndicesClient indices = client.indices();
        //执行删除索引
        DeleteIndexResponse delete = indices.delete(deleteIndexRequest);
        //得到响应
        boolean acknowledged = delete.isAcknowledged();
        System.out.println(acknowledged);

    }

添加索引库 及设置映射

    //创建索引库
    @Test
    public void testCreateIndex() throws IOException {
        //创建索引对象
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("xc_course");
        //设置参数
        createIndexRequest.settings(Settings.builder().put("number_of_shards","1").put("number_of_replicas","0"));
        //指定映射
        createIndexRequest.mapping("doc"," {\n" +
                " \t\"properties\": {\n" +
                "            \"studymodel\":{\n" +
                "             \"type\":\"keyword\"\n" +
                "           },\n" +
                "            \"name\":{\n" +
                "             \"type\":\"keyword\"\n" +
                "           },\n" +
                "           \"description\": {\n" +
                "              \"type\": \"text\",\n" +
                "              \"analyzer\":\"ik_max_word\",\n" +
                "              \"search_analyzer\":\"ik_smart\"\n" +
                "           },\n" +
                "           \"pic\":{\n" +
                "             \"type\":\"text\",\n" +
                "             \"index\":false\n" +
                "           }\n" +
                " \t}\n" +
                "}", XContentType.JSON);
        //操作索引的客户端
        IndicesClient indices = client.indices();
        //执行创建索引库
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
        //得到响应
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println(acknowledged);
    }

  查询索引

   其中id在创建索引时不指定自动生成的 

    @Test
    public void testGetDoc() throws IOException {
        //查询请求对象
        GetRequest getRequest = new GetRequest("xc_course","doc","AQFYBm4BZkCN9Sl9Vn43");
        GetResponse getResponse = client.get(getRequest);
        //得到文档的内容
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        System.out.println(sourceAsMap);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值