[toc]


Search API概览

1. Search API分两种

  • URI Search: 在URI中应用查问参数
  • Request Body Search: 应用ES提供的,基于JSON格局的更加齐备的DSL:

    Query Domain Specific Language 查问畛域特定语言

2. 指定查问的索引

语法范畴
/_search集群上的所有index
/index1/_searchindex1上查问
/index1,index2/_searchindex1和index2
/index*/_search通配符匹配:index结尾的索引名相干的索引上查问

3. 查问数据筹备:ES中的usersindex里的数据一览

_index_type_id▲_scorenameagegenderbirth
users_doc11niewj36male1985-01-01
users_doc31lifubo33male
users_doc41weibinbin32male
users_doc21nie26male1995-02-02

4. URI查问

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

反对 GETPOST

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"    }  }}

后果

{  "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"        }      }    ]  }}