[elk] 1. elasticsearch


Elasticsearchは


Apache Luceneに基づいて開発されたリアルタイム分散検索と分析エンジン

RDBMSとの違い


RDBMSElasticSearchDatabaseIndexTableTypeRowDocumentColumnFieldIndexAnalyzePrimary key_idSchemaMappingPhysical partitionShardLogical partitionRouteRelationalParent/Chille, NestedSQLQuery DSL

ドッキングステーションとしてインストールしてみます

$ docker pull docker.elastic.co/elasticsearch/elasticsearch:7.6.2

$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.6.2
9200:httpポート、9300:転送ポート

作成-/{index}/{type}/{id}


idを入力しないと、idとしてランダム文字列が生成されます。

Request
POST http://localhost:9200/database/user/1
{
  "name": "zkdlu",
  "age": 25
}
Response
{
  "_index": "database",
  "_type": "user",
  "_id": "5",
  "_version": 1,
  "result": "created",
  "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

変更-/{index}/{type{id}


Request
PUT http://localhost:9200/database/user/1
{
  "name": "zkdlu",
  "age": 24
}
Response
{
   "_index": "database",
   "_type": "user",
   "_id": "1",
   "_version": 3,
   "result": "updated",
   "_shards": {
       "total": 2,
       "successful": 1,
       "failed": 0
   },
   "_seq_no": 6,
   "_primary_term": 1
}

クエリー-/{index}/{type}/{id}


Request
GET http://localhost:9200/database/user/1
Response
{
    "_index": "database",
    "_type": "user",
    "_id": "1",
    "_version": 3,
    "_seq_no": 6,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "zkdlu",
        "age": 24
    }
}

削除-/{index}/{type}/{id}


Request
DELETE http://localhost:9200/database/user/1
Response
{
    "_index": "database",
    "_type": "user",
    "_id": "1",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

完全クエリー-/{index}/{type}/{id}/search


Request
GET http://localhost:9200/database/user/_search
Response
{
    "took": 945,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
      "hits": [
           {
               "_index": "database",
               "_type": "user",
               "_id": "2",
               "_score": 1.0,
               "_source": {
                   "name": "zkdlu2",
                   "age": 25
               }
           },
           ... 생략

クエリーのすべての条件-/{index}/{type}/{id}/search?q={key}:{value}


Request
GET localhost:9200/database/user/_search?q=name:zkdlu
Response
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0296195,
        "hits": [
            {
                "_index": "database",
                "_type": "user",
                "_id": "1",
                "_score": 1.0296195,
                "_source": {
                    "name": "zkdlu",
                    "age": 25
                }
            }
        ]
    }
}

バージョンアップ


作成後、上のAPIがElasticsearch 6であることが分かりました.xバージョンで使用されるapi.
7.xタイプ構造を削除し、Documentをdocに置き換えます.

変更されたAPIの比較


  • Search
    API6.x7.xsearch/{index}/{type}/_search{index}/_searchmsearch/{index}/{type}/_msearch/{index}/_msearchcount/{index}/{type}/_count/{index}/_countexplain/{index}/{type}/{id}/_explain/{index}/_explain/{id}search template/{index}/{type}/_search/template/{index}/_search/templatemsearch template/{index}/{type}/_msearch/template/{index}/_msearch/template

  • Document
    API6.x7.xindex{index}/{type}/{id}/{index}/_doc/{id}delete/{index}/{type}/{id}/{index}/_doc/{id}get/{index}/{type}/{id}/{index}/_doc/{id}update/{index}/{type}/{id}/_update/{index}/_update/{id}get source/{index}/{type}/{id}/_source/{index}/_source/{id}bulk/{index}/{type}/_bulk/{index}/_bulkmget/{index}/{type}/_mget/{index}/_mgettermvectors/{index}/{type}/{id}/_termvector/{index}/_termvector/{id}mtermvectors/{index}/{type}/_mtermvectors/{index}/_mtermvectors

  • Index
    API6.x7.xcreate index/{index}∏get mapping/{index}/mappingput mapping/{index}/mappingput mapping/{index}/{index}/{type}/{index}/mapping/field/{field}get template/{template/{template}