Python で Elasticsearch の settings と mappings を作成を実施してみます。
python 3.6で実施してみました。
事前準備
事前にライブラリのインストールを行います。
1 2 3 |
# pip install elasticsearch |
Elasticsearch へ python で接続するプログラムの作成
続いて本体のプログラムの作成です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# -*- coding:utf-8 -*- from elasticsearch import Elasticsearch def main(): # コネクション作成 elastic_con = Elasticsearch(host='localhost', verify_certs=True) mapping = ''' { "settings": { "analysis": { "tokenizer": { "sudachi_tokenizer": { "type": "sudachi_tokenizer", "mode" : "search", "discard_punctuation": true, "resources_path": "/etc/elasticsearch/sudachi", "settings_path": "/etc/elasticsearch/sudachi/sudachi.json" } }, "analyzer": { "sudachi_analyzer": { "filter": [ ], "tokenizer": "sudachi_tokenizer", "type": "custom" } } } }, "mappings": { "type_name": { "properties": { "field1": { "type": "text", "analyzer": "sudachi_analyzer" }, "field2": { "type": "text", "analyzer": "sudachi_analyzer" }, "field3": { "type": "text", "analyzer": "sudachi_analyzer" } } } } }''' # settingsとmappingsを指定してインデックスを作成 elastic_con.indices.create(index='test-index', body=mapping) if __name__ == '__main__': main() |
settingsでは前に作成したsudachi関連でトークナイザーの設定をしていますが、あまり詳細には設定入れていません。
Elasticseach への接続するためのコネクションの確立とそれ以降の設定の実施を行なっています。
あまり難しい感じではないですが、 python と elasticsearch の組み合わせの基本ですので。
★関連記事
pythonでのElasticsearchのエイリアス(ailias)の作成と確認と削除