2016-06-03 7 views
1

Ich habe sehr wenige Felder in ElasticSearch und möchte einzelne Felder von _source erhalten. nachstehendes Beispiel in einem Python-Skript wird einen Schlüsselfehler:Wie man ein einzelnes Feld, das Teil von "_source" ist, mit Python mit elastischer Suche druckt?

es = elasticsearch.Elasticsearch([{'host': host_name}]) 
res = es.search(index="collections", body={"query": {"match_all": {}}, "from": 0, "size": 1}) 
for doc in res['hits']['hits']: 
    print(doc ["_id"], doc ["_type"], doc ["_source.collection.id"]) 
+0

schreiben Sie bitte den Fehler – glls

+0

Wenn das Skript I läuft Holen Sie Folgendes: Traceback (zuletzt letzten Aufruf): Datei "C: \ Python34 \ Scripts \ ElasticGetCollectionInfo.py", Zeile 27, in drucken (doc ["_id"], doc ["_type"], doc ["_source.collection.id"]) KeyError: '_source.collection.id' – DonL

Antwort

1

Sie sollen wahrscheinlich doc ["_source"]["collection"]["id"] anstelle von doc ["_source.collection.id"]

Beispiel: print(doc ["_id"], doc ["_type"], doc ["_source"]["collection"]["id"])

+0

Vielen Dank. Das funktioniert perfekt! – DonL

+0

sicher np, willkommen zu Stack-Überlauf :) [akzeptieren Sie die Antwort, wenn es geholfen hat] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – keety

Verwandte Themen