python连接elasticsearch获取数据

2 篇文章 0 订阅
1 篇文章 0 订阅
# -*- encoding: utf-8 -*-
import sys
import json
from elasticsearch import Elasticsearch

reload(sys)
sys.setdefaultencoding("utf8")

######################################################
# 用于连接ES环境,查询检索小区信息,返回排名靠前10的小区信息。
# http_auth=('es_username', 'es_passwd')
# es_search(city,name):es_search(深圳,登科花园)
######################################################


es = Elasticsearch(
        ['xxx.xxx.xxx.xxx'],
        http_auth=('elastic', 'passwd'),
        port=9200
)


def es_search(city, name):
    query_json = {
        "bool": {
            "must": {
                "term": {
                    "city": city
                }
            },
            "must_not":{
                "term": {
                    "base_inf.kind":'商铺'
                }
            },
            "should": [
                {
                    "match": {
                        "message": name
                    }
                }
            ]
        }
    }

    source_arr = ["name",
                  "Long_lat.lon",
                  "Long_lat.lat",
                  "detail_inf",
                  "avg_price",
                  "base_inf.kind",
                  "base_inf.build_time"]



    res = es.search(index="st_soufang", body={"query": query_json, "_source": source_arr})  # 获取所有数据

    # 获取第一条数据,得分最高。
    top_10_recodes = res['hits']['hits']
    # print json.dumps(top_10_recodes)
    return [top_10_recodes]
    #
    # for item in best_recode:
    #     if item != '_source':
    #         print item,best_recode[item]


if __name__ == "__main__":
    # 测试单例
    city = '深圳'
    name = '东方星大厦'
    es_search(city, name)

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值