此文章的所有访问操作都需要使用Apifox等接口调用软件
一、基本语法操作
1.创建索引
http://192.168.87.128:9200/shopping
2.查询单个索引
http://192.168.87.128:9200/shopping
3.删除单个索引
http://192.168.87.128:9200/shopping
二、高级使用
1.文档创建
http://192.168.87.128:9200/dept/_doc
{
"id": 3,
"parentId": 0,
"deptName": "通讯部",
"orderCode": "0002",
"isStart": 1,
"address": "北京大街120号",
"description": "通讯部门"
}
2.指定id创建文档
当没有指定id时创建文档,会随机生成字符串id,如下
当需要指定创建文档的id时,只需要吧路径改成下列路径即可
http://192.168.87.128:9200/dept/_doc/1008
路径后面增加指定的id
3.全局查询
http://192.168.87.128:9200/dept/_search
4.指定id查询
http://192.168.87.128:9200/dept/_doc/1001
5.指定字段查询
5.1关键字段查询
//查询地址带有“上海”字段的数据
{
"query":{
"match":{
"address":"上海"
}
}
}
5.2查询所有
{
"query":{
"match_all":{
}
}
}
5.3查询指定字段
{
"query": {
"match_all": {}
},
"_source":["deptName","address"]
}
5.4分页查询
{
"query": {
"match_all": {}
},
"from": 0,
"size": 3
}
返回下列信息
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8, //共有8条数据
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "dept",
"_type": "_doc",
"_id": "m2A0yYAB3wdmtvVQI2j-",
"_score": 1,
"_source": {
"id": 3,
"parentId": 0,
"deptName": "通讯部",
"orderCode": "0002",
"isStart": 1,
"address": "北京大街120号",
"description": "通讯部门"
}
},
{
"_index": "dept",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"id": 1002,
"parentId": 0,
"deptName": "销售部",
"orderCode": "1002",
"isStart": 1,
"address": "北京大街110号",
"description": "销售部门"
}
},
{
"_index": "dept",
"_type": "_doc",
"_id": "1002",
"_score": 1,
"_source": {
"id": 1001,
"parentId": 0,
"deptName": "技术部",
"orderCode": "0002",
"isStart": 1,
"address": "北京大街120号",
"description": "技术部"
}
}
]
}
}
5.5排序查询
注意排序的字段必须为文档中的关键字,否则可能会报错
{
"query": {
"match_all": {}
},
"sort": {
"id":{
"order":"desc"
}
}
}
6.多条件查询
{
"query":{
"bool":{
"must":[
{
"match":{
"deptName":"销售"
}
},
{
"match":{
"address":"北京"
}
}
]
}
}
}
- bool代表条件
- must代表条件组合相当于and
{
"query":{
"bool":{
"should":[
{
"match":{
"deptName":"销售"
}
},
{
"match":{
"deptName":"技术"
}
}
]
}
}
}
- should相当于or,查询销售部和技术部数据
7. 范围查询
{
"query":{
"bool":{
"should":[
{
"match":{
"deptName":"销售"
}
},
{
"match":{
"deptName":"技术"
}
}
],
"filter":{
"range":{
"id":{
"gt":1001
}
}
}
}
}
}
- filter代表范围查询
- gt代表大约,lt代表小于