python操作es

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值