Python调用Elasticsearch相关
一、安装
pip install elasticsearch
二、一个小封装类
#索引类
class ElasticSearchClient(object):
# TODO:实例和事务化单个node,若需要多个node,需要重构代码
def __init__(self, filepath="app/conf/conf.ini"):
#读取es配置
conf=configparser.ConfigParser()
conf.read(filepath,encoding='utf-8')
# TODO:传参
self.es_servers = [{
"host": conf.get('Elasticsearch','url'),
"port": conf.get('Elasticsearch','port')
}]
# http_auth是对设置了安全机制的es库需要写入 账号与密码,如果没有设置则不用写这个参数
self.es_client = elasticsearch.Elasticsearch(hosts=self.es_servers,http_auth=("xxx", "xxxxx"))
# TODO:进行创建一个数据库,即index
def create_index(self, index_name):
self.es_client.indices.create(index=index_name)
# TODO:指定map创建一个数据库
def createindex_by_map(self,index_name,map):
self.es_client.indices.create(index=index_name,body=map)
# TODO:进行删除一个数据库,即index
def delete_es_index(self, index_name):
self.es_client.indices.delete(index=index_name)
# 数据库不用进入,也不用退出。
class LoadElasticSearch(object):
# TODO:对单个index进行增删改查
def __init__(self, index, doc_type='docx'):
# TODO:输入单个index的名称
self.index = index
self.doc_type = doc_type
try:
self.es_client = ElasticSearchClient().es_client
except Exception as e:
print(e)
print('连接es失败,请查看是否连接。')
if not self.es_client.indices.exists(index=index):
# 创建Index
self.es_client.indices.create(index=self.index)
def set_index_mapping(self, set_mappings):
# TODO:设置mapping结构
"""
设置index的mapping,类似于表结构。
注意!!!!现在仅仅对mapping中的properties参数,其他的参数还很多
前提为:已有index,并且已自定义分词器,详情见https://blog.csdn.net/u013905744/article/details/80935846
输入参数举例说明:
set_mappings = {"answer": {
"type": "string",
"index": "not_analyzed"
},
"answerAuthor": {
"type": "string"
},
"answerDate": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"//这里出现了复合类型
},
...
{...
}
}
"""
mapping = {
self.doc_type: {
"properties": set_mappings
}
}
self.es_client.indices.put_mapping(index=self.index, doc_type=self.doc_type, body=mapping)
def add_date(self, row_obj):
"""
单条插入ES
"""
self.es_client.index(index=self.index, doc_type=self.doc_type, body=row_obj)
def add_date_bulk(self, row_obj_list):
"""
批量插入ES,输入文本格式为单条插入的list格式
"""
load_data = []
i = 1
bulk_num = 2000 # 2000条为一批
for row_obj in row_obj_list:
action = {
"_index": self.index,
"_type": self.doc_type,
"_source": row_obj
}
load_data.append(action)
i += 1
# 批量处理
if len(load_data) == bulk_num:
print('插入', i / bulk_num, '批数据')
print(len(load_data))
success, failed = bulk(self.es_client, load_data, index=self.index, raise_on_error=True)
del load_data[0:len(load_data)]
print(success, failed)
if len(load_data) > 0:
success, failed = bulk(self.es_client, load_data, index=self.index, raise_on_error=True)
del load_data[0:len(load_data)]
print(success, failed)
def update_by_id(self, row_obj):
"""
根据给定的_id,更新ES文档
:return:
"""
_id = row_obj.get("_id", 1)
row_obj.pop("_id")
self.es_client.update(index=self.index, doc_type=self.doc_type, body={"doc": row_obj}, id=_id)
def delete_by_id(self, _id):
"""
根据给定的id,删除文档
:return:
"""
self.es_client.delete(index=self.index, doc_type=self.doc_type, id=_id)
def search_by_query(self, body):
'''
根据查询的query语句,来搜索查询内容
'''
search_result = self.es_client.search(index=self.index, doc_type=self.doc_type, body=body)
return search_result
三、如何使用
1. 创建索引时指定 Mapping
我们在创建索引时,需要给创建的索引指定 Mapping,我将 Mapping 文件放入了一个 xxx.json 文件中
{
"settings": {
#设置副本数
"number_of_replicas": 1,
#设置分片
"number_of_shards": 4,
#设置分析器 我们采用ik作为tokenizer pinyin作为filter
"analysis": {
"analyzer": {
"my_analyzer":{
"type":"custom",
"tokenizer":"ik_max_word",
"filter":["pinyin_first_letter_and_full_pinyin_filter"]
}
},
"filter": {
"pinyin_first_letter_and_full_pinyin_filter": {
"type" : "pinyin",
"keep_first_letter" : "true",
"keep_full_pinyin" : "false",
"keep_none_chinese" : "true",
"keep_original" : "false",
"limit_first_letter_length" : 16,
"lowercase" : "true",
"trim_whitespace" : "true",
"keep_none_chinese_in_first_letter" : "true"
}
}
}
},
"mappings": {
"dynamic_templates": [
{
"strings":{
#设定读取到索引中是String类型就设置type为text字段采用我自己设置的分析器,并增加 keyword字段
"match_mapping_type":"string",
"mapping":{
"type":"text",
"analyzer":"my_analyzer",
"fields":{
"raw":{
"type":"keyword"
}
}
}
}
}
]
}
}
创建代码
mappath="xxxx/xxxx.json"
f=open(mappath,'r',encoding='utf-8')
#读取map
map=json.load(f)
es=ElasticSearchClient()
#创建索引
es.createindex_by_map(indexname,map=map)
2. 查询
es_client = LoadElasticSearch(indexname)
search={"query":xxxx}
res = es_client.search_by_query(one_body)
Herbert SvIwBcDKRJGisV 5 21 2022 lasix and potassium
Although the differences did not achieve statistical significance, there was a trend toward greater time in therapeutic range in the self management group 55 versus 49 buy cialis generic You deserve to have a normal pregnancy, which is what the TAC does, he told me I could carry quintuplets and my cervix would not budge
She how to arbs work must have reached the sanctuary, I know, it s not your fault, If I how to wean off blood pressure meds were you, I d choose her too, She can high blood pressure cause itchy skin suddenly couldn t control it and burst into tears buy cialis pro
arimidex capsules
motilium cost
zanaflex tablet
flomax generic for sale
best tretinoin cream in india
colchicine 0.3 mg
motilium tablets price
how much is generic cipro generic malegra fxt mail order pharmacy india
retin a 1 buy online
A hazard ratio HR of 0 accutane dosage calculator
colchicine 0.6 mg price india
where can i buy tretinoin over the counter
flomax price
1 orlistat
flomax generic alternative
motilium otc
We identified seven novel single nucleotide polymorphisms SNPs associated with endoxifen sensitivity through the expression of 10 genes using the genome wide integrative analysis canadian pharmacy cialis Health Affairs 2011; 30 1 91 9
lasix 40 mg price in india
flomax generic price
cost cialis australia order viagra with paypal propranolol price disulfiram medicine amoxil price south africa zanaflex 2mg capsules otc disulfiram
cymbalta cost 60mg
strattera 60 mg capsule
strattera 120 mg daily
colchicine brand
motilium 10
tadalafil 100mg india medicine prednisone 10mg can i buy metformin without prescription prazosin hcl 2mg cap 370 mg prozac
misoprostol 200 mcg tab
The patient is only 1- month post- op cialis super active
nexium price south africa
generic for zestril
prozac online pharmacy
cymbalta viagra prescription coupon kamagra 100 price in india no prescription needed pharmacy cafergot over the counter bactrim 800 160 mg buy paroxetine
200 mg strattera
buy generic cymbalta
buy lyrica 50 mg
propecia 2018 cymbalta from canada price voltaren 200 mg prozac 10 mg tablets canadian pharmacy cheap buspar online
singulair for allergies cost of prozac uk levitra prescription canada furosemide medicine xenical pills
albendazole otc
strattera 18 mg capsule
dexamethasone rx
overseas online pharmacy
xenical buy uk
azithromycin from amazon Preliminary observations on the effectiveness of levetiracetam in the open adjunctive treatment of refractory bipolar disorder
synthroid 75 mcg tablet price
otc flomax
best price metformin
clopidogrel 10 mg
indocin medicine
Yamshchikov, A cheap cialis generic online Genotype 1 HEV, which is responsible for the large majority of cases in all endemic countries, has never been isolated from pigs
medrol 4mg tab
neurontin cap 300mg
top online pharmacy
escrow pharmacy online
propranolol 10 mg over the counter
lyrica tablets uk
indocin 75 mg
gabapentin 40 mg
lanoxin 0.125 mg
baclofen 2 cream
anafranil generic cost
piroxicam capsules 10mg
average price of celexa