Elasticsearch 入門ノート (1)

環境構築

  Elasticsearch は検索エンジンであり、一般的な検索ツールの 1 つです。

  Kibana は、Elasticsearch と連携するように設計されたオープンソースの分析および視覚化プラットフォームです。Kibana は、Elasticsearch インデックスに保存されているデータを検索、表示、操作する機能を提供します。開発者やオペレーターは、高度なデータ分析を簡単に実行し、さまざまなチャート、表、地図でデータを視覚化できます。

  その他のビジュアライゼーションには、elasticsearch-head (軽量、対応する Chrome プラグイン付き) などがありますが、この記事では詳しく紹介しません。

  ElasticsearchとKibanaのバージョンは7.17.0を使用し、Dockerを使用して環境を構築していますdocker-compose.yml

version: "3.1"
# 服务配置
services:
  elasticsearch:
    container_name: elasticsearch-7.17.0
    image: elasticsearch:7.17.0
    environment:
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
      - "http.host=0.0.0.0"
      - "node.name=elastic01"
      - "cluster.name=cluster_elasticsearch"
      - "discovery.type=single-node"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - ./es/plugins:/usr/share/elasticsearch/plugins
      - ./es/data:/usr/share/elasticsearch/data
    networks:
      - elastic_net

  kibana:
    container_name: kibana-7.17.0
    image: kibana:7.17.0
    ports:
      - "5601:5601"
    networks:
      - elastic_net

# 网络配置
networks:
  elastic_net:
    driver: bridge

基本的なコマンド

  • ElasticSearch が正常に開始されたかどうかを確認します。
curl http://IP:9200
  • クラスターが正常かどうかを確認する
curl http://IP:9200/_cat/health?v
  • ElasticSearch のすべてのインデックスを表示する
curl http://IP:9200/_cat/indices
  • ElasticSearch のすべてのインデックスまたはインデックスのドキュメント数を表示する
curl http://IP:9200/_cat/count?v
curl http://IP:9200/_cat/count/some_index_name?v
  • 各ノードで実行されているプラ​​グインに関する情報を表示する
curl http://IP:9200/_cat/plugins?v&s=component&h=name,component,version,description
  • ik プラグインの単語分割結果を表示する
curl -H 'Content-Type: application/json'  -XGET 'http://IP:9200/_analyze?pretty' -d '{"analyzer":"ik_max_word","text":"美国留给伊拉克的是个烂摊子吗"}'

インデックス操作

  • インデックスのマッピングを表示する
curl http://IP:9200/some_index_name/_mapping
  • インデックスのすべてのデータを表示する
curl http://IP:9200/some_index_name/_search
  • IDによるクエリ
curl -X GET http://IP:9200/索引名称/文档类型/ID
  • インデックスのすべてのデータを取得する
curl http://IP:9200/索引名称/_search?pretty
curl -X POST http://IP:9200/索引名称/_search?pretty -d "{
    
    \"query\": {
    
    \"match_all\": {} }}"
  • インデックスの最初のいくつかのデータを取得します (サイズが指定されていない場合、デフォルトは 10)
curl -XPOST IP:9200/索引名称/_search?pretty -d "{
    
    \"query\": {
    
    \"match_all\": {} }, \"size\" : 2}"
  • 特定のインデックスの中間データ(11~20番目のデータなど)を取得します。
curl -XPOST IP:9200/索引名称/_search?pretty -d "{
    
    \"query\": {
    
    \"match_all\": {} }, \"from\" : 10, \"size\" : 10}}"
  • インデックスを取得し、コンテキスト フィールドのみを返します
curl -XPOST IP:9200/索引名称/_search?pretty -d "{
    
    \"query\": {
    
    \"match_all\": {} }, \"_source\": [\"context\"]}"
  • インデックスを削除する
curl -XDELETE 'IP:9200/index_name'

ES検索

  1. 検索キーワードが複数ある場合、Elastic はそれらが OR 関係にあるとみなします。
  2. 複数のキーワードに対して AND 検索を実行する場合は、ブール クエリを使用する必要があります。
