ElasticSearch的restful参考文档
一. 索引库
1.1.创建索引库 Create Indexe
创建索引的请求格式:
请求方式:PUT
请求路径:/索引库名
请求参数:json格式:
PUT /douglas
{
"settings": {
"属性名": "属性值"
}
}
settings:就是索引库设置,其中可以定义索引库的各种属性,目前我们可以不设置,都走默认。
1.2、查看索引库 Get Index
请求方式:GET
请求路径:/索引库名
语法:GET /索引库名
示例:
GET /douglas
1.3、删除索引库 delete index
请求方式:DELETE
请求路径:/索引库名
语法:DELETE /索引库名
示例:
DELETE /douglas
二. 类型及映射操作
2.1、 创建映射关系
语法:
PUT /索引库名/_mapping/类型名称
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
类型名称:就是前面将的type的概念,类似于数据库中的表 字段名:任意填写,下面指定许多属性,例如:
- type:类型,可以是text、long、short、date、integer、object等
- index:是否索引,默认为true
- store:是否存储,默认为false
- analyzer:分词器,这里的ik_max_word即使用ik分词器
示例
发起请求:
PUT douglas/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
响应结果:
{
"acknowledged": true
}
上述案例中,就给douglas这个索引库添加了一个名为goods的类型,并且在类型中设置了3个字段:
- title:商品标题
- images:商品图片
- price:商品价格
2.2、查看映射关系
语法:
GET /索引库名/_mapping
查看某个索引库中的所有类型的映射。如果要查看某个类型映射,可以再路径后面跟上类型名称。即:
GET /索引库名/_mapping/映射名 或者 GET /douglas/_mapping/goods
示例:
GET /douglas/_mapping
响应:
{
"douglas": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
2.3、一次创建索引库和类型
基本语法:
put /索引库名
{
"settings":{
"索引库属性名":"索引库属性值"
},
"mappings":{
"类型名":{
"properties":{
"字段名":{
"映射属性名":"映射属性值"
}
}
}
}
}
示例:
PUT /douglas2
{
"settings": {},
"mappings": {
"goods": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
三. 文档操作
3.1、新增文档
语法:
POST /索引库名/类型/id值
{
...
}
示例:
POST /douglas/goods/2
{
"title":"大米手机",
"images":"http://image.douglas.com/12479122.jpg",
"price":2899.00
}
或者
POST /douglas/goods/
{
"title":"小米手机",
"images":"http://image.douglas.com/12479122.jpg",
"price":2699.00
}
3.2、查看文档
语法:
GET /douglas/goods/2
查看结果:
{
"_index": "douglas",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_version": 1,
"found": true,
"_source": {
"title": "小米手机",
"images": "http://image.douglas.com/12479122.jpg",
"price": 2699
}
}
3.3、修改文档
把新增的请求方式改为PUT,就是修改了。不过修改必须指定id,
- id对应文档存在,则修改
- id对应文档不存在,则新增
比如,我们把使用id为3,不存在,则应该是新增:
PUT /douglas/goods/3
{
"title":"超米手机",
"images":"http://image.douglas.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}
我们再次执行刚才的请求,不过把数据改一下:
PUT /douglas/goods/3
{
"title":"超大米手机",
"images":"http://image.douglas.com/12479122.jpg",
"price":3299.00,
"stock": 100,
"saleable":true
}
查看结果:
{
"_index": "douglas",
"_type": "goods",
"_id": "3",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
可以看到结果是:updated
,显然是更新数据
3.4、删除文档
删除使用DELETE请求,同样,需要根据id进行删除:
语法
DELETE /索引库名/类型名/id值
示例:
DELETE /douglas/goods/3
四. 文档查询
基本语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
· 查询类型:
o 例如:match_all, match,term , range 等等
4.1、查询所有(match_all)
示例:
GET /douglas/_search
{
"query":{
"match_all": {}
}
}
· query
:代表查询对象
· match_all
:代表查询所有
4.2、匹配查询(match)
match
类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
GET /douglas/_search
{
"query":{
"match":{
"title":"小米电视"
}
}
}
4.3、词条匹配(term)
term
查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串
GET /douglas/_search
{
"query":{
"term":{
"price":2699.00
}
}
}
4.4、模糊查询 (fuzzy)
我们新增一个商品:
POST /douglas/goods/4
{
"title":"apple手机",
"images":"http://image.douglas.com/12479122.jpg",
"price":6899.00
}
fuzzy
查询是 term
查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
GET /douglas/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
上面的查询,也能查询到apple手机
我们可以通过fuzziness
来指定允许的编辑距离:
GET /douglas/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":1
}
}
}
}
4.5、范围查询 (range)
range
查询找出那些落在指定区间内的数字或者时间
GET /douglas/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
range
查询允许以下字符:
操作符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
4.6、布尔组合 (bool)
bool
把各种其它查询通过must
(与)、must_not
(非)、should
(或)的方式进行组合
GET /douglas/_search
{
"query":{
"bool":{
"must": { "match": { "title": "大米" }},
"must_not": { "match": { "title": "电视" }},
"should": { "match": { "title": "手机" }}
}
}
}
结果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "douglas",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "大米手机",
"images": "http://image.douglas.com/12479122.jpg",
"price": 2899
}
}
]
}
}
4.7、结果过滤
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source
的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加_source
的过滤
4.7.1、直接指定字段
示例:
GET /douglas/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
返回的结果:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "douglas",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手机"
}
}
]
}
}
4.7.2、指定includes和excludes
我们也可以通过:
- includes:来指定想要显示的字段
- excludes:来指定不想要显示的字段
- 二者都是可选的。
- 【示例】:
GET /douglas/_search
{
"_source": {
"includes":["title","price"]
},
"query": {
"term": {
"price": 2699
}
}
}
与下面的结果将是一样的:
GET /douglas/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
4.8、查询过滤(filter)
4.8.1、条件查询中进行过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter
方式:
GET /douglas/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
4.8.2、无查询条件,直接过滤
如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score
取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
GET /douglas/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
}
4.9、排序
4.9.1、单字段排序
sort
可以让我们按照不同的字段进行排序,并且通过order
指定排序的方式
GET /douglas/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
4.9.2、多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /douglas/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000,"lt":3000}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
4.10、分页
elasticsearch的分页与mysql数据库非常相似,都是指定两个值:
- from:开始位置
- size:每页大小
GET /douglas/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"from": 2,
"size": 3
}
结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "douglas",
"_type": "goods",
"_id": "4",
"_score": null,
"_source": {
"title": "小米电视4A",
"images": "http://image.douglas.com/12479122.jpg",
"price": 3899
},
"sort": [
3899
]
}
]
}
}
4.11、高亮
高亮原理:
- 服务端搜索数据,得到搜索结果
- 把搜索结果中,搜索关键字都加上约定好的标签
- 前端页面提前写好标签的CSS样式,即可高亮
- elasticsearch中实现高亮的语法比较简单:
GET /douglas/_search
{
"query": {
"match": {
"title": "手机"
}
},
"highlight": {
"pre_tags": "<em>",
"post_tags": "</em>",
"fields": {
"title": {}
}
}
}
在使用match查询的同时,加上一个highlight属性:
- pre_tags:前置标签
- post_tags:后置标签
- fields:需要高亮的字段
- title:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空
- 【结果】:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.2876821,
"hits": [
{
"_index": "douglas",
"_type": "goods",
"_id": "2",
"_score": 0.2876821,
"_source": {
"title": "大米手机",
"images": "http://image.douglas.com/12479122.jpg",
"price": 2899
},
"highlight": {
"title": [
"大米<em>手机</em>"
]
}
},
{
"_index": "douglas",
"_type": "goods",
"_id": "JP6xa2kBtq36Pzvxpjaf",
"_score": 0.19856805,
"_source": {
"title": "小米手机",
"images": "http://image.douglas.com/12479122.jpg",
"price": 2699
},
"highlight": {
"title": [
"小米<em>手机</em>"
]
}
},
{
"_index": "douglas",
"_type": "goods",
"_id": "3",
"_score": 0.16853254,
"_source": {
"title": "超大米手机",
"images": "http://image.douglas.com/12479122.jpg",
"price": 3299,
"stock": 200,
"saleable": true,
"subTitle": "哈哈"
},
"highlight": {
"title": [
"超大米<em>手机</em>"
]
}
}
]
}
}