實踐005-elasticsearch的Search API概覽

niewj發表於2022-05-04

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

相關文章