from elasticsearch import Elasticsearch
es = Elasticsearch(["192.168.61.129:9200"])
body = {
"settings": {
"index": {
# 切片数量
"number_of_shards": 3,
# 备份数量+
"number_of_replicas": 2
}
},
"mappings": {
# 指定类型Type
"novel": {
"properties": {
# 名称
"name": {
"type": "text",
"analyzer": "ik_max_word",
"index": True,
"store": False
},
# 作者名称
"author": {
"type": "keyword"
},
# 字数
"count": {
"type": "long"
},
# 上架时间
"onsale": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
# 描述
"desc": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
# 这里也是使用了 ignore 参数,来忽略 Index 不存在而删除失败导致程序中断的问题。
result = es.indices.create(index='test-index', ignore=400, body=body)
result
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'test-index'}
es.info()
{'name': 'AvPhieW',
'cluster_name': 'docker-cluster',
'cluster_uuid': 'P-HHpDWzTZGvuSqNMCnr4A',
'version': {'number': '6.5.4',
'build_flavor': 'default',
'build_type': 'tar',
'build_hash': 'd2ef93d',
'build_date': '2018-12-17T21:17:40.758843Z',
'build_snapshot': False,
'lucene_version': '7.5.0',
'minimum_wire_compatibility_version': '5.6.0',
'minimum_index_compatibility_version': '5.0.0'},
'tagline': 'You Know, for Search'}
data = [
{
‘name’: ‘人间草木’,
‘author’: ‘汪曾祺’,
‘count’: 100000,
‘onsale’: ‘2017-04-01’,
‘desc’: ‘《人间草木》是汪曾祺先生的文集,他写出了草木山川、花鸟虫鱼的人味,写出了乡情民俗、凡人小事温润的乡土味;以一颗从容豁达的心写出了世间的美好与灵动。他的笔尖下总是有着一连串的惊喜:清晨薄雾里带着露珠的洁白的缅桂花,明亮、丰满而使人丰满的昆明的雨,饱涨着花骨朵的木香,自得其乐的栀子花,巷子里卖杨梅的苗族女子柔柔的声音,联大那些令人难以忘却的师友,抑或是没有大喜大忧、没有烦恼、无欲望亦无追求、天然恬淡、抱膝闲看“活庄子”般的闹市闲民。汪曾祺先生一生都对生活投入真情,那水洗般的文字有种洗涤红尘世俗的力量,赋予了作品纯真的生命力’
},
{
'name': '慢煮生活',
'author': '汪曾祺',
'count': 200000,
'onsale': '2019-09-01',
'desc': '本书为汪曾祺的散文精选集,完整收录《五味》《昆明的雨》《人间草木》《星斗其文,赤子其人》等经典名篇,同时新增《猫》《一技》《名优逸事》《和尚》《一辈古人》等罕见篇目。作者从花鸟虫鱼、乡情民俗、凡人小事、旅途见闻等多个主题出发,详尽展现了一代“生活家”汪曾祺的精神世界与生活志趣你很辛苦,很累了,那么坐下来歇一会儿,喝一杯不凉不烫的清茶,读一点我的作品。'
},
{
'name': '心安即是归处',
'author': '季羡林',
'count': 500000,
'onsale': '2020-08-01',
'desc': '季羡林经历过人生的大苦大悲,生命的跌宕起伏。然而先生的一生,不争不辩,不怨不艾,满怀天真,执着自己的执着,安于当下。是世界上仅有的精通于吐火罗文的几位学者之一,同时又用质朴的文字向世人传达一个理念——心安即是生命的归处。本书旨在阐释先生的生命智慧,从谈人生的意义到分别谈读书、处世、行走、当下、孤独、生死等跟大家密切相关的生命话题。高山仰止,景行行止。愿我们能了悟人间万相的本真,拥有应对世事的智慧。万事安然于心,从容而行。'
}
]for index, datum in enumerate(data):
es.create(index=‘test-index’, doc_type=‘novel’, id=index, body=datum)
data = {
"name": "人间草木-create",
"author": "汪曾祺",
"count": 10000,
"onsale": "2000-01-01",
"desc": "《人间草木》是汪曾祺先生的文集。"
}
es.create(index='test-index', doc_type='novel', id=1, body=data)
{'_index': 'test-index',
'_type': 'novel',
'_id': '1',
'_version': 1,
'result': 'created',
'_shards': {'total': 3, 'successful': 1, 'failed': 0},
'_seq_no': 0,
'_primary_term': 1}
data = {
"name": "人间草木-index",
"author": "汪曾祺",
"count": 10000,
"onsale": "2000-01-01",
"desc": "《人间草木》是汪曾祺先生的文集。"
}
es.index(index='test-index', doc_type='novel', body=data)
{'_index': 'test-index',
'_type': 'novel',
'_id': 'nYNtankBRd9cGn4B55kp',
'_version': 1,
'result': 'created',
'_shards': {'total': 3, 'successful': 1, 'failed': 0},
'_seq_no': 1,
'_primary_term': 1}
dsl = {
'query': {
'match': {
'desc': '人间草木'
}
}
}
es.search(index='test-index', doc_type='novel', body=dsl)
{'took': 11,
'timed_out': False,
'_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0},
'hits': {'total': 2,
'max_score': 0.36464313,
'hits': [{'_index': 'test-index',
'_type': 'novel',
'_id': '1',
'_score': 0.36464313,
'_source': {'name': '人间草木-create',
'author': '汪曾祺',
'count': 10000,
'onsale': '2000-01-01',
'desc': '《人间草木》是汪曾祺先生的文集。'}},
{'_index': 'test-index',
'_type': 'novel',
'_id': 'nYNtankBRd9cGn4B55kp',
'_score': 0.36464313,
'_source': {'name': '人间草木-index',
'author': '汪曾祺',
'count': 10000,
'onsale': '2000-01-01',
'desc': '《人间草木》是汪曾祺先生的文集。'}}]}}
es.search(index='test-index', doc_type='novel')
{'took': 1,
'timed_out': False,
'_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0},
'hits': {'total': 2,
'max_score': 1.0,
'hits': [{'_index': 'test-index',
'_type': 'novel',
'_id': '1',
'_score': 1.0,
'_source': {'name': '人间草木-create',
'author': '汪曾祺',
'count': 10000,
'onsale': '2000-01-01',
'desc': '《人间草木》是汪曾祺先生的文集。'}},
{'_index': 'test-index',
'_type': 'novel',
'_id': 'nYNtankBRd9cGn4B55kp',
'_score': 1.0,
'_source': {'name': '人间草木-index',
'author': '汪曾祺',
'count': 10000,
'onsale': '2000-01-01',
'desc': '《人间草木》是汪曾祺先生的文集。'}}]}}
dsl = {
'query': {
'match': {
'desc': '人间草木'
}
}
}
es.count(index='test-index', doc_type='novel', body=dsl)
{'count': 2,
'_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0}}
es.count(index='test-index', doc_type='novel')
{'count': 2,
'_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0}}
es.exists(index='test-index', doc_type='novel', id=1)
True
body = {
'doc': {
'name': '人间草木-create-新版'
}
}
es.update(index='test-index', doc_type='novel', id=1, body=body)
{'_index': 'test-index',
'_type': 'novel',
'_id': '1',
'_version': 2,
'result': 'updated',
'_shards': {'total': 3, 'successful': 1, 'failed': 0},
'_seq_no': 2,
'_primary_term': 1}
data = {
"name": "人间草木-index-新版",
"author": "汪曾祺",
"count": 10000,
"onsale": "2020-01-01",
"desc": "《人间草木》是汪曾祺先生的文集。"
}
# 如果要更新的文档不存在,则为新增数据
es.index(index='test-index', doc_type='novel', id='nYNtankBRd9cGn4B55kp', body=data)
# es.index(index='test-index', doc_type='novel', body=data)
{'_index': 'test-index',
'_type': 'novel',
'_id': 'nYNtankBRd9cGn4B55kp',
'_version': 2,
'result': 'updated',
'_shards': {'total': 3, 'successful': 1, 'failed': 0},
'_seq_no': 3,
'_primary_term': 1}
es.delete(index='test-index', doc_type='novel', id=1)
# 这里也是使用了 ignore 参数,来忽略 Index 不存在而删除失败导致程序中断的问题。
result = es.indices.delete(index='test-index', ignore=[400, 404])
result
{'acknowledged': True}