elasticsearch

一、elasticsearch 相关概念

二、elasticsearch 安装配置

三、 elasticsearch  术语

1、索引  相当于 mysql中的一个数据库

2、types 相当于 mysql中的一个表

3、Documents 相当于mysql中的 一行数据

4、filelds 相当于mysql  一列

5、mapping  对字段的一些 约束定义  相当于 mysql 表结构的定义

6、接近实时NRT 

Elasticsearch是一个接近实时的搜索平台。这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒以内)

7、集群 

8、node

9、分片复制

第四章 ElasticSearch的客户端操作

实际开发中,主要有三种方式可以作为elasticsearch服务的客户端:

  • 第一种,elasticsearch-head插件

  • 第二种,使用elasticsearch提供的Restful接口直接访问

  • 第三种,使用elasticsearch提供的API进行访问

使用 插件:

1、创建索引

索引名

分片数量

副本

 

 

这里我创建了两个索引 ,msg 和 index-hello

都使用了 默认分片 和 默认副本数量 

现在 我安装的是单机版 所以分片上面的 数字是灰色(Unassigned)

上面的健康值 是黄色 集群健康值: yellow (10 of 20)  有10个结点是不可用的。

也可以使用 postman 来进行http 对 es进行操作

1、使用postman向es添加数据

put  localhost:9200/blog  向blog 索引添加数据

 

一开始 es里面是 没有 blog 这个索引的  发送请求 他就会创建这个索引

但是 查看索引信息  mappings 是空的

 

我们再发送一个请求 localhost:9200/blog1  同时发送 mappings

解释一下 发送的mappings

{

    "mappings": { //相当于mysql 创建表的语句。是处理数据的方式和规则方面做一些限制,如某个字段的数据类型、默认值、分析器、是否被索引等等,这些都是映射里面可以设置的,其它就是处理es里面数据的一些使用规则设置也叫做映射,按着最优规则处理数据对性能提高很大,因此才需要建立映射,并且需要思考如何建立映射才能对性能更好。

        "article": {// 表示 type  相当于 mysql 里有多少个表。article文章表

            "properties": { //属性 就相当于字段的意思

                "id": { //id字段

                    "type": "long", //数据类型 长整型

                    "store": true,//存储  存储的

                    "index":"not_analyzed"//索引 默认是无索引

                },

                "title": { //字段title 标题

                    "type": "text",//数据类型

                    "store": true,//存储 存储的

                    "index":"analyzed",//索引

                    "analyzer":"standard"//使用的分词器 标准分词器

                },

                "content": { //字段content 内容

                    "type": "text",//数据类型

                    "store": true,//存储 存储的

                    "index":"analyzed",//索引

                    "analyzer":"standard"//使用的分词器 标准分词器

                }

            }

        }

    }

}

 

提交 成功 查看 http://localhost:9100/ 刷新页面 查看索引信息

 

 之前blog 里面没有设置 mappings

现在我们来设置他

 

需要注意的地方  这个blog 是之前我们创建好的 现在要设置mappings

在 url上面有所不同 需要加上 为那个 type 做什么 

localhost:9200/blog/hello/_mapping 表示 为 hello 这个type (_mapping表示 添加mapping  )

下面的请求体里面 mapping 要去掉  ,type要改成 hello.

提交:返回  "acknowledged": true 说明 设置mappings成功。

也可以删除 索引库

 

用postman 先创建索引blog2 再删除他

 

 刷新 可见已删除了

 

使用  elasticsearch-head插件 也可以进行相关操作

 

 

 

给bolg2设置mappings

 

 删除blog2

刷新没有了

 

三 、创建文档 也就是向索引库添加数据 

向索引 blog 的 hello(type) 表添加数据。

localhost:9200/blog/hello/1 这里的 1 是_id 是文档的id

请求体里面的id 是我们的 属性id ,他俩不是一个。

 

{

    "_index": "blog",

    "_type": "hello",

    "_id": "1", //文档正真的id 并不是属性id

    "_version": 1,

    "result": "created",

    "_shards": {

        "total": 2,

        "successful": 1,

        "failed": 0

    },

    "_seq_no": 0,

    "_primary_term": 1

}

查看我们添加进去的数据

刷新页面:_id是我们文档正真的id 后面那个id是我们的属性id 一般我们保持一致就行了

这里再后面也可以不添加 id

如:

 

下图

下图:1、索引(相当于mysql的数据库)

2、类型(相当于表)

3、字段:这里文本框可以进行过滤

 postman 删除id为1 的文档数据

刷新页面已经删除

 

 es 更新数据:他的底层是先删除在添加

1、我们搜先添加一个文档

 

根据文档id 1 修改文档 

 

 

查看修改侯的数据

es 查询:

1、根据id查询文档

 

2、根据关键词来查询

我们之前说过 我们设置的blog 

title和content 的分词器是标准分词器 就是一个汉字 作为一个关键词

所以我们搜索的时候 只能是一个汉字。

如果两个字就查不到

 

{

    "took": 1,

    "timed_out": false,

    "_shards": {

        "total": 5,

        "successful": 5,

        "skipped": 0,

        "failed": 0

    },

    "hits": {//命中

        "total": 11,//总条数

        "max_score": 0.7261542,

        "hits": [ //结果

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "7",

                "_score": 0.7261542,

                "_source": {

                    "id": 7,

                    "title": "新加的文档标题7",

                    "content": "新加的文档内容7"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "PFtok3oBIvQ9t1nU0G7t",

                "_score": 0.2876821,

                "_source": {

                    "id": 1,

                    "title": "新加的文档标题1",

                    "content": "新加的文档内容1"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "3",

                "_score": 0.18232156,

                "_source": {

                    "id": 3,

                    "title": "新加的文档标题1",

                    "content": "新加的文档内容1"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "11",

                "_score": 0.18232156,

                "_source": {

                    "id": 11,

                    "title": "新加的文档标题11",

                    "content": "新加的文档内容11"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "2",

                "_score": 0.13353139,

                "_source": {

                    "id": 2,

                    "title": "新加的文档标题1",

                    "content": "新加的文档内容1"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "4",

                "_score": 0.13353139,

                "_source": {

                    "id": 4,

                    "title": "新加的文档标题4",

                    "content": "新加的文档内容4"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "6",

                "_score": 0.13353139,

                "_source": {

                    "id": 6,

                    "title": "新加的文档标题6",

                    "content": "新加的文档内容6"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "5",

                "_score": 0.105360515,

                "_source": {

                    "id": 5,

                    "title": "新加的文档标题5",

                    "content": "新加的文档内容5"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "8",

                "_score": 0.105360515,

                "_source": {

                    "id": 8,

                    "title": "新加的文档标题8",

                    "content": "新加的文档内容8"

                }

            },

            {

                "_index": "blog",

                "_type": "hello",

                "_id": "9",

                "_score": 0.105360515,

                "_source": {

                    "id": 9,

                    "title": "新加的文档标题9",

                    "content": "新加的文档内容9"

                }

            }

        ]

    }

}

 

 

3、根据字符串来查询

{
    "query": {
        "query_string": {
            "default_field": "title", //默认搜索域 相当于 mysql 根据那个字段来搜索
            "query": "关键词" //关键词 他会根据查询内容先分词
        }
    }
}

因为我们 使用的是 标准分析器  他会把关键词 一个汉字一个关键词 进行匹配。

他会先分词 在查询

 tahui

 

 

 

输入标题8  他会 匹配  :标,题 , 8 的 数据

 使用 插件 基本查询

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值