• 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('
    批量插入数据完成:
    ',result['_source'])
    #
    # AddDatas()
    # print(2)
    # def ParaSearch():
    #     es = Elasticsearch([{'host':'localhost','port':9200}])
    #     query1 = es.search(index="my_index", body={"query":{"match_all":{}}})
    #     print('
    查询所有文档
    ',query1)
    #     query2 = es.search(index="my_index", body={"query":{"term":{'name':'python'}}})
    #     print('
    查找名字Python的文档:
    ',query2['hits']['hits'][0])
    #
    # ParaSearch()
    #
  • 相关阅读:
    从 http 升级到 https 过程中遇到的一些问题
    Java 对象,数组 与 JSON 字符串 相互转化
    jQuery 事件探秘
    eclipse 添加 hibernate 插件
    struts2 + jquery + json 简单的前后台信息交互
    匿名类、匿名方法、扩展方法
    禁用右键
    JS聊天室
    MVC知识汇总
    知识点汇总
  • 原文地址:https://www.cnblogs.com/AKsnoopy/p/15181513.html
Copyright © 2020-2023  润新知