以下操作之前,先看下服务启动情况
http://127.0.0.1:9200/
版本:6.3.1
1、索引操作
操作 | 方式 | 接口 | 参数 |
---|---|---|---|
集群健康 | GET | /_cat/health?v | - |
节点列表 | GET | /_cat/nodes?v | - |
索引列表 | GET | /_cat/indices?v | - |
创建索引 | PUT | /website | - |
查看设置 | GET | /website/_settings | - |
设置索引 | PUT | /website/_settings | {"number_of_replicas": "2"} |
复制索引 | POST | /_reindex | {"source":{"index":"website"}, "dest":{"index":"test002"}} |
关闭索引 | POST | /website/_close | - |
打开索引 | POST | /website/_open | - |
删除索引 | DELETE | /website | - |
2、文档操作
操作 | 方式 | 接口 | 参数 |
---|---|---|---|
添加文档 | PUT | /website/blog/001 | {"name": "tom"} |
获取文档 | GET | /website/blog/001 | - |
检查文档是否存在 | HEAD | /website/blog/001 | - |
一次取多个 | POST | /website/blog/_mget | {"docs": [{"_id": "001"}, {"_id": "002"}] } |
搜索多个 | POST | /website/_search | {"query":{"terms": {"_id":["001", "002"]}}} |
更新文档(doc方式) | POST | /website/blog/001/_update | {"doc":{"name":"Jack" }} |
更新文档(脚本方式) | POST | /website/blog/001/_update | { "script":{"inline": "ctx._source.star += params.star", "lang":"painless","params":{ "star":100} }} |
查询更新 | POST | /website/_update_by_query | {"script":{"inline":"ctx._source.age = 23", "lang":"painless"}, "query":{"term":{"name":"Tom"} } } |
删除文档(指定ID) | DELETE | /website/blog/001 | - |
删除文档(带查询条件) | DELETE | /website | {"query":{"term":{"name":"Tom"} } } |
批量操作(多行格式) | POST | /_bulk | {"index":{ "_index": "books", "_type": "IT", "_id": "1" }}{"name": "Tom"} |
3、搜索操作
搜索查询
POST | /website/_search
操作 | 参数 |
---|---|
查看所有 | {"query":{"match_all":{} } } |
精确匹配 | {"query":{"constant_score":{"filter":{"term":{"price":549} } } } } |
词项查询(term query) | {"query":{"term":{"title":"java"} } } |
分词查询(match query) | {"query":{"match":{"title":"Core Java"} } } |
分词查询(全部匹配) | {"query":{"match":{"title":{"query":"Core Java", "operator":"and"} } } } |
顺序匹配 | {"query":{"match_phrase":{"title":"Core Java"} } } |
前缀匹配 | {"query":{"match_phrase_prefix":{"title":"Core J"} } } |
多字段搜索 | {"query":{"multi_match":{"query":"1986 deep", "fields":["title", "description"] } } } |
多词查询 | {"query":{"terms":{"title":["deep", "core"] } } } |
范围查询 | {"query":{"range":{"publish_time":{"gte":"2016-01-01", "lte":"2016-12-31", "format":"yyyy-MM-dd"} } } } |
存在字段查询 | {"query":{"exists":{"field":"author"} } } |
前缀查询 | {"query":{"prefix":{"title":"cor"} } } |
通配符查询("?“一个,”*"多个) | {"query":{"wildcard":{"title":"cor?"} } } |
正则表达式 | {"query":{"regexp":{"description":"[0-9]{4}"} } } |
模糊查询 | {"query":{"fuzzy":{"description":"1987"} } } |
复合查询(must, should, must_not, filter) | {"query":{"bool":{"must":{"term":{"title":"java"} }, "must_not":[{"term":{"title":"core"}} ] } } } |
脚本查询 | {"query":{"bool":{"must":{"script":{"script":{"inline":"doc['price'].value>500", "lang":"painless"} } } } } } |
指定排序字段 | {"query":{"term":{"title":"java"} }, "sort":[{"price":{"order":"desc"}} ] } |
4、其他操作
操作 | 方式 | 接口 | 参数 |
---|---|---|---|
分词效果 | POST | /website/_analyze | {"field":"title", "text":"Core Java", "analyzer": "ik_smart"} |
设置分词器 | PUT | /blog | {"settings" : {"index" : {"analysis.analyzer.default.type": "ik_max_word"} } } |
分页 | GET | /website/_search?size=10&from=0 | - |
高亮 | POST | /website/_search | {"query" : {"match_phrase" : {"about" : "rock climbing"} }, "highlight": {"fields" : {"about" : {} } } } |
数量 | GET | /website/_count | - |
参考