我在Python语言中使用PyES来使用ElasticSearch。通常,我使用以下格式构建查询:
# Create connection to server.
conn = ES('127.0.0.1:9200')
# Create a filter to select documents with 'stuff' in the title.
myFilter = TermFilter("title", "stuff")
# Create query.
q = FilteredQuery(MatchAllQuery(), myFilter).search()
# Execute the query.
results = conn.search(query=q, indices=['my-index'])
print type(results)
# > <class 'pyes.es.ResultSet'>当查询返回大量文档列表时,我的问题就开始了。将结果转换为字典列表的计算要求很高,因此我尝试返回字典中已有的查询结果。
但我不知道我到底该做什么。基于前面的链接,我尝试了一下:
from pyes import *
from pyes.query import *
from pyes.es import ResultSet
from pyes.connection import connect
# Create connection to server.
c = connect(servers=['127.0.0.1:9200'])
# Create a filter to select documents with 'stuff' in the title.
myFilter = TermFilter("title", "stuff")
# Create query / Search object.
q = FilteredQuery(MatchAllQuery(), myFilter).search()
# (How to) create the model ?
mymodel = lambda x, y: y
# Execute the query.
# class pyes.es.ResultSet(connection, search, indices=None, doc_types=None,
# query_params=None, auto_fix_keys=False, auto_clean_highlight=False, model=None)
resSet = ResultSet(connection=c, search=q, indices=['my-index'], model=mymodel)
# > resSet = ResultSet(connection=c, search=q, indices=['my-index'], model=mymodel)
# > TypeError: __init__() got an unexpected keyword argument 'search'有没有人能从ResultSet那里拿到判决书?任何有效地将ResultSet转换为字典(列表)的好建议也将受到赞赏。
发布于 2013-07-17 15:14:48
我尝试了太多的方法来直接将ResultSet转换为dict,但一无所获。我最近使用的最好的方法是将ResultSet项附加到另一个列表或字典中。ResultSet以字典的形式涵盖了每一个单独的条目。
下面是我的用法:
#create a response dictionary
response = {"status_code": 200, "message": "Successful", "content": []}
#set restul set to content of response
response["content"] = [result for result in resultset]
#return a json object
return json.dumps(response)发布于 2013-07-12 15:08:49
这并不复杂:只需迭代结果集即可。
for item in results:
print itemhttps://stackoverflow.com/questions/15222175
复制相似问题