python操作es

mac2025-08-01  2

from elasticsearch import Elasticsearch from elasticsearch import helpers es=Elasticsearch('bigdata-yace08:9200') ''' #创建索引 mappings={ "mappings":{ "data": { "properties": { "score": { "type": "float" }, "sname": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "departid": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "sage": { "type": "long" }, "birthday": { "type": "date", "format": "yyyy-MM-dd" }, "stamp": { "type": "long" }, "c@receive_time": { "type": "date", "format": "epoch_millis" }, "c@receive_time2": { "type": "date" }, "sid": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } } } } } } res = es.indices.create(index = 'index_test',body =mappings) ''' #删除一条数据 # res = es.delete(index="index_test",doc_type="data", id ="AzuWH24BfHiurkRi6H3S") ''' #写入一条数据 action={ "id":"111122", "score":78.9, "sname":"s1003", "departid":"d1001", "sage":20, "birthday":"1991-10-11", "stamp":1572423878, "c@receive_time":1572422878123, "c@receive_time2":"2019-10-30T15:51:39.934Z", "sid":"s1003" } es.index(index="index_test",doc_type="data",body=action) from elasticsearch.helpers import bulk #写入多条数据 actions=[] action1={ "_index": "index_test", "_type": "data", "_source":{ "score":90, "sname":"s1006", "departid":"d1001", "sage":39, "birthday":"1982-10-11", "stamp":1572423878, "c@receive_time":1572423878124, "c@receive_time2":"2019-10-30T15:51:39.934Z", "sid":"s1006" } } action2={ "_index": "index_test", "_type": "data", "_source": { "score": 82.9, "sname": "s1005", "departid": "d1001", "sage": 19, "birthday": "1992-10-11", "stamp": 1572423978, "c@receive_time": 1572423878125, "c@receive_time2": "2019-09-11T15:51:39.935Z", "sid": "s1005" } } actions.append(action1) actions.append(action2) res=bulk(es, actions,index='index_test',raise_on_error=True) print(res) ''' ''' #查找数据 doc={ "query": { "bool": { "must": [ { "term": { "sid.keyword": "s1001" } } ] } } } res=es.search(index="index_test",body=doc) #只打印查询出来的索引内容 print(res['hits']['hits']) print("=="*10) #打印所有 print(res) '''

 

最新回复(0)