ElasticSearchは素早く入門教程を学びます.

12851 ワード

原文のリンク:http://tabalt.net/blog/elasti...
最も人気があり、最も活力のある全文検索エンジンシステムとして、ElasticSearchは拒否できない魅力を持っています.迅速にプロジェクトの中に集めて、大容量のデータを保存、検索、分析することができます.本文はゼロから始めます.ElasticSearchを体験してみます.
ダウンロード&インストール&ElasticSearchを起動します.
ElasticSearch公式サイトのダウンロードページを開きます.https://www.elastic.co/downlo... それぞれのバージョンのダウンロードアドレスを取得できます.インストールをダウンロードしてElasticSearchを起動します.
cd ~/soft/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.3.zip
unzip elasticsearch-5.6.3.zip

cd elasticsearch-5.6.3
./bin/elasticsearch     #   -d             
なお、上記の例でダウンロードしたElasticSearch 5.6.3は、Javaバージョンが8以上であることを要求しています.Javaまたはバージョンがインストールされていない場合、先に更新してから実行する必要があります.また、ElasticSearchは機器の配置要求も高いです.
コマンドラインで使用./bin/elasticsearch起動に成功したかどうかをテストできます.正常出力は以下の通りです.
{
  "name" : "8Low6xs",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "CAMAT2P2QS-UnI32tB53_A",
  "version" : {
    "number" : "5.6.3",
    "build_hash" : "1a2f265",
    "build_date" : "2017-10-06T20:33:39.012Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}
ElasticSearch RESTful API
ElasticSearchはJson形式のHTTPベースRESTful APIを提供しています.C URLコマンドを通じて直接要求してもいいし、任意のプログラミング言語でも非常に簡単に使用できます.公式に提供された常用言語クライアントは、https://www.elastic.co/guide/... ダウンロードを検索します
インターフェース要求
要求フォーマット:
curl -X  '://:/?' -d ''
パラメータ
説明
VERB
HTTP方法:curl 'http://localhost:9200/?pretty'GETPOSTPUTHEADPROTOCOL
httpまたはhttps
HOST
クラスタ内の任意のノードのホスト名
PORT
ポート番号は、デフォルトは9200です.
PATH
APIの端末パス
QUERY_STRING
任意のオプションのクエリー文字列パラメータ
BODY
JSON形式の要求体(必要であれば)
要求例:
curl -X GET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
インターフェース応答
Elasticsearchインターフェースは、一つのHTTP状態コード(例えば、DELETE)と一つのJSONフォーマットの戻り値(200 OK要求は除く)を返します.上のC URL要求は下記のようなJSON体に戻ります.
{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  }
}
状態コードを表示するにはHEADコマンドのcurlパラメータが使用できます.
ElasticSearch記憶構造と概念
文書Dcument
Elasticsearchはドキュメント向けで、JSONを使用して、プログレッシブフォーマットとしてオブジェクト全体を格納します.userオブジェクトドキュメントの例は以下の通りです.
{
    "email":      "[email protected]",
    "first_name": "John",
    "last_name":  "Smith",
    "info": {
        "bio":         "Eco-warrior and defender of the weak",
        "age":         25,
        "interests": [ "dolphins", "whales" ]
    },
    "join_date": "2014/05/01"
}
実際に記憶された文書は、文書のメタデータ、メタデータの共通要素も含まれています.
元素
説明
_.index
ドキュメントはどのインデックスに保存されますか?
_.タイプ
文書オブジェクトの種類
_.id
ドキュメント固有の識別情報
_.version
データバージョン
注意:TypeはIndexの仮想論理グループに過ぎません.Typeは似たような構造を持つべきです.6.x版は各IndexにTypeを含めるだけで、7.x版は完全にTypeを削除します.
インデックス
インデックス(Index)はElasticSearchで多義語です.
  • 1、データベースの概念に似た記憶文書の集合の場所を索引(名詞)
  • 2、Elasticsearchにデータを格納する行為をインデックス(動詞)ともいう
  • 3、データ検索速度を上げるために使用される逆配列インデックス構造
  • ElasticSearchはデフォルトでインデックス(1)の各ドキュメントの属性ごとに逆配列インデックス(3)を作成し、迅速に検索することができます.
    ノードノードノードノードノードノードノードノードClausterとスライスShards
    ElasticSearchは分散データベースであり、複数のサーバが協働することができ、各サーバが複数のインスタンスを実行することができる.単一の例はノードと呼ばれ、ノードのセットはクラスタを構成する.スライスは下の作業ユニットであり、ドキュメントはスライス内に保存され、スライスはまたクラスタ内の各ノードに割り当てられ、各スライスはすべてのデータの一部のみを保存する.
    ElasticSearchにおける添削の基本操作
    私たちはwecompany社の従業員情報管理を例にとってElasticSearchの基本操作を学びます.
    索引ドキュメント
    wecompanyという名前の索引にemployeeというタイプの3つの従業員情報を添付する文書:
    curl -X PUT 'http://localhost:9200/wecompany/employee/1?pretty' -d '
    {
        "first_name" : "John",
        "last_name" :  "Smith",
        "age" :        25,
        "about" :      "I love to go rock climbing",
        "interests": [ "sports", "music" ]
    }
    '
    curl -X PUT 'http://localhost:9200/wecompany/employee/2?pretty' -d '
    {
        "first_name" :  "Jane",
        "last_name" :   "Smith",
        "age" :         32,
        "about" :       "I like to collect rock albums",
        "interests":  [ "music" ]
    }
    '
    curl -X PUT 'http://localhost:9200/wecompany/employee/3?pretty' -d '
    {
        "first_name" :  "Douglas",
        "last_name" :   "Fir",
        "age" :         35,
        "about":        "I like to build cabinets",
        "interests":  [ "forestry" ]
    }
    '
    文書を検索
    IDが1の文書を取得:
    curl -X GET 'http://localhost:9200/wecompany/employee/1?pretty'
    
    {
      "_index" : "wecompany",
      "_type" : "employee",
      "_id" : "1",
      "_version" : 1,
      "found" : true,
      "_source" : {
        "first_name" : "John",
        "last_name" : "Smith",
        "age" : 25,
        "about" : "I love to go rock climbing",
        "interests" : [
          "sports",
          "music"
        ]
      }
    }
    名字-iの従業員情報を検索する:
    curl -X GET 'http://localhost:9200/wecompany/employee/_search?q=last_name:Smith&pretty'
    
    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2,
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "wecompany",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.2876821,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          },
          {
            "_index" : "wecompany",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 0.2876821,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          }
        ]
      }
    }
    検索式を使って名字Smithの従業員情報を検索します.
    curl -X GET 'http://localhost:9200/wecompany/employee/_search?pretty' -d '
    {
        "query" :  {
            "match" : {
                "last_name" : "Smith"
            }
        }
    }
    '
    
    #       
    名字はSmith年齢30以上の複雑な条件で従業員情報を検索する:
    curl -X GET 'http://localhost:9200/wecompany/employee/_search?pretty' -d '
    {
        "query" :  {
            "bool" : {
                "must" : {
                    "match" : {
                        "last_name" : "Smith"
                    }
                },
                "filter": {
                    "range" : {
                        "age" : { "gt" : 30 } 
                    }
                }
            }
        }
    }
    '
    
    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "wecompany",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.2876821,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    ロッククライミングが好きな従業員の情報を全文検索します.
    curl -X GET 'http://localhost:9200/wecompany/employee/_search?pretty' -d '
    {
        "query" :  {
            "match" : {
                "about" : "rock climbing"
            }
        }
    }
    '
    
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2,
        "max_score" : 0.53484553,
        "hits" : [
          {
            "_index" : "wecompany",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 0.53484553,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index" : "wecompany",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.26742277,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    
    また、上述した要求の「match」を「matchubrase」に置き換えると、フレーズ「rock climbing」の結果と正確に一致することができる.「query」同級に「highlight」パラメータを追加すると、結果にマッチするキーワードをSmithラベルで表示できます.
    {
    "query" :{ ... }
    "highlight" : {
            "fields" : {
                "about" : {}
            }
        }
    }
    集約分析従業員の興味:
  • 関連フィールドの分析機能を先に有効にする
  • curl -X PUT 'http://localhost:9200/wecompany/_mapping/employee?pretty' -d '
    {
        "properties": {
            "interests": { 
                "type":     "text",
                "fielddata": true
            }
        }
    }
    '
    
    {
      "acknowledged" : true
    }
  • 集計結果の照会
  • curl -X GET 'http://localhost:9200/wecompany/employee/_search?pretty' -d '
    {
        "aggs": {
            "all_interests": {
                "terms": { "field": "interests" }
            }
        }
    }
    '
    
    {
      "took" : 33,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 1.0,
        "hits" : [
          ...
        ]
      },
      "aggregations" : {
        "all_interests" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "music",
              "doc_count" : 2
            },
            {
              "key" : "forestry",
              "doc_count" : 1
            },
            {
              "key" : "sports",
              "doc_count" : 1
            }
          ]
        }
      }
    }
    文書を更新
    IDが2の文書を更新するには、再度PUTを行うだけでいいです.
    curl -X PUT 'http://localhost:9200/wecompany/employee/2?pretty' -d '
    {
        "first_name" :  "Jane",
        "last_name" :   "Smith",
        "age" :         33,
        "about" :       "I like to collect rock albums",
        "interests":  [ "music" ]
    }
    '
    文書を削除
    curl -X DELETE 'http://localhost:9200/wecompany/employee/1?pretty'
    おわりに
    今、ElasticSearchのインストールと簡単な概念は大体分かりましたが、ここで立ち止まらないでください.ElasticSearchは深い内包と豊かな機能を持っています.公式文書は最新の最高の学習資料です.次のページを開けばそれが得られます.
    https://www.elastic.co/guide/...
    原文のリンク:http://tabalt.net/blog/elasti...