ElasticSearch 基础api语法

查询 所有索引
GET /_cat/indices


批查询 api mget
GET /_mget
{
"docs":[
{
"_index":"mc_prescription_rec",
"_type":"prescription",
"_id":"115654_2036"

},
{
"_index":"mc_prescription_rec",
"_type":"prescription",
"_id":"115654_2163"

},
{
"_index":"mc_prescription_rec",
"_type":"prescription",
"_id":"115654_2058"

}
]
}

备注:mget 如果请求url 里面有 index 和 type 后面 的 请求体里面就可以不写 index 和type

例如:

GET /mc_prescription_rec/prescription/_mget
{
"docs":[
{

"_id":"115654_2036"

},
{

"_id":"115654_2163"

},
{

"_id":"115654_2058"

}
]
}


查询 所有
GET /mc_prescription_rec/prescription/_search
{
"query":{
"match_all": {}
}
}


指定 字段 url 的查询:

GET mc_prescription_rec/prescription/_search?q=paid:1



GET /mc_prescription_rec/prescription/_search
{
"query":{
"match": {
"positive_tags": "预防"
}
}

}

范围查找

GET /mc_prescription_rec/prescription/_search
{
"query":{
"range": {
"prescription_id": {
"gte": 2000,
"lte": 2100
}
}
}

}


多条件的 复合 查询
GET /mc_prescription_rec/prescription/_search
{
"query":{
"bool": {
"must": [
{ "match": {
"status": "0"
}}
],
"should": [
{"match": {
"paid": "1"
}
},
{"match": {
"positive_tags": "健身"
}


}
],
"minimum_should_match": 1

}
}

}

GET /mc_prescription_rec/prescription/_search
{
"query":{
"bool": {
"must": [
{"match": {
"prescription_id": "2129"
}

},
{"match": {
"app_id": "313304"
}

}
]

}
}

}

备注:bool 里面的 都是一些 条件 ,must 必须瞒足,should 只要要满足 minimum_should_match 个 条件是ture ,filter 只是过滤 不计入评分


只 显示指定结果 ( _source )

GET /mc_prescription_rec/prescription/_search
{
"query": {
"match_all": {}
},
"_source": ["prescription_id","app_id"]
}


post_filter 和 query 的 区别 ,语法上没区别,唯一的在于 filter 不评分,所以 filter 比 query 快很多 ,filter 和query 可以共存。

GET /mc_prescription_rec/prescription/_search
{
"post_filter": {
"match_all": {}
},
"_source": ["prescription_id","app_id"]
}


查询非分页
GET /mc_prescription_rec/prescription/_search
{
"query":{
"match_all": {}
},
"from":1076,
"size":2

}


备注:深分页问题,效率会很低,劲量避免深分页。

  备注2:深分页:如果要查询出 每页 100 条,的第 100 页数据数据( 9900 - 10000 ),如果是去5 个节点查询,那么会在 每个节点查询出 第 9900- 10000 条数据,然后 汇总到 坐标几点,然后排序后取出 9900-10000 条,这样做非常占 资源。

查询结果范例:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 135,
"max_score": 1,
"hits": [
{
"_index": "mc_prescription_rec",
"_type": "prescription",
"_id": "313304_2169",
"_score": 1,
"_source": {
"app_id": 313304,
"prescription_id": 2169,
"status": 0,
"rcmd": 1,
"novice": 0,
"paid": 1,
"positive_tags": [
"预防心血管疾病"
],
"negative_tags": [],
"category": "",
"timestamp": 1573627618183
}
},
{
"_index": "mc_prescription_rec",
"_type": "prescription",
"_id": "316401_2165",
"_score": 1,
"_source": {
"app_id": 316401,
"prescription_id": 2165,
"status": 0,
"rcmd": 1,
"novice": 0,
"paid": 1,
"positive_tags": [
"减肥"
],
"negative_tags": [],
"category": "",
"timestamp": 1573627617081
}
}]
}}


https://www.cnblogs.com/cxygg/p/9471372.html

猜你喜欢

转载自www.cnblogs.com/yazhong-java/p/12636888.html