[toc]
Search API概览
1. Search API分两种
- URI Search: 在URI中使用查询参数
- Request Body Search: 使用ES提供的,基于JSON格式的更加完备的DSL:
Query Domain Specific Language 查询领域特定语言
语法 | 范围 |
---|---|
/_search | 集群上的所有index |
/index1/_search | index1上查询 |
/index1,index2/_search | index1和index2 |
/index*/_search | 通配符匹配:index开头的索引名相关的索引上查询 |
users
index里的数据一览_index | _type | _id | ▲_score | name | age | gender | birth |
---|---|---|---|---|---|---|---|
users | _doc | 1 | 1 | niewj | 36 | male | 1985-01-01 |
users | _doc | 3 | 1 | lifubo | 33 | male | |
users | _doc | 4 | 1 | weibinbin | 32 | male | |
users | _doc | 2 | 1 | nie | 26 | male | 1995-02-02 |
4.1 curl方式在命令行 使用"q", 指定查询字符串; "query string syntax", KV键值对:
curl -XGET --user username:password "http://localhost:9200/users/_search?q=name:nie"
结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.87546873,
"hits": [
{
"_index": "users",
"_type": "_doc",
"_id": "2",
"_score": 0.87546873,
"_source": {
"name": "nie",
"age": 26,
"gender": "male",
"birth": "1995-02-02"
}
}
]
}
}
4.2 在kibana DevTools里:
GET /users/_search?q=name:nie
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"name" : "nie",
"age" : 26,
"gender" : "male",
"birth" : "1995-02-02"
}
}
]
}
}
5.Request Body Search
支持
GET
、POST
5.1 curl方式在命令行
curl -XGET --user username:password "http://localhost:9200/users/_search"-H 'Content-Type:application/json' -d '{"query":{"match":{"name":"nie"}}}'
- username:替换成自己的用户名
- password:替换成自己的密码
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.87546873,
"hits": [
{
"_index": "users",
"_type": "_doc",
"_id": "2",
"_score": 0.87546873,
"_source": {
"name": "nie",
"age": 26,
"gender": "male",
"birth": "1995-02-02"
}
}
]
}
}
5.2 在kibana DevTools里
GET /users/_search
{
"query": {
"match": {
"name": "nie"
}
}
}
【实践005-elasticsearch的Search API概览】结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"name" : "nie",
"age" : 26,
"gender" : "male",
"birth" : "1995-02-02"
}
}
]
}
}
推荐阅读
- 中间件|Elasticsearch专栏-5.es基本用法-分词查询
- ElasticsearchNEST高级客户端--Mapping映射
- 实践004-elasticsearch之Index Template和Dynamic Template
- elasticsearch mapping
- ElasticSearch5.4.3离线搭建
- spring data整合elasticsearch的applicationContext.xml文件模板
- elasticsearch的mapping和analysis
- elasticsearch index 之 put mapping
- ElasticSearch添加mapping