Skip to content

Instantly share code, notes, and snippets.

@bonzanini
Last active May 25, 2023 23:35
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save bonzanini/fe2ff32116f16e3009be to your computer and use it in GitHub Desktop.
Save bonzanini/fe2ff32116f16e3009be to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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