Elastic Search 6.5.1 API 简单使用

简介

Elastic Search 就像一个数据库,只不过搜索功能比数据库强大,API 操作起来就是发送 http 请求,类似于数据库里面的写SQL语句(此处只是记录ES的简单理解和简单操作)

比如一个 ElasticSearch 请求

http://47.105.159.23:9200/manage/employee/_search

索引--index-- manage ===== 类似于MySQL里面的一个数据库

类型--type-- employee ===== 类似于MySQL里面的一张表

操作--oper-- _search ====== 类似于MySQL里面的 select * from employee

插入 Insert

添加操作一般有两种方式

  1. PUT请求,需要自己携带请求ID,将请求的数据保存在此ID下,若此ID原本存在,则将之前的数据覆盖(不推荐,不友好,我也就不演示)
  2. POST请求,只需要传递请求的数据,ES自动生成ID,插入成功后将ID返回
    Request
    POST  http://47.105.159.23:9200/manage/employee/
    Content-Type  application/json
    {
    	"name": "周益",
    	"phone": "18874791111",
    	"sex": 2,
    	"salary": 15000,
    	"post": "IOS工程师",
    	"dept": {
    		"id": "1001",
    		"name": "技术中心"
    	},
    	"join_time": "2018-10-03",
    	"desc": "a IOS boy"
    }
    
    Response
    {
        "_index": "manage",
        "_type": "employee",
        "_id": "nVZ-l2cBof4Qu3GrFc1L",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1
    }

    由于我是使用的 Postman 发送的请求,所以我的请求格式如下,JSON 格式比较直观,便于理解

查询 Search

查询就有很多种了,这里只记录最基本最简单的

  1. 没有查询条件(查询所有)
    GET  http://47.105.159.23:9200/manage/employee/_search

  2. 带查询条件

term :一般用户常量值的匹配,最常用为数值匹配

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"term": {
    		"salary": 6000
    	}
    }
}
======Response========
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oVYCmGcBof4Qu3Gr8c33",
                "_score": 1,
                "_source": {
                    "name": "青萍",
                    "phone": "18874795555",
                    "sex": 2,
                    "salary": 6000,
                    "post": "会计",
                    "dept": {
                        "id": "1002",
                        "name": "财务部门"
                    },
                    "join_time": "2018-08-03",
                    "desc": "a sunny girl"
                }
            }
        ]
    }
}

terms :用于一个字段匹配多个值时使用

GET   http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"terms": {
    		"salary": [6000, 10000]
    	}
    }
}
========Response===========
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 1,
                "_source": {
                    "name": "高节",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oVYCmGcBof4Qu3Gr8c33",
                "_score": 1,
                "_source": {
                    "name": "青萍",
                    "phone": "18874795555",
                    "sex": 2,
                    "salary": 6000,
                    "post": "会计",
                    "dept": {
                        "id": "1002",
                        "name": "财务部门"
                    },
                    "join_time": "2018-08-03",
                    "desc": "a sunny girl"
                }
            }
        ]
    }
}

range :范围查找,一般可用于数值,日期

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"range": {
    		"join_time": {
    			"lt": "2018-12-02",
    			"gt": "2018-10-02"
    		}
    	}
    }
}
===========Response=================
{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": 1,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                }
            }
        ]
    }
}

match :普通查询,一般用于文本搜索,以一个汉字或者一个单词为最小单位,比如内容为hello word,输入hello能够匹配成功,但是输入hell则匹配不上

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"match": {
    		"desc": "boy man"
    	}
    }
}
=============Response==============
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0.6931472,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oFYAmGcBof4Qu3Gr880L",
                "_score": 0.6931472,
                "_source": {
                    "name": "李岳",
                    "phone": "18874794444",
                    "sex": 2,
                    "salary": 12000,
                    "post": "Android工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a rude man"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": 0.37881336,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "olYDmGcBof4Qu3Grw837",
                "_score": 0.37881336,
                "_source": {
                    "name": "铜川",
                    "phone": "18874796666",
                    "sex": 1,
                    "salary": 16000,
                    "post": "设计UI",
                    "dept": {
                        "id": "1003",
                        "name": "设计中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a sunny boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 0.3034693,
                "_source": {
                    "name": "高节",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": 0.2876821,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架构师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                }
            }
        ]
    }
}

multi_match :多字段匹配,比如输入一个关键字从姓名和描述中查找

以下会匹配出desc或者name存在beautiful或者高节的记录

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"multi_match": {
    		"query": "beautiful 高节",
    		"fields": ["desc", "name"]
    	}
    }
}
==========================
{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.4079456,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 2.4079456,
                "_source": {
                    "name": "高节",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "n1YAmGcBof4Qu3GrB83o",
                "_score": 0.6931472,
                "_source": {
                    "name": "丽燕",
                    "phone": "18874793333",
                    "sex": 2,
                    "salary": 12000,
                    "post": "测试工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a beautiful girl"
                }
            }
        ]
    }
}

bool :过滤查询,其中可以包含多个查询条件,可以通过must,must_not,should连接

must:所有条件必须全部符合(最常用,使用频率最高)

must_not:一个都不要符合

should:至少有一个符合(若与must公用,must存在满足时,should可以不匹配成功)

以下查询性别为1,且工资大于10000,且 desc 包含 framework 的记录

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "sex": 1
                    }
                },
                {
                    "range": {
                        "salary": {
                            "gt": 10000
                        }
                    }
                },
                {
                	"match": {
                		"desc": "framework"
                	}
                }
            ]
        }
    }
}
=================================
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.287682,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": 2.287682,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架构师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                }
            }
        ]
    }
}

分页:from 起始(默认从0开始),size 读取记录数,sort 排序字段(可定义排序字段集合以及排序规则)

以下语句查询 desc 字段包含 单词 a 的记录,从第1条开始查询,返回5条记录,首先按照工资降序排序,若工资相同,然后按照入职时间降序排序

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
        "match": {
            "desc": "a"
        }
    },
    "from": 0,
    "size": 5,
    "sort": [
        {
            "salary": {
                "order": "desc"
            }
        },
        {
        	"join_time": {
        		"order": "desc"
        	}
        }
    ]
}
=====================================
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": null,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": null,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架构师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                },
                "sort": [
                    35000,
                    1520035200000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "o1ZWmGcBof4Qu3GrG81P",
                "_score": null,
                "_source": {
                    "name": "桂禄",
                    "phone": "18874797777",
                    "sex": 1,
                    "salary": 16000,
                    "post": "设计前端",
                    "dept": {
                        "id": "1003",
                        "name": "设计中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a front boy"
                },
                "sort": [
                    16000,
                    1525305600000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "olYDmGcBof4Qu3Grw837",
                "_score": null,
                "_source": {
                    "name": "铜川",
                    "phone": "18874796666",
                    "sex": 1,
                    "salary": 16000,
                    "post": "设计UI",
                    "dept": {
                        "id": "1003",
                        "name": "设计中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a sunny boy"
                },
                "sort": [
                    16000,
                    1522713600000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": null,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                },
                "sort": [
                    15000,
                    1538524800000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "n1YAmGcBof4Qu3GrB83o",
                "_score": null,
                "_source": {
                    "name": "丽燕",
                    "phone": "18874793333",
                    "sex": 2,
                    "salary": 12000,
                    "post": "测试工程师",
                    "dept": {
                        "id": "1001",
                        "name": "技术中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a beautiful girl"
                },
                "sort": [
                    12000,
                    1525305600000
                ]
            }
        ]
    }
}

修改 update

根据传入ID,修改对于的数据,只修改传入的字段,不传入的字段不操作

POST  http://47.105.159.23:9200/manage/employee/o1ZWmGcBof4Qu3GrG81P/_update
{
	"doc": {
		"salary": 50000
	}
}
=================
{
    "_index": "manage",
    "_type": "employee",
    "_id": "o1ZWmGcBof4Qu3GrG81P",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

删除 delete

DELETE 请求,传入要删除的ID即可

DELETE http://47.105.159.23:9200/manage/employee/pFZimGcBof4Qu3Gro80s
=====Response======
{
    "_index": "manage",
    "_type": "employee",
    "_id": "pFZimGcBof4Qu3Gro80s",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值