Elasticsearch 入门:简单实例

下载直接pip install就行

直接挂简单样例代码

import elasticsearch

import os
import re
import json
import time

from elasticsearch.helpers import bulk
#
# def es_login(host="localhost", port="9200"):
#     return elasticsearch.Elasticsearch(hosts=[{"host": host, "port": port}])
#
#
#
# print(es_login())

from elasticsearch import Elasticsearch

# Connect to the elastic cluster



from elasticsearch import Elasticsearch
es=Elasticsearch([{host:localhost,port:9200}])
ssr1={
    "first_name" : "八岐",
    "last_name" :  "大蛇",
    "age" :        648,
    "about" :      "求求你别再刷魂土了",
    "interests": [ "把队友变蛇", "篡位" ]
}


#余下代码为写入三段数据
sp1={
    "first_name" :  "鬼王",
    "last_name" :   "酒吞童子",
    "age" :         998,
    "about" :       "我不是男同",
    "interests":  [ "喝酒" ]
}

ssr2={
    "first_name" :  "唤莲",
    "last_name" :   "帝释天",
    "age" :         25,
    "about":        "让你在新的世界里洗掉自己的罪孽吧",
    "interests":  [ "阿修罗" ]
}



res1 = es.index(index="megacorp", doc_type=employee, id=1,body=ssr1)
res2 = es.index(index="megacorp", doc_type=employee, id=2,body=sp1)
res3 = es.index(index="megacorp", doc_type=employee, id=3,body=ssr2)

test={
    "query" : {
               "match" : {"last_name" : "帝释天" }
                }
     }

print("1")

rt1= es.search(index="megacorp", body=test)
# print(rt1[‘hits‘][‘hits‘])
for list in rt1[hits][hits]:
    print (list)
# print(type(rt1[‘hits‘][‘hits‘]))



# e1={
# "first_name":"nitin",
# "last_name":"panwar",
# "age": 27,
# "about": "Love to play cricket",
# "interests": [‘sports‘,‘music‘],}
#
# print(e1)

#现在让我们将此文档存储在Elasticsearch

# res = es.index(index =‘megacorp‘,doc_type =‘employee‘,id = 1,body = e1)


# def AddDatas():
#     es = Elasticsearch([{‘host‘:‘localhost‘,‘port‘:9200}])
#     datas = [{
#             ‘name‘: ‘美国留给伊拉克的是个烂摊子‘,
#             ‘addr‘: ‘http://view.news.qq.com/zt2011/usa_iraq/index.htm‘
#             },{
#             "name":"python",
#             "addr":‘四川省‘
#             }]
#     for i,data in enumerate(datas):
#         es.create(index="my_index",doc_type="test_type",
#           id=i,ignore=[400,409],body=data)
#     # 查询结果
#     result = es.get(index="my_index",doc_type="test_type",id=0)
#     print(‘\n批量插入数据完成:\n‘,result[‘_source‘])
#
# AddDatas()
# print(2)
# def ParaSearch():
#     es = Elasticsearch([{‘host‘:‘localhost‘,‘port‘:9200}])
#     query1 = es.search(index="my_index", body={"query":{"match_all":{}}})
#     print(‘\n查询所有文档\n‘,query1)
#     query2 = es.search(index="my_index", body={"query":{"term":{‘name‘:‘python‘}}})
#     print(‘\n查找名字Python的文档:\n‘,query2[‘hits‘][‘hits‘][0])
#
# ParaSearch()
#

 

Elasticsearch 入门:简单实例

上一篇:List


下一篇:windows9小鲜肉-体验