2013-03-05 1 views
1

저는 Python을 사용하여 Python에서 ElasticSearch를 사용하고 있습니다. 일반적으로, 나는 다음과 같은 형식으로 내 쿼리를 작성 :PyES에서 ResultSet을 사용하는 방법

# 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에서 딕셔너리를 얻을 수있었습니다 : 이전 링크를 기반으로, 나는이 시도했습니다? 결과 집합을 사전의 (목록)으로 효율적으로 변환하는 데 좋은 해결 방법도 있습니다.

+0

당신은 DICT 또는 유사한로 변환하려고해서는 안 : 여기

내가 사용하는 방법입니다. 동일한 작업이 두 번 수행됩니다. 내가 한 일은 DottedDict 액세스를 사용하지 않는 ES 개체를 덮어 쓴 것입니다. 그러나 또 다른 가능성은 사용자에게 원시 쿼리입니다. –

답변

0

복잡하지 않다. 결과 세트를 반복하면된다. for 루프와 예를 들어 :

for item in results: 
    print item 
+0

이것은 정확하게 피하려고했던 것입니다. 큰 결과 집합을 처리 할 때이 aproach는 매우 느리게 변합니다. – JCJS

1

나는 DICT에 ResultSet의 캐스팅에 직접 너무 많은 방법을 시도했지만 아무것도 없어. 최근에 사용한 가장 좋은 방법은 ResultSet 항목을 다른 목록이나 dict에 추가하는 것입니다. 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) 
관련 문제