python es操作摘要(pymongo增/查)

連接服務端

    def connect(self):
        try:
            # es.Transport(hosts=,connection_pool_class=es.ConnectionPool(dead_timeout=5))todo:es連接池
            self.es = es.Elasticsearch(hosts=self.ip+":"+self.port,timeout=self.timeout)
            self.es.ping()
        except Exception as e:
            print(e)

單條插

    def insert_one(self,type,data_dict):
        try:
            result = self.es.index(index=self.now_index,doc_type = type,body = data_dict,refresh = True)
            print(result)
        except Exception as e:
            print(e)

批量插

    """
    直接在data_dict_list中指定每個需要插入文檔的index和type
    """
    def insert_many(self,data_dict_list):
        try:
            helpers.bulk(self.es,actions=data_dict_list)
        except Exception as e:
            print(e)

    """
    type: es type指定
    body: 包含es DSL查詢語法
    size: 返回的結果條數
    """
    def search(self,type,body,size):
        try:
            return self.es.search(index=self.now_index,doc_type=type,body=body,size=size,filter_path=["hits.hits._*"])
        except Exception as e:
            print(e)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章