基础搜索

搜索文档

POST /api/:target/_search

搜索请求示例

POST http://localhost:4080/api/stackoverflow-6/_search

请求参数:

  1. {
  2. "search_type": "match",
  3. "query": {
  4. "term": "shell window",
  5. "field": "_all",
  6. "start_time": "2021-12-25T15:08:48.777Z",
  7. "end_time": "2021-12-28T16:08:48.777Z"
  8. },
  9. "sort_fields": ["-@timestamp"],
  10. "from": 0,
  11. "max_results": 20,
  12. "_source": [
  13. "Field1", "Field2" // Leave this as empty array to return all fields.
  14. ]
  15. }

搜索示例

Python 搜索示例

  1. import base64
  2. import json
  3. import requests
  4. user = "admin"
  5. password = "Complexpass#123"
  6. bas64encoded_creds = base64.b64encode(bytes(user + ":" + password, "utf-8")).decode("utf-8")
  7. params = {
  8. "search_type": "match",
  9. "query":
  10. {
  11. "term": "DEMTSCHENKO",
  12. "start_time": "2021-06-02T14:28:31.894Z",
  13. "end_time": "2021-12-02T15:28:31.894Z"
  14. },
  15. "from": 0, # use together with max_results for paginated results.
  16. "max_results": 20,
  17. "_source": [] # Leave this as empty array to return all fields.
  18. }
  19. # params = {
  20. # "search_type": "querystring",
  21. # "query":
  22. # {
  23. # "term": "+City:Turin +Silver",
  24. # "start_time": "2021-06-02T14:28:31.894Z",
  25. # "end_time": "2021-12-02T15:28:31.894Z"
  26. # },
  27. # "_source": ["_all"]
  28. # }
  29. headers = {"Content-type": "application/json", "Authorization": "Basic " + bas64encoded_creds}
  30. index = "games3"
  31. zinc_host = "http://localhost:4080"
  32. zinc_url = zinc_host + "/api/" + index + "/_search"
  33. res = requests.post(zinc_url, headers=headers, data=json.dumps(params))
  34. print(res.text)

Golang 搜素示例

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "strings"
  8. )
  9. func main() {
  10. query = `{
  11. "search_type": "match",
  12. "query":
  13. {
  14. "term": "DEMTSCHENKO",
  15. "start_time": "2021-06-02T14:28:31.894Z",
  16. "end_time": "2021-12-02T15:28:31.894Z"
  17. },
  18. "from": 0,
  19. "max_results": 20,
  20. "_source": []
  21. }`
  22. req, err := http.NewRequest("POST", "http://localhost:4080/api/games3/_search", strings.NewReader(query))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. req.SetBasicAuth("admin", "Complexpass#123")
  27. req.Header.Set("Content-Type", "application/json")
  28. req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
  29. resp, err := http.DefaultClient.Do(req)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. defer resp.Body.Close()
  34. log.Println(resp.StatusCode)
  35. body, err := io.ReadAll(resp.Body)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. fmt.Println(string(body))
  40. }

响应示例

  1. {
  2. "took": 0,
  3. "timed_out": false,
  4. "max_score": 7.6978611753656345,
  5. "hits": {
  6. "total": {
  7. "value": 3
  8. },
  9. "hits": [
  10. {
  11. "_index": "games3",
  12. "_type": "games3",
  13. "_id": "bd3e67f0-679b-4aa4-b0f5-81b9dc86a26a",
  14. "_score": 7.6978611753656345,
  15. "@timestamp": "2021-10-20T04:56:39.000871Z",
  16. "_source": {
  17. "Athlete": "DEMTSCHENKO, Albert",
  18. "City": "Turin",
  19. "Country": "RUS",
  20. "Discipline": "Luge",
  21. "Event": "Singles",
  22. "Gender": "Men",
  23. "Medal": "Silver",
  24. "Season": "winter",
  25. "Sport": "Luge",
  26. "Year": 2006
  27. }
  28. },
  29. {
  30. "_index": "games3",
  31. "_type": "games3",
  32. "_id": "230349d9-72b3-4225-bac7-a8ab31af046d",
  33. "_score": 7.6978611753656345,
  34. "@timestamp": "2021-10-20T04:56:39.215124Z",
  35. "_source": {
  36. "Athlete": "DEMTSCHENKO, Albert",
  37. "City": "Sochi",
  38. "Country": "RUS",
  39. "Discipline": "Luge",
  40. "Event": "Singles",
  41. "Gender": "Men",
  42. "Medal": "Silver",
  43. "Season": "winter",
  44. "Sport": "Luge",
  45. "Year": 2014
  46. }
  47. },
  48. {
  49. "_index": "games3",
  50. "_type": "games3",
  51. "_id": "338fea31-81f2-4b56-a096-b8294fb6cc92",
  52. "_score": 7.671309826309841,
  53. "@timestamp": "2021-10-20T04:56:39.215067Z",
  54. "_source": {
  55. "Athlete": "DEMTSCHENKO, Albert",
  56. "City": "Sochi",
  57. "Country": "RUS",
  58. "Discipline": "Luge",
  59. "Event": "Mixed Relay",
  60. "Gender": "Men",
  61. "Medal": "Silver",
  62. "Season": "winter",
  63. "Sport": "Luge",
  64. "Year": 2014
  65. }
  66. }
  67. ]
  68. },
  69. "buckets": null,
  70. "error": ""
  71. }

将 “from” 和 “max_results” 结合以允许分页。

sort_fields: 用于对结果进行排序的字段列表。在字段前加上减号”-“以更改为降序排序。

search_type可以具有以下值:

  • matchall
  • match
  • matchphrase
  • term
  • querystring
  • prefix
  • wildcard
  • fuzzy
  • daterange