Pylander February 2016

Python ElasticSearch DSL Truncating Results

I am trying to utilize the elasticsearch dsl library in python but I seem to be getting some kind of truncated results/metadata instead of a document/response object. Sanitized query below:

from elasticsearch_dsl import Search, Q, F
from elasticsearch_dsl.query import MultiMatch
from elasticsearch import Elasticsearch
import requests
import json  

client = Elasticsearch("")

s = Search().using(client).query("match", id="1") 
response = s.execute()
test_response = response.json()

Which gives this error:

AttributeError: 'Response' object has no attribute 'json'

I was hoping to use the newer DSL to keep things less verbose. Has anyone encountered something like this or have any tips. Much appreciated!

Answers


Artur Nowak February 2016

The result of execute() is the parsed JSON already (the structure is the same as with response from the HTTP API):

print(response.hits.total)

You can also iterate over the results as follows (documentation):

for h in response:
   print(h.title, h.body)

Post Status

Asked in February 2016
Viewed 1,185 times
Voted 6
Answered 1 times

Search




Leave an answer