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)
    endophy
    endophy  2022-10-23, 03:02

    Herbert SvIwBcDKRJGisV 5 21 2022 lasix and potassium

    Boymmethy
    Boymmethy  2023-01-25, 20:59

    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

    torytwene
    torytwene  2023-01-28, 17:25

    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

    UgoTum
    UgoTum  2023-01-30, 05:54

    flomax generic for sale

    UgoTum
    UgoTum  2023-01-30, 13:05

    best tretinoin cream in india

    BooTum
    BooTum  2023-01-31, 05:41

    motilium tablets price

    SamTum
    SamTum  2023-01-31, 16:03

    how much is generic cipro generic malegra fxt mail order pharmacy india

    torytwene
    torytwene  2023-02-02, 04:24

    A hazard ratio HR of 0 accutane dosage calculator

    BooTum
    BooTum  2023-02-02, 05:30

    colchicine 0.6 mg price india

    WimTum
    WimTum  2023-02-02, 14:24

    where can i buy tretinoin over the counter

    BooTum
    BooTum  2023-02-04, 03:20

    flomax generic alternative

    Boymmethy
    Boymmethy  2023-02-04, 08:50

    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

    UgoTum
    UgoTum  2023-02-04, 21:56

    lasix 40 mg price in india

    WilliamFible
    WilliamFible  2023-02-05, 00:28

    cost cialis australia order viagra with paypal propranolol price disulfiram medicine amoxil price south africa zanaflex 2mg capsules otc disulfiram

    BooTum
    BooTum  2023-02-05, 03:07

    strattera 60 mg capsule

    BooTum
    BooTum  2023-02-05, 14:27

    strattera 120 mg daily

    DarrylCic
    DarrylCic  2023-02-05, 23:49

    tadalafil 100mg india medicine prednisone 10mg can i buy metformin without prescription prazosin hcl 2mg cap 370 mg prozac

    torytwene
    torytwene  2023-02-07, 00:04

    The patient is only 1- month post- op cialis super active

    JackTum
    JackTum  2023-02-08, 15:28

    nexium price south africa

    RichardSmole
    RichardSmole  2023-02-10, 09:11

    cymbalta viagra prescription coupon kamagra 100 price in india no prescription needed pharmacy cafergot over the counter bactrim 800 160 mg buy paroxetine

    EyeTum
    EyeTum  2023-02-18, 01:06

    propecia 2018 cymbalta from canada price voltaren 200 mg prozac 10 mg tablets canadian pharmacy cheap buspar online

    AlanTum
    AlanTum  2023-02-19, 02:48

    singulair for allergies cost of prozac uk levitra prescription canada furosemide medicine xenical pills

    MiclGot
    MiclGot  2023-02-20, 03:56

    strattera 18 mg capsule

    JimTum
    JimTum  2023-02-25, 14:41

    overseas online pharmacy

    ErrodaHon
    ErrodaHon  2023-03-01, 08:44

    azithromycin from amazon Preliminary observations on the effectiveness of levetiracetam in the open adjunctive treatment of refractory bipolar disorder

    Michaelarofe
    Michaelarofe  2023-03-03, 20:06

    synthroid 75 mcg tablet price

    ErrodaHon
    ErrodaHon  2023-03-10, 17:52

    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

    DarrylCic
    DarrylCic  2023-03-20, 16:51

    propranolol 10 mg over the counter

    AshTum
    AshTum  2023-03-27, 15:14

    average price of celexa

    Michaelfug
    Michaelfug  2023-03-28, 18:45

    nolvadex australia pharmacy

    AshTum
    AshTum  2023-03-28, 19:41

    citalopram prescription nz

    ZakTum
    ZakTum  2023-03-28, 23:16

    nexium 20 mg over the counter

    Elwoodbaf
    Elwoodbaf  2023-03-29, 01:11

    safe online pharmacies in canada

    MaryTum
    MaryTum  2023-03-29, 10:57

    online pharmacy birth control pills

    DarrylCic
    DarrylCic  2023-03-29, 11:18

    ampicillin capsule 500mg

    JoeTum
    JoeTum  2023-03-29, 11:44

    albuterol inhaler cost

    JackTum
    JackTum  2023-03-29, 15:10

    citalopram blood pressure

    CurtisFen
    CurtisFen  2023-03-29, 20:32

    doxycycline pills price in south africa

    WilliamFible
    WilliamFible  2023-03-29, 20:42

    order antibiotics tetracycline no prescription

    JackTum
    JackTum  2023-03-29, 21:32

    robaxin tablets australia

    SueTum
    SueTum  2023-03-29, 23:43

    amitriptyline tab 75mg

    SamTum
    SamTum  2023-03-30, 02:22

    how much is lisinopril 5 mg

    SueTum
    SueTum  2023-03-30, 05:46

    no prescription needed pharmacy

    Josephrem
    Josephrem  2023-03-30, 09:22

    canadian pharmacy in canada

    AshTum
    AshTum  2023-03-30, 09:47

    how to get misoprostol in usa

    DavisViesy
    DavisViesy  2023-03-30, 10:16

    canadian pharmacy silagra

    DarrylCic
    DarrylCic  2023-03-30, 11:25

    metformin 500mg tablets in india

    DarrylCic
    DarrylCic  2023-03-30, 14:39

    cheapest pharmacy for prescription drugs

    DarrylCic
    DarrylCic  2023-03-30, 18:49

    disulfiram cost in india

    SueTum
    SueTum  2023-03-31, 02:33

    robaxin purchase online

    Elwoodbaf
    Elwoodbaf  2023-03-31, 04:47

    zoloft medication for sale on line

    RichardSmole
    RichardSmole  2023-03-31, 06:05

    elimite over the counter medicine

    ZakTum
    ZakTum  2023-03-31, 16:41

    silkroad online pharmacy

    Elwoodbaf
    Elwoodbaf  2023-04-01, 01:20

    robaxin 500 mg tablet cost

    EyeTum
    EyeTum  2023-04-01, 04:50

    cost of lopressor 50 mg

    RichardSmole
    RichardSmole  2023-04-01, 10:44

    how to get metformin prescription

    JackTum
    JackTum  2023-04-01, 13:17

    provigil buy online australia

    JoeTum
    JoeTum  2023-04-01, 14:44

    atenolol canadian pharmacy

    EyeTum
    EyeTum  2023-04-01, 18:40

    diflucan over the counter canada

    RichardSmole
    RichardSmole  2023-04-01, 22:17

    buy trazodone online canada

    Samuelsaich
    Samuelsaich  2023-04-01, 23:21

    robaxin over the counter uk

    DarrylCic
    DarrylCic  2023-04-02, 05:27

    safe canadian pharmacies

    Josephrem
    Josephrem  2023-04-02, 14:04

    acyclovir 400 mg tablets buy

    SueTum
    SueTum  2023-04-02, 14:33

    where to get nolvadex pct

    Michaelfug
    Michaelfug  2023-04-02, 21:15

    canadian pharmacy accutane with no prescription

    SamTum
    SamTum  2023-04-03, 00:56

    which online pharmacy is the best

    MaryTum
    MaryTum  2023-04-03, 01:42

    disulfiram price in india

    AshTum
    AshTum  2023-04-03, 03:14

    trustworthy online pharmacy

    DarrylCic
    DarrylCic  2023-04-03, 05:34

    where to purchase diflucan

    MaryTum
    MaryTum  2023-04-03, 07:16

    disulfiram drug brand name

    EyeTum
    EyeTum  2023-04-03, 15:28

    order furosemide online

    Josephrem
    Josephrem  2023-04-03, 17:17

    amitriptyline 10mg online

    Samuelsaich
    Samuelsaich  2023-04-03, 18:43

    clonidine over the counter uk

    SamTum
    SamTum  2023-04-03, 19:31

    uk pharmacy online modafinil

    SueTum
    SueTum  2023-04-03, 20:15

    best tadalafil tablets in india

    IvyTum
    IvyTum  2023-04-03, 20:41

    atarax 25 mg tablet price

    Josephrem
    Josephrem  2023-04-03, 22:28

    glucophage price south africa

    DavisViesy
    DavisViesy  2023-04-04, 04:54

    erythromycin tablets in india

    DarrylCic
    DarrylCic  2023-04-04, 08:50

    atarax 25 mg prescription

    ZakTum
    ZakTum  2023-04-04, 10:05

    where can i get ciprofloxacin

    Michaelfug
    Michaelfug  2023-04-04, 10:14

    buy inderal online canada

    AlanTum
    AlanTum  2023-04-04, 20:28

    bupropion tablets 100 mg

    Davidzekly
    Davidzekly  2023-04-05, 00:40

    erythromycin cost australia

    IvyTum
    IvyTum  2023-04-05, 01:59

    flagyl no prescription

    DavisViesy
    DavisViesy  2023-04-05, 03:54

    atenolol online without prescription

    JackTum
    JackTum  2023-04-05, 05:09

    ciprofloxacin 750 mg price

    IvyTum
    IvyTum  2023-04-05, 07:49

    happy family pharmacy uk

    Josephrem
    Josephrem  2023-04-05, 08:48

    buy atarax 25mg online without rx

    WilliamFible
    WilliamFible  2023-04-05, 09:35

    pharmacy no prescription required

    JackTum
    JackTum  2023-04-05, 12:08

    lipitor canadian pharmacy

    Josephrem
    Josephrem  2023-04-05, 16:00

    flagyl online without prescription

    IvyTum
    IvyTum  2023-04-05, 17:51

    zithromax capsules australia

    DarrylCic
    DarrylCic  2023-04-05, 19:52

    tamoxifen 20 mg price in india

    Samuelsaich
    Samuelsaich  2023-04-05, 22:54

    acyclovir 200 mg tablets price

    SueTum
    SueTum  2023-04-06, 02:13

    ventolin prescription australia

    CurtisFen
    CurtisFen  2023-04-06, 04:40

    reputable online pharmacy uk

    SamTum
    SamTum  2023-04-06, 11:34

    zithromax over the counter uk

    ZakTum
    ZakTum  2023-04-06, 13:58

    clonidine price canada

    AlanTum
    AlanTum  2023-04-06, 15:03

    best online pharmacy no prescription

    ZakTum
    ZakTum  2023-04-06, 21:26

    price of accutane in india

    JackTum
    JackTum  2023-04-07, 03:53

    canadian pharmacy no prescription

    SueTum
    SueTum  2023-04-07, 03:59

    ciprofloxacin cost australia

    IvyTum
    IvyTum  2023-04-07, 11:33

    suhagra online purchase in india

    JoeTum
    JoeTum  2023-04-07, 12:14

    tadalafil over the counter usa

    DarrylCic
    DarrylCic  2023-04-07, 17:20

    dexamethasone cost india

    MaryTum
    MaryTum  2023-04-07, 21:45

    buy disulfiram online uk

    RichardSmole
    RichardSmole  2023-04-07, 21:54

    online pharmacy prescription

    EyeTum
    EyeTum  2023-04-08, 01:13

    bupropion 150 mg price in india

    AshTum
    AshTum  2023-04-08, 06:36

    express scripts com pharmacies

    DarrylCic
    DarrylCic  2023-04-08, 13:46

    happy family pharmacy coupon

    Josephrem
    Josephrem  2023-04-08, 19:43

    online pharmacy fungal nail

    DarrylCic
    DarrylCic  2023-04-08, 21:12

    canadian pharmacies not requiring prescription

    MaryTum
    MaryTum  2023-04-08, 22:47

    buy brand name prozac online

    AlanTum
    AlanTum  2023-04-09, 04:07

    online pharmacy delivery

    ZakTum
    ZakTum  2023-04-09, 10:13

    advair 250 price canada

    EyeTum
    EyeTum  2023-04-09, 13:18

    order cialis without prescription

    Elwoodbaf
    Elwoodbaf  2023-04-09, 17:50

    ventolin for sale online

    WilliamFible
    WilliamFible  2023-04-09, 18:33

    tadalafil 40 mg online india

    Josephrem
    Josephrem  2023-04-09, 18:46

    zithromax z-pak price without insurance

    IvyTum
    IvyTum  2023-04-09, 20:16

    zithromax 500mg price in india

    EyeTum
    EyeTum  2023-04-10, 08:27

    drug allopurinol 300 mg

    SamTum
    SamTum  2023-04-10, 08:31

    paxil tablet price in india

    EyeTum
    EyeTum  2023-04-10, 08:46

    canada pharmacy not requiring prescription

    IvyTum
    IvyTum  2023-04-10, 08:51

    accutane online canada pharmacy

    SamTum
    SamTum  2023-04-10, 17:32

    cymbalta generic online

    RichardSmole
    RichardSmole  2023-04-10, 20:04

    arimidex 1mg price in india