1

我正在使用 PyES 在 Python 中使用 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'>

这非常有效。当查询返回大量文档时,我的问题就开始了。将结果转换为字典列表对计算的要求很高,所以我试图返回已经在字典中的查询结果。我遇到了这个文档:

http://pyes.readthedocs.org/en/latest/faq.html#id3 http://pyes.readthedocs.org/en/latest/references/pyes.es.html#pyes.es.ResultSet https:// github.com/aparo/pyes/blob/master/pyes/es.py(第1304行)

但我无法弄清楚我到底应该做什么。根据之前的链接,我试过这个:

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 转换为(列表)字典的好建议也将不胜感激。

4

2 回答 2

1

我尝试了太多直接将 ResultSet 转换为 dict 的方法,但一无所获。我最近使用的最好方法是将 ResultSet 项附加到另一个列表或字典中。ResultSet 将每个项目本身作为一个 dict 覆盖。

这是我的使用方法:

#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-17T07:14:48.180 回答
0

它并不复杂:只需遍历结果集。例如使用 for 循环:

for item in results:
   print item
于 2013-07-12T07:08:49.060 回答