$ curl 'localhost:9200/索引名称/文档类型/_search'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "content": "软件" } },
        { "match": { "content": "系统" } }
      ]
    }
  }
}'
  1. 複雑な検索:

SQL ステートメント:

select * from test_index where name='tom' or (hired =true and (personality ='good' and rude != true ))

DSL ステートメント:

GET /test_index/_search
{
    "query": {
            "bool": {
                "must": { "match":{ "name": "tom" }},
                "should": [
                    { "match":{ "hired": true }},
                    { "bool": {
                        "must":{ "match": { "personality": "good" }},
                        "must_not": { "match": { "rude": true }}
                    }}
                ],
                "minimum_should_match": 1
            }
    }
}

トークナイザー

  ik ワード ブレーカーは、Elasticsearch 用の中国語ワード ブレーカー プラグインであり、中国語ワード ブレーカーのサポートが強化されています。ik バージョンは Elasticsearch と一致している必要があります。

  ik 7.17.0 のダウンロード アドレスは: https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.0ダウンロード後、名前を ik に変更し、Elasticsearch Down の plugins フォルダーに置きます。 。

  ik トークナイザーを使用するコマンド (Kibana 環境):

POST _analyze
{
    
    
  "text": "戚发轫是哪里人",
  "analyzer": "ik_smart"
}

出力は次のとおりです。

{
    
    
  "tokens" : [
    {
    
    
      "token" : "戚",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
    
    
      "token" : "发轫",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
    
    
      "token" : "是",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
    
    
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}

  ik は、ユーザー辞書とストップワードのロードをサポートしています。ik は構成ファイル IKAnalyzer.cfg.xml (ik/config パスの下に置きます) を提供します。これを使用して、独自の拡張ユーザー辞書、ストップワード辞書、およびリモート拡張ユーザー辞書を構成でき、複数を構成できます。

  拡張ユーザー辞書とリモート拡張ユーザー辞書を設定した後は、ES を再起動する必要があります。後でユーザー辞書を更新する場合は、ES を再起動する必要があります。リモート拡張ユーザー辞書の設定後は、ホット アップデートとチェックがサポートされます60 秒ごとに更新します。両方の拡張辞書は ik のメイン辞書に追加され、すべてのインデックスに対して有効になります。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">custom/mydict.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
	<!--用户可以在这里配置远程扩展字典 -->
	<!-- <entry key="remote_ext_dict">words_location</entry> -->
	<!--用户可以在这里配置远程扩展停止词字典-->
	<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

  ユーザー辞書ファイルのパスはcustom/mydict.dic、ストップワード辞書のパスはcustom/ext_stopword.dicで、これらをik/config/customパスの下に置きます。

  「Qi Fashi」をユーザー辞書ファイルに追加し、「yes」をストップワード辞書に追加して、元のテキストをセグメント化します。

POST _analyze
{
    
    
  "text": "戚发轫是哪里人",
  "analyzer": "ik_smart"
}

出力は次のとおりです。

{
    
    
  "tokens" : [
    {
    
    
      "token" : "戚发轫",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
    
    
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

  「analyzer」が ik_smart を選択した場合、テキストは最も粗い粒度で分割され、ik_max_word が選択された場合、テキストは最も細かい粒度で分割されます。テストは次のとおりです。

POST _analyze
{
    
    
  "text": "戚发轫是哪里人",
  "analyzer": "ik_max_word"
}

出力は次のとおりです。

{
    
    
  "tokens" : [
    {
    
    
      "token" : "戚发轫",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
    
    
      "token" : "发轫",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
    
    
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
    
    
      "token" : "哪里",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
    
    
      "token" : "里人",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

要約する

  この記事は主にElasticsearchの基本的なコマンドや使い方を紹介するもので、今後も継続的に更新していく筆者のElasticsearch勉強記の第一弾です。

この記事のコードは、Github (https://github.com/percent4/ES_Learning   ) に配置されています

おすすめ

転載: blog.csdn.net/jclian91/article/details/131996556