想用pycharm链接Elasticsearch,但是报错了,这里记录解决方法:
报错:ModuleNotFoundError:No module named 'fcntl'
其中访问地址:localhost:9200/索引/类型/id/_source
如:http://localhost:9200/megacorp/employee/1/_source
解决过程:
fcnl问题:在python安装路径下的lib中创建fcnl.py
里面写上,随后在pip中安装bpython
reload需要from imp import reload
setdefaultencoding函数已经没有了,注释掉即可
安装reload:
参考:https://blog.csdn.net/ziqiaowang/article/details/54972279
https://blog.csdn.net/qq_36330643/article/details/78584467
最后根据需求改写代码:
用于连接ES环境,查询检索雇员信息,返回相关信息有rock albums的
# -*- encoding: utf-8 -*-
import sys
import json
from imp import reload
from elasticsearch import Elasticsearch
reload(sys)
#sys.setdefaultencoding('utf8')
######################################################
# 用于连接ES环境,查询检索雇员信息,返回相关信息有rock albums的
# http_auth=('es_username', 'es_passwd')
# es_search(lastname):es_search(smith)
######################################################
es = Elasticsearch(
['localhost'],
# http_auth=('elastic', 'passwd'),
port=9200
)
def es_search(about_like):
query_json = {
"match":{
# "last_name":lastname
"about":about_like
}
}
source_arr = ["first_name",
"last_name",
"Long_lat.lat",
"age",
"about",
"interests"]
res = es.search(index="megacorp", body={"query": query_json, "_source": source_arr}) # 获取所有数据
# 获取第一条数据,得分最高。
top_10_recodes = res['hits']['hits']
# print json.dumps(top_10_recodes)
for item in top_10_recodes:
#根据匹配度的数值,返回最贴近rock albums的人
print(item)
return [top_10_recodes]
#
# for item in best_recode:
# if item != '_source':
# print item,best_recode[item]
if __name__ == "__main__":
# 测试单例
about_like='rock albums'
es_search(about_like)