elasticsearch 7.9参考文档翻译选翻

基本概念

Node 与 Cluster

linux用cURL命令交互

curl -X<VERB> '://:/?' -d ''

< VERB>
GET, POST, PUT, HEAD, or DELETE.

HTTPS 或 HTTP

The hostname of any node in your Elasticsearch cluster.

defaults to 9200.

such as _cluster/stats or _nodes/stats/jvm.

Any optional query-string parameters.

A JSON-encoded request body (if necessary).

设置索引

PUT /customer/_doc/1
{
     
  "name": "John Doe"
}

This request automatically creates the customer index if it doesn’t already exist, adds a new document that has an ID of 1, and stores and indexes the name field.

如果customer索引不存在则这个请求自动创建它,添加一个ID为1的新文档,并存储和索引name字段。

The new document is available immediately from any node in the cluster. You can retrieve it with a GET request that specifies its document ID:

可以从集群中的任何节点立即获得新文档。您可以使用指定其文档ID的GET请求检索它

GET /customer/_doc/1

查询

将一些数据输入到Elasticsearch索引后,可以通过向_search端点发送请求来搜索它。要访问完整的搜索功能套件,可以使用Elasticsearch查询DSL在请求主体中指定搜索条件。您可以在请求URI中指定要搜索的索引的名称。

例如,以下请求检索bank索引中按账号排序的所有文档:

GET /bank/_search
{
     
  "query": {
      "match_all": {
     } },
  "sort": [
    {
      "account_number": "asc" }
  ]
}

默认情况下,响应的hits部分包括匹配搜索条件的前10个文档:

{
     
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
     
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
     
    "total" : {
     
        "value": 1000,
        "relation": "eq"
    },
    "max_score" : null,
    "hits" : [ {
     
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "0",
      "sort": [0],
      "_score" : null,
      "_source" : {
     "account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"[email protected]","city":"Hobucken","state":"CO"}
    }, {
     
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "1",
      "sort": [1],
      "_score" : null,
      "_source" : {
     "account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL"}
    }, ...
    ]
  }
}

该响应还提供了以下有关搜索请求的信息:

  • took – 查询时间,毫秒
  • timed_out --请求是否超时
  • _shards – 搜索了多少个碎片,以及有多少碎片成功、失败或跳过的细分。
  • max_score – 找到的最相关文件的分数
  • hits.total.value – 找到了多少匹配的文档
  • hits.sort – 文档的排序位置(当不按相关性评分排序时)
  • hits._score – 文档的相关性得分(在使用match_all时不适用)

每个搜索请求都是自包含的:Elasticsearch不会跨请求维护任何状态信息。要分页搜索结果,请在请求中指定from和size参数。

GET /bank/_search
{
     
  "query": {
      "match_all": {
     } },
  "sort": [
    {
      "account_number": "asc" }
  ],
  "from": 10,
  "size": 10
}

要在字段中进行条件搜索,可以使用match查询。
例如,下面的请求搜索address字段以查找地址包含mill或lane的客户:

GET /bank/_search
{
     
  "query": {
      "match": {
      "address": "mill lane" } }
}

要执行短语搜索而不是匹配单个条件,可以使用match_phrase而不是match。例如,下面的请求只匹配包含短语mill lane的地址:

GET /bank/_search
{
     
  "query": {
      "match_phrase": {
      "address": "mill lane" } }
}

要构造更复杂的查询,可以使用bool查询组合多个查询条件。您可以指定标准:(must match)、(should match)或(must not match)。

例如,以下请求在bank索引中搜索属于40岁客户的账户,但不包括居住在爱达荷州的客户(ID):

GET /bank/_search
{
     
  "query": {
     
    "bool": {
     
      "must": [
        {
      "match": {
      "age": "40" } }
      ],
      "must_not": [
        {
      "match": {
      "state": "ID" } }
      ]
    }
  }
}

bool查询中的每个must、should和must_not元素被称为查询子句。文档满足每个must或should子句中的标准的程度将决定文档的相关性得分。得分越高,文档越符合您的搜索条件。默认情况下,Elasticsearch返回按相关度评分排序的文档。

must_not子句中的条件被视为筛选器。它影响结果中是否包含文档,但不影响如何对文档进行评分。您还可以显式地指定任意过滤器,以基于结构化数据包含或排除文档。

例如,下面的请求使用范围筛选器将结果限制为余额在$20,000到$30,000之间的账户。

GET /bank/_search
{
     
  "query": {
     
    "bool": {
     
      "must": {
      "match_all": {
     } },
      "filter": {
     
        "range": {
     
          "balance": {
     
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

聚合查询

Elasticsearch 聚合使您能够获得关于搜索结果的元信息,并回答诸如“德克萨斯州有多少账户持有者?”或“田纳西州的平均账户余额是多少?”您可以在一个请求中搜索文档、筛选命中结果和使用聚合来分析结果。

例如,以下请求使用terms聚合将bank索引中的所有帐户按州分组,并按递减顺序返回帐户最多的10个州:

GET /bank/_search
{
     
  "size": 0,
  "aggs": {
     
    "group_by_state": {
     
      "terms": {
     
        "field": "state.keyword"
      }
    }
  }
}

响应中的bucketstate字段的值。doc_count显示每个州的帐户数量。例如,您可以看到ID (Idaho)中有27个帐户。因为请求集size=0,所以响应只包含聚合结果。

{
     
  "took": 29,
  "timed_out": false,
  "_shards": {
     
    "total": 5,
    "successful": 5,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
     
     "total" : {
     
        "value": 1000,
        "relation": "eq"
     },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
     
    "group_by_state" : {
     
      "doc_count_error_upper_bound": 20,
      "sum_other_doc_count": 770,
      "buckets" : [ {
     
        "key" : "ID",
        "doc_count" : 27
      }, {
     
        "key" : "TX",
        "doc_count" : 27
      }, {
     
        "key" : "AL",
        "doc_count" : 25
      }, {
     
        "key" : "MD",
        "doc_count" : 25
      }, {
     
        "key" : "TN",
        "doc_count" : 23
      }, {
     
        "key" : "MA",
        "doc_count" : 21
      }, {
     
        "key" : "NC",
        "doc_count" : 21
      }, {
     
        "key" : "ND",
        "doc_count" : 21
      }, {
     
        "key" : "ME",
        "doc_count" : 20
      }, {
     
        "key" : "MO",
        "doc_count" : 20
      } ]
    }
  }
}

您可以结合聚合构建更复杂的数据的总结。例如,下面的请求在前一个group_by_state聚合中嵌套了一个avg聚合以计算每个州的平均账户余额。

GET /bank/_search
{
     
  "size": 0,
  "aggs": {
     
    "group_by_state": {
     
      "terms": {
     
        "field": "state.keyword"
      },
      "aggs": {
     
        "average_balance": {
     
          "avg": {
     
            "field": "balance"
          }
        }
      }
    }
  }
}

您可以通过指定terms聚合中的顺序,使用嵌套聚合的结果进行排序,而不是按计数对结果进行排序:

GET /bank/_search
{
     
  "size": 0,
  "aggs": {
     
    "group_by_state": {
     
      "terms": {
     
        "field": "state.keyword",
        "order": {
     
          "average_balance": "desc"
        }
      },
      "aggs": {
     
        "average_balance": {
     
          "avg": {
     
            "field": "balance"
          }
        }
      }
    }
  }
}

你可能感兴趣的:(elasticsearch 7.9参考文档翻译选翻)