python elasticsearch环境搭建详解

  

windows下载zip
  

  

linux下载焦油
  

  

下载地址:https://www.elastic.co/downloads/elasticsearch

  

解压后运行:bin/elasticsearch(或本\ elasticsearch.bat>   #编码:utf8   从__future__进口unicode_literals   进口操作系统   导入的时间   导入配置   从datetime进口datetime   从elasticsearch进口elasticsearch   从elasticsearch。助手批量导入      类ElasticSearch ():   def __init__(自我、index_name index_type、ip=127.0.0.1):   “‘   :param index_name:索引名称   :param index_type:索引类型   “‘   自我。index_name=index_name   自我。index_type=index_type   #无用户名密码状态   # self.es=Elasticsearch ((ip))   #用户名密码状态   self.es=Elasticsearch ((ip), http_auth=(“弹性”,“密码”),端口=9200)   def create_index(自我,index_name=癴tech460 index_type=吧缜?:   “‘   创建索引,创建索引名称为奥特,类型为ott_type的索引   :param例:Elasticsearch对象   返回:   “‘   #创建映射   _index_mappings={   “映射”:{   自我。index_type: {   "属性":{   " city_code ": {   “类型”:“字符串”,   #“指数”:“not_analyzed”   },   "名称":{   “类型”:“字符串”,   #“指数”:“not_analyzed”   },   "地址":{   “类型”:“字符串”,   #“指数”:“not_analyzed”   }   }   }      }   }   如果self.es.indices.exists(指数=self.index_name)是正确的:   self.es.indices.delete(指数=self.index_name)   res=self.es.indices.create(指数=self.index_name身体=_index_mappings)   打印res      def build_data_dict(自我):   name_dict={}   打开(os.path.join (config.datamining_dir data_output, house_community.dat)) f:   f的线:   line_list=line.decode (utf - 8) .split (‘\ t’)   community_code=line_list [6]   name=line_list [7]   city_code=line_list [0]   name_dict [community_code]=(名称、city_code)      address_dict={}   打开(os.path.join (config.datamining_dir data_output, house_community_detail.dat)) f:   f的线:   line_list=line.decode (utf - 8) .split (‘\ t’)   community_code=line_list [6]   地址=line_list [10]   address_dict [community_code]=地址      返回name_dict address_dict      def bulk_index_data(自我、name_dict address_dict):   “‘   用散装将批量数据存储到   返回:   “‘   list_data=https://www.yisu.com/zixun/[]   在name_dict.items community_code,数据():   tmp={}   tmp=community_code“代码”   tmp['名字']=[0]的数据   tmp [' city_code ']=数据[1]      如果community_code address_dict:   tmp['地址']=address_dict [community_code]   其他:   tmp['地址']="      list_data.append (tmp)   行动=[]   在list_data线:   action={   “_index”: self.index_name,   “_type”: self.index_type,   “_id”:线(“代码”),# _id小区代码   " _source ": {   “city_code”:线(“city_code”),   “名称”:线(“名字”),   “地址”:线(“地址”)   }   }   ACTIONS.append(行动)   #批量处理   成功,_=散装(self.es、行动、指数=自我。index_name raise_on_error=True)   #单条写入单条写入速度很慢   # self.es.index(指数=self.index_name doc_type=癲oc_type_test”,身体=行动)      打印(% d动作执行的%成功)      def delete_index_data(自我,id):   “‘   删除索引中的一条   :param id:   返回:   “‘   res=self.es.delete(指数=self.index_name doc_type=自我。index_type id=id)   打印res      def get_data_id(自我,id):   res=self.es.get(指数=self.index_name doc_type=self.index_type id=id)   # #输出查询到的结果   打印res [' _source '] [' city_code '], res [' _id '], res (“_source”)(“名字”),res (“_source”)(“地址”)      def get_data_by_body(自我、名称、city_code):   # doc={“查询”:{“match_all”: {}}}   医生={   "查询":{   “bool”: {   “过滤器”:{   “":{   “city_code”: city_code   }   },   "必须":{   " multi_match ": {   “查询”:名字,   “类型”:“phrase_prefix”,   “字段”:['名字^ 3 ','地址'],   “污水”:1、      }      }   }   }   }   _searched=self.es.search(指数=self.index_name doc_type=自我。index_type,身体=doc)   data=https://www.yisu.com/zixun/_searched['打']['打']   返回数据         if __name__==癬_main__”:   #数据插入   obj=ElasticSearch (“ftech460”、“社区”)   obj.create_index ()   name_dict address_dict=obj.build_data_dict ()   obj.bulk_index_data (name_dict address_dict)      #从es读取数据   methoda=ElasticSearch (“ftech460”、“社区”)   obj2.get_data_by_body (u '保利’,‘510100’)

python elasticsearch环境搭建详解