python連接elasticsearch獲取數據

# -*- 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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章