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
nolvadex australia pharmacy
citalopram prescription nz
buy sildalis
order robaxin online
biaxin otc
diflucan pills online
nexium 20 mg over the counter
buy erectafil 5
purchase nexium 40 mg
vermox uk buy online
safe online pharmacies in canada
colchicine 5mg
buy yasmin pill usa
buy erectafil 5
nexium price usa
nexium 10 mg mexico
sildalis cheap
antabuse tablets
online pharmacy birth control pills
ampicillin capsule 500mg
albuterol inhaler cost
elimite 5 cream
citalopram blood pressure
baclofen 10 mg cost
bupropion 142
buy generic indocin
nexium purple pill
feldene gel 60g
buy cheap robaxin
doxycycline pills price in south africa
order antibiotics tetracycline no prescription
robaxin tablets australia
flagyl 500 mg
nexium online canada
silagra 0.25
amitriptyline tab 75mg
how much is lisinopril 5 mg
no prescription needed pharmacy
where to get flagyl
generic elimite cream
canadian pharmacy in canada
how to get misoprostol in usa
canadian pharmacy silagra
price of zoloft
metformin 500mg tablets in india
wellbutrin buy online
antabuse online india
cheapest pharmacy for prescription drugs
levaquin from canada
erectafil 40
strattera 50 mg price
disulfiram cost in india
lexapro 40 mg
levitra india generic
how much is amoxicillin
robaxin purchase online
drug cozaar
zoloft medication for sale on line
buy fluconizole online
elimite over the counter medicine
lisinopril pill
where can i get valtrex
buy kamagra tablets uk
silkroad online pharmacy
cost of atenolol
buy generic ventolin
robaxin 500 mg tablet cost
antabuse uk pharmacy
disulfiram drug price
professional pharmacy
citalopram 60 mg
buy desyrel
arimidex cost generic
cost of lopressor 50 mg
nexium pills for sale
levaquin 750
buy vermox
how to get metformin prescription
tamoxifen coupon
provigil buy online australia
atenolol canadian pharmacy
elimite cream ebay
feldene drug
robaxin south africa
diflucan over the counter canada
elimite otc
buy trazodone online canada
buy cafergot
robaxin over the counter uk
lexapro 20mg pill
austria pharmacy online
levaquin brand name
lasix cheap
safe canadian pharmacies
canadianpharmacymeds
cafergot pills
acyclovir 400 mg tablets buy
where to get nolvadex pct
tenormin pills
canadian pharmacy accutane with no prescription
suhagra 500
vermox tablets price
which online pharmacy is the best
disulfiram price in india
inderal 80
cost of 1mg propecia
trustworthy online pharmacy
25 mg lisinopril
where to purchase diflucan
ventolin 108
disulfiram drug brand name
rx pharmacy
20 mg lipitor price
vermox 500g
ampicillin medicine
elimite otc prescription
order furosemide online
amitriptyline 10mg online
clonidine over the counter uk
uk pharmacy online modafinil
best tadalafil tablets in india
atarax 25 mg tablet price
glucophage price south africa
tizanidine 2mg cost
inderal 40 mg price
avana 50
erythromycin tablets in india
benicar coupon
robaxin otc canada
robaxin 75 mg
avodart canada price
atarax 25 mg prescription
where can i get ciprofloxacin
buy inderal online canada
flagyl no prescription
benicar 10 mg
avodart 2.5 mg
suhagra 50 tablet
lisinopril drug
strattera cap 80mg
buy tamoxifen online
purchase strattera
levaquin 250mg
synthroid 125 mg cost
bupropion tablets 100 mg
generic cialis 200mg
cheap zofran online
canadian pharmacy ltd
erythromycin cost australia
toradol generic
flagyl no prescription
atenolol online without prescription
all in one pharmacy
ciprofloxacin 750 mg price
inderal 5mg tablet
buy generic priligy uk
generic advair 2017
happy family pharmacy uk
buy atarax 25mg online without rx
pharmacy no prescription required
motilium usa
strattera drug coupon
lipitor canadian pharmacy
atarax prescription
motilium 10mg uk
flagyl online without prescription
anafranil cap 25mg
zithromax online pharmacy
atenolol 15 mg
zithromax capsules australia
cleocin 100mg cost
tamoxifen 20 mg price in india
malegra 100 tablet
acyclovir 200 mg tablets price
brand name prednisolone
generic for cozaar
flagyl 500 tablet
ventolin prescription australia
bactrim ds septra ds
avodart no prescription
reputable online pharmacy uk
avodart mexico
tadacip online
pharmacy website india
flagyl 250 mg tablet
lisinopril 5 mg pill
erectafil 10
zithromax over the counter uk
motilium medicine
cost of anafranil 75 mg
motrin 200 mg generic
fildena 100 paypal
modafinil otc
clonidine price canada
best online pharmacy no prescription
misoprostol where to buy
tadacip 100 mg
professional pharmacy
price of accutane in india
buy cheap provigil
stromectol tablets uk
canadian pharmacy no prescription
ciprofloxacin cost australia
finasteride 5mg nz
atarax price
online medicine flagyl
lopressor 25 mg
suhagra online purchase in india
tadalafil over the counter usa
citalopram libido
robaxin 500 mg tablets
dexamethasone cost india
cheap lipitor 20 mg
buy disulfiram online uk
online pharmacy prescription
zestoretic price
celebrex 400
bupropion 150 mg price in india
flagyl generic
tamoxifen uk
clonidine tab 0.1 mg
motilium 10mg canada
express scripts com pharmacies
prazosin 6 mg
generic for estrace
buy prazosin online
happy family pharmacy coupon
canada drug pharmacy
online pharmacy fungal nail
canadian pharmacies not requiring prescription
buy brand name prozac online
atenolol 50
seroquel xr 50
prazosin 1 mg price
avodart price in india
online pharmacy delivery
levaquin cheap
cleocin 150mg cost
3131 avana
levaquin generic
advair 250 price canada
order bupropion online
ivermectin topical
dapoxetine 2018
levaquin buy
order cialis without prescription
prazosin 5 mg
ventolin for sale online
tadalafil 40 mg online india
zithromax z-pak price without insurance
zithromax 500mg price in india
erythromycin 1952
zovirax cream costs
citalopram hbr 20
robaxin 1500 mg
silagra 0.25
finasteride 5mg price
drug allopurinol 300 mg
paxil tablet price in india
canada pharmacy not requiring prescription
accutane online canada pharmacy
buy robaxin 750 mg
aralen medicine price
tenormin 50
cymbalta generic online
aurogra 100 prices
zoloft cost 25mg
arimidex 1mg price in india