Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Elasticsearch/Python test
curl -XPOST http://localhost:9200/test/articles/1 -d '{
"content": "The quick brown fox"
}'
curl -XPOST http://localhost:9200/test/articles/2 -d '{
"content": "What does the fox say?"
}'
curl -XPOST http://localhost:9200/test/articles/3 -d '{
"content": "The quick brown fox jumped over the lazy dog"
}'
curl -XPOST http://localhost:9200/test/articles/4 -d '{
"content": "The quick lazy brown fox did not jump."
}'
from elasticsearch import Elasticsearch
if __name__ == '__main__':
es = Elasticsearch()
res = es.search(index="test", doc_type="articles", body={"query": {"match": {"content": "fox"}}})
print("%d documents found:" % res['hits']['total'])
for doc in res['hits']['hits']:
print("%s) %s" % (doc['_id'], doc['_source']['content']))
#es.create(index="test", doc_type="articles", body={"content": "One more fox"})
"""
Simple example of querying Elasticsearch creating REST requests
"""
import requests
import json
def search(uri, term):
"""Simple Elasticsearch Query"""
query = json.dumps({
"query": {
"match": {
"content": term
}
}
})
response = requests.get(uri, data=query)
results = json.loads(response.text)
return results
def format_results(results):
"""Print results nicely:
doc_id) content
"""
data = [doc for doc in results['hits']['hits']]
for doc in data:
print("%s) %s" % (doc['_id'], doc['_source']['content']))
def create_doc(uri, doc_data={}):
"""Create new document."""
query = json.dumps(doc_data)
response = requests.post(uri, data=query)
print(response)
if __name__ == '__main__':
uri_search = 'http://localhost:9200/test/articles/_search'
uri_create = 'http://localhost:9200/test/articles/'
results = search(uri_search, "fox")
format_results(results)
#create_doc(uri_create, {"content": "The fox!"})
#results = search(uri_search, "fox")
#format_results(results)
curl -XPOST http://localhost:9200/test/articles/_search?pretty=true -d '{
"query": {
"match": {
"content": "dog"
}
}
}'
@ouri11

This comment has been minimized.

Copy link

@ouri11 ouri11 commented May 18, 2018

bonjour,
je suis en galère sur un script python qui interagi avec elasticsearch
j'essaye de faire une requête qui récupère tous les noms avec leur IPs associé mais le problème c que je récupère tous les nom avec toute les adresses et pas une adresse pour un nom
pourriez vous m'aider?
voici mon code:

from elasticsearch import Elasticsearch
import json
es = Elasticsearch(['localhost:9200'])
res = es.search(
index='test',search_type='count',

body={ "aggs":{
"all_accountname":{
"terms": {
"field": "AccountName",
}
},
"all_ip":{
"terms": {
"field": "server_ip",
}
}
}
#print (res)
print ('noms des comptes')
for i in res['aggregations']['all_accountname']['buckets']:
for z in res['aggregations']['all_ip']['buckets']:
print ( i['key'], z['key'])

@burtstreetdata

This comment has been minimized.

Copy link

@burtstreetdata burtstreetdata commented Nov 25, 2018

Thanks for these. On current version of elasticsearch, you have to specify content type when you search

json_q = json.dumps({"query": { "match_all": {}}})
url = "http://localhost:9200/plays/_search"
headers ={'Content-Type' : 'application/json'}
res=requests.get(url, headers=headers,  data = json_q)  # <-- specify content type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment