Remove examples/ in favor of docs/examples on master (#1131)

This commit is contained in:
Seth Michael Larson
2020-03-05 16:33:02 -06:00
committed by GitHub
parent 04403ffb1d
commit f3b3da4a82
56 changed files with 0 additions and 1193 deletions
-27
View File
@@ -1,27 +0,0 @@
Example code for `elasticsearch-py`
===================================
This example code demonstrates the features and use patterns for the Python client.
To run this example make sure you have elasticsearch running on port 9200,
install additional dependencies (on top of `elasticsearch-py`)::
pip install python-dateutil GitPython
or
pip install -r requirements.txt
And now you can load the index (the index will be called `git`)::
python load.py
This will create an index with mappings and parse the git information of this
repository and load all the commits into it. You can run some sample queries by
running::
python queries.py
Look at the `queries.py` file for querying example and `load.py` on examples on
loading data into elasticsearch. Both `load` and `queries` set up logging so in
`/tmp/es_trace.log` you will have a transcript of the commands being run in the
curl format.
-2
View File
@@ -1,2 +0,0 @@
_*.asciidoc
_*.adoc
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Delete.py[tag=47b5ff897f26e9c943cee5c06034181d,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Delete.py[tag=c5e5873783246c7b1c01d8464fed72c4,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Delete.py[tag=d90a84a24a407731dfc1929ac8327746,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=0ba0b2db24852abccb7c0fc1098d566e,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=138ccd89f72aa7502dd9578403dcc589,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=1d65cb6d055c46a1bde809687d835b71,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=5eabcdbf61bfcb484dc694f25c2bba36,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=69a7be47f85138b10437113ab2f0d72d,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=710c7871f20f176d51209b1574b0d61b,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=745f9b8cdb8e91073f6e520e1d9f8c05,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=89a8ac1509936acc272fc2d72907bc45,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=8fdf2344c4fb3de6902ad7c5735270df,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=98234499cfec70487cec5d013e976a84,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=d222c6a6ec7a3beca6c97011b0874512,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Get.py[tag=fbcf5078a6a9e09790553804054c36b3,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=048d8abd42d094bbdcf4452a58ccb35b,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=1f336ecc62480c1d56351cc2f82d0d08,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=36818c6d9f434d387819c30bd9addb14,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=625dc94df1f9affb49a082fd99d41620,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=b918d6b798da673a33e49b94f61dcdc0,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=bb143628fd04070683eeeadc9406d9cc,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../src/Examples/Docs/Docs/Index.py[tag=d718b63cf1b6591a1d59a0cf4fd995eb,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=0caf6b6b092ecbcf6f996053678a2384,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=193864342d9f0a36ec84a91ca325f5ec,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=1c0f3b0bc4b7e53b53755fd3bda5b8cf,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=21fe98843fe0b5473f515d21803db409,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=345ea7e9cb5af9e052ce0cf6f1f52c23,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=37a3b66b555aed55618254f50d572cd6,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=37c778346eb67042df5e8edf2485e40a,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=3c31f9eb032861bff64abd8b14758991,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=3d7527bb7ac3b0e1f97b22bdfeb99070,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=506844befdc5691d835771bcbb1c1a60,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=6a8d7b34f2b42d5992aaa1ff147062d9,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=731621af937d66170347b9cc6b4a3c48,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=75bda7da7fefff2c16f0423a9aa41c6e,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=7d32a32357b5ea8819b72608fcc6fd07,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=92e3c75133dc4fdb2cc65f149001b40b,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=9c5ef83db886840355ff662b6e9ae8ab,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=b8459547da50aebddbcdd1aaaac02b5f,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=c181969ef91c3b4a2513c1885be98e26,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=c3fa04519df668d6c27727a12ab09648,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=db20adb70a8e8d0709d15ba0daf18d23,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=e8035a7476601ad4b136edb250f92d53,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=f8cc4b331a19ff4df8e4a490f906ee69,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../src/Examples/Docs/GettingStarted.py[tag=fa5f618ced25ed2e67a1439bb77ba340,indent=0]
----
@@ -1,4 +0,0 @@
[source,python]
----
include::./../../../../src/Examples/Docs/Setup/Install/CheckRunning.py[tag=3d1ff6097e2359f927c88c2ccdb36252,indent=0]
----
@@ -1,46 +0,0 @@
#!/usr/bin/env python
'''
Licensed to Elasticsearch B.V under one or more agreements.
Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
See the LICENSE file in the project root for more information
'''
from elasticsearch import Elasticsearch
es = Elasticsearch()
print("c5e5873783246c7b1c01d8464fed72c4 - L:9")
# tag::c5e5873783246c7b1c01d8464fed72c4[]
response = es.delete(
index='twitter',
id=1,
)
# end::c5e5873783246c7b1c01d8464fed72c4[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("47b5ff897f26e9c943cee5c06034181d - L:84")
# tag::47b5ff897f26e9c943cee5c06034181d[]
response = es.delete(
index='twitter',
id=1,
routing='kimchy',
)
# end::47b5ff897f26e9c943cee5c06034181d[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("d90a84a24a407731dfc1929ac8327746 - L:147")
# tag::d90a84a24a407731dfc1929ac8327746[]
response = es.delete(
index='twitter',
id=1,
timeout='5m',
)
# end::d90a84a24a407731dfc1929ac8327746[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
-168
View File
@@ -1,168 +0,0 @@
#!/usr/bin/env python
'''
Licensed to Elasticsearch B.V under one or more agreements.
Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
See the LICENSE file in the project root for more information
'''
from elasticsearch import Elasticsearch
es = Elasticsearch()
print("fbcf5078a6a9e09790553804054c36b3 - L:9")
# tag::fbcf5078a6a9e09790553804054c36b3[]
response = es.get(
index='twitter',
id=0,
)
# end::fbcf5078a6a9e09790553804054c36b3[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("98234499cfec70487cec5d013e976a84 - L:46")
# tag::98234499cfec70487cec5d013e976a84[]
response = es.exists(
index='twitter',
id=0,
)
# end::98234499cfec70487cec5d013e976a84[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("138ccd89f72aa7502dd9578403dcc589 - L:72")
# tag::138ccd89f72aa7502dd9578403dcc589[]
response = es.get(
index='twitter',
id=0,
_source=False,
)
# end::138ccd89f72aa7502dd9578403dcc589[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("8fdf2344c4fb3de6902ad7c5735270df - L:84")
# tag::8fdf2344c4fb3de6902ad7c5735270df[]
response = es.get(
index='twitter',
id=0,
_source_includes='*.id',
_source_excludes='entities',
)
# end::8fdf2344c4fb3de6902ad7c5735270df[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("745f9b8cdb8e91073f6e520e1d9f8c05 - L:93")
# tag::745f9b8cdb8e91073f6e520e1d9f8c05[]
response = es.get(
index='twitter',
id=0,
_source='*.id,retweeted',
)
# end::745f9b8cdb8e91073f6e520e1d9f8c05[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("913770050ebbf3b9b549a899bc11060a - L:109")
print("TODO")
print("5eabcdbf61bfcb484dc694f25c2bba36 - L:131")
# tag::5eabcdbf61bfcb484dc694f25c2bba36[]
response = es.index(
index='twitter',
id=1,
body={
'counter': 1,
'tags': ['red'],
},
)
# end::5eabcdbf61bfcb484dc694f25c2bba36[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("710c7871f20f176d51209b1574b0d61b - L:144")
# tag::710c7871f20f176d51209b1574b0d61b[]
response = es.get(
index='twitter',
id=1,
stored_fields='tags,counter',
)
# end::710c7871f20f176d51209b1574b0d61b[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("0ba0b2db24852abccb7c0fc1098d566e - L:178")
# tag::0ba0b2db24852abccb7c0fc1098d566e[]
response = es.index(
index='twitter',
id=1,
body={
'counter': 1,
'tags': ['white'],
},
routing='user1',
)
# end::0ba0b2db24852abccb7c0fc1098d566e[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("69a7be47f85138b10437113ab2f0d72d - L:189")
# tag::69a7be47f85138b10437113ab2f0d72d[]
response = es.get(
index='twitter',
id=2,
routing='user1',
stored_fields='tags,counter',
)
# end::69a7be47f85138b10437113ab2f0d72d[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("89a8ac1509936acc272fc2d72907bc45 - L:229")
# tag::89a8ac1509936acc272fc2d72907bc45[]
response = es.get_source(
index='twitter',
id=1,
)
# end::89a8ac1509936acc272fc2d72907bc45[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("d222c6a6ec7a3beca6c97011b0874512 - L:238")
# tag::d222c6a6ec7a3beca6c97011b0874512[]
response = es.get_source(
index='twitter',
id=1,
_source_includes='*.id',
_source_excludes='entities',
)
# end::d222c6a6ec7a3beca6c97011b0874512[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("2468ab381257d759d8a88af1141f6f9c - L:248")
print("TODO")
print("1d65cb6d055c46a1bde809687d835b71 - L:262")
# tag::1d65cb6d055c46a1bde809687d835b71[]
response = es.get(
index='twitter',
id=1,
routing='user1',
)
# end::1d65cb6d055c46a1bde809687d835b71[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
@@ -1,130 +0,0 @@
#!/usr/bin/env python
'''
Licensed to Elasticsearch B.V under one or more agreements.
Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
See the LICENSE file in the project root for more information
'''
from elasticsearch import Elasticsearch
es = Elasticsearch()
print("bb143628fd04070683eeeadc9406d9cc - L:11")
# tag::bb143628fd04070683eeeadc9406d9cc[]
response = es.index(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
}
)
# end::bb143628fd04070683eeeadc9406d9cc[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("804a97ff4d0613e6568e4efb19c52021 - L:77")
print("TODO")
print("d718b63cf1b6591a1d59a0cf4fd995eb - L:121")
# tag::d718b63cf1b6591a1d59a0cf4fd995eb[]
response = es.index(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
},
op_type='create',
)
# end::d718b63cf1b6591a1d59a0cf4fd995eb[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("048d8abd42d094bbdcf4452a58ccb35b - L.:134")
# tag::048d8abd42d094bbdcf4452a58ccb35b[]
response = es.create(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
}
)
# end::048d8abd42d094bbdcf4452a58ccb35b[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("36818c6d9f434d387819c30bd9addb14 - L:153")
# tag::36818c6d9f434d387819c30bd9addb14[]
response = es.index(
index='twitter',
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
}
)
# end::36818c6d9f434d387819c30bd9addb14[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("625dc94df1f9affb49a082fd99d41620 - L:204")
# tag::625dc94df1f9affb49a082fd99d41620[]
response = es.index(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
},
routing='kimchy',
)
# end::625dc94df1f9affb49a082fd99d41620[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("b918d6b798da673a33e49b94f61dcdc0 - L:327")
# tag::b918d6b798da673a33e49b94f61dcdc0[]
response = es.index(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
},
timeout='5m',
)
# end::b918d6b798da673a33e49b94f61dcdc0[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("1f336ecc62480c1d56351cc2f82d0d08 - L:357")
# tag::1f336ecc62480c1d56351cc2f82d0d08[]
response = es.index(
index='twitter',
id=1,
body={
'user': 'kimchy',
'post_date': '2009-11-15T14:12:12',
'message': 'trying out Elasticsearch',
},
version=2,
version_type='external',
)
# end::1f336ecc62480c1d56351cc2f82d0d08[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
@@ -1,338 +0,0 @@
#!/usr/bin/env python
'''
Licensed to Elasticsearch B.V under one or more agreements.
Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
See the LICENSE file in the project root for more information
'''
from elasticsearch import Elasticsearch
es = Elasticsearch()
print("f8cc4b331a19ff4df8e4a490f906ee69 - L: 209")
# tag::f8cc4b331a19ff4df8e4a490f906ee69[]
response = es.cat.health(v=True)
# end::f8cc4b331a19ff4df8e4a490f906ee69[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("db20adb70a8e8d0709d15ba0daf18d23 - L:240")
# tag::db20adb70a8e8d0709d15ba0daf18d23[]
response = es.cat.nodes(v=True)
# end::db20adb70a8e8d0709d15ba0daf18d23[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("c3fa04519df668d6c27727a12ab09648 - L:263")
# tag::c3fa04519df668d6c27727a12ab09648[]
response = es.cat.indices(v=True)
# end::c3fa04519df668d6c27727a12ab09648[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("0caf6b6b092ecbcf6f996053678a2384 - L:284")
# tag::0caf6b6b092ecbcf6f996053678a2384[]
# // PUT /customer?pretty
response = es.cat.indices(v=True)
# end::0caf6b6b092ecbcf6f996053678a2384[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("21fe98843fe0b5473f515d21803db409 - L:311")
# tag::21fe98843fe0b5473f515d21803db409[]
response = es.index(
index='customer',
id=1,
body={
'name': 'John Doe',
}
)
# end::21fe98843fe0b5473f515d21803db409[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("37a3b66b555aed55618254f50d572cd6 - L:347")
# tag::37a3b66b555aed55618254f50d572cd6[]
response = es.get(
index='customer',
id=1,
)
# end::37a3b66b555aed55618254f50d572cd6[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("92e3c75133dc4fdb2cc65f149001b40b - L:378")
# tag::92e3c75133dc4fdb2cc65f149001b40b[]
es.indices.delete(index='customer')
response = es.cat.indices(v=True)
# end::92e3c75133dc4fdb2cc65f149001b40b[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("fa5f618ced25ed2e67a1439bb77ba340 - L:398")
# tag::fa5f618ced25ed2e67a1439bb77ba340[]
response = es.index(
index='customer',
id=1,
body={
'name': 'John Doe',
}
)
response = es.get(
index='customer',
id=1,
)
es.indices.delete(index='customer')
# end::fa5f618ced25ed2e67a1439bb77ba340[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("21fe98843fe0b5473f515d21803db409 - L:431")
# tag::21fe98843fe0b5473f515d21803db409[]
response = es.index(
index='customer',
id=1,
body={
'name': 'John Doe',
}
)
# end::21fe98843fe0b5473f515d21803db409[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("75bda7da7fefff2c16f0423a9aa41c6e - L:442")
# tag::75bda7da7fefff2c16f0423a9aa41c6e[]
response = es.index(
index='customer',
id=2,
body={
'name': 'Jane Doe',
}
)
# end::75bda7da7fefff2c16f0423a9aa41c6e[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("37c778346eb67042df5e8edf2485e40a - L:454")
# tag::37c778346eb67042df5e8edf2485e40a[]
response = es.index(
index='customer',
body={
'name': 'Jane Doe',
}
)
# end::37c778346eb67042df5e8edf2485e40a[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("1c0f3b0bc4b7e53b53755fd3bda5b8cf - L:470")
# tag::1c0f3b0bc4b7e53b53755fd3bda5b8cf[]
response = es.index(
index='customer',
body={
'name': 'Jane Doe',
}
)
# end::1c0f3b0bc4b7e53b53755fd3bda5b8cf[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("6a8d7b34f2b42d5992aaa1ff147062d9 - L:489")
# tag::6a8d7b34f2b42d5992aaa1ff147062d9[]
response = es.update(
index='customer',
id=1,
body={
'doc': {
'name': 'Jane Doe',
},
},
)
# end::6a8d7b34f2b42d5992aaa1ff147062d9[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("731621af937d66170347b9cc6b4a3c48 - L:501")
# tag::731621af937d66170347b9cc6b4a3c48[]
response = es.update(
index='customer',
id=1,
body={
'doc': {
'name': 'Jane Doe',
'age': 20,
},
},
)
# end::731621af937d66170347b9cc6b4a3c48[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("38dfa309717488362d0f784e17ebd1b5 - L:513")
print("TODO")
print("9c5ef83db886840355ff662b6e9ae8ab - L:532")
# tag::9c5ef83db886840355ff662b6e9ae8ab[]
response = es.delete(
index='customer',
id=2,
)
# end::9c5ef83db886840355ff662b6e9ae8ab[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("7d32a32357b5ea8819b72608fcc6fd07 - L:550")
# tag::7d32a32357b5ea8819b72608fcc6fd07[]
response = es.bulk(body=[
{'index': {'_index': 'customer', '_id': 1}},
{'name': 'John Doe'},
{'index': {'_index': 'customer', '_id': 2}},
{'name': 'Jane Doe'},
])
# end::7d32a32357b5ea8819b72608fcc6fd07[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("193864342d9f0a36ec84a91ca325f5ec - L:562")
# tag::193864342d9f0a36ec84a91ca325f5ec[]
response = es.bulk(body=[
{'index': {'_index': 'customer', '_id': 1}},
{'name': 'John Doe'},
{'delete': {'_index': 'customer', '_id': 2}},
])
# end::193864342d9f0a36ec84a91ca325f5ec[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("c181969ef91c3b4a2513c1885be98e26 - L:647")
# tag::c181969ef91c3b4a2513c1885be98e26[]
response = es.search(
index='bank',
q='*',
sort={
'account_number': 'asc',
},
)
# end::c181969ef91c3b4a2513c1885be98e26[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("506844befdc5691d835771bcbb1c1a60 - L:720")
# tag::506844befdc5691d835771bcbb1c1a60[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
sort={
'account_number': 'asc',
},
)
# end::506844befdc5691d835771bcbb1c1a60[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("345ea7e9cb5af9e052ce0cf6f1f52c23 - L:789")
# tag::345ea7e9cb5af9e052ce0cf6f1f52c23[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
)
# end::345ea7e9cb5af9e052ce0cf6f1f52c23[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("3d7527bb7ac3b0e1f97b22bdfeb99070 - L:805")
# tag::3d7527bb7ac3b0e1f97b22bdfeb99070[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
size=1,
)
# end::3d7527bb7ac3b0e1f97b22bdfeb99070[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("3c31f9eb032861bff64abd8b14758991 - L:820")
# tag::3c31f9eb032861bff64abd8b14758991[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
from_=10,
size=10,
)
# end::3c31f9eb032861bff64abd8b14758991[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("e8035a7476601ad4b136edb250f92d53 - L:836")
# tag::e8035a7476601ad4b136edb250f92d53[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
sort={
'balance': 'desc',
},
)
# end::e8035a7476601ad4b136edb250f92d53[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
print("b8459547da50aebddbcdd1aaaac02b5f - L:854")
# tag::b8459547da50aebddbcdd1aaaac02b5f[]
response = es.search(
index='bank',
body={
'query': {
'match_all': {},
},
},
_source=['account_number', 'balance'],
)
# end::b8459547da50aebddbcdd1aaaac02b5f[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
@@ -1,19 +0,0 @@
#!/usr/bin/env python
'''
Licensed to Elasticsearch B.V under one or more agreements.
Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
See the LICENSE file in the project root for more information
'''
from elasticsearch import Elasticsearch
es = Elasticsearch()
print("3d1ff6097e2359f927c88c2ccdb36252 - L:7")
# tag::3d1ff6097e2359f927c88c2ccdb36252[]
response = es.info()
# end::3d1ff6097e2359f927c88c2ccdb36252[]
print("---------------------------------------")
print(response)
print("---------------------------------------")
-181
View File
@@ -1,181 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from os.path import dirname, basename, abspath
from datetime import datetime
import logging
import sys
import argparse
import git
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import TransportError
from elasticsearch.helpers import bulk, streaming_bulk
def create_git_index(client, index):
# we will use user on several places
user_mapping = {
"properties": {
"name": {"type": "text", "fields": {"keyword": {"type": "keyword"}}}
}
}
create_index_body = {
"settings": {
# just one shard, no replicas for testing
"number_of_shards": 1,
"number_of_replicas": 0,
# custom analyzer for analyzing file paths
"analysis": {
"analyzer": {
"file_path": {
"type": "custom",
"tokenizer": "path_hierarchy",
"filter": ["lowercase"],
}
}
},
},
"mappings": {
"properties": {
"repository": {"type": "keyword"},
"author": user_mapping,
"authored_date": {"type": "date"},
"committer": user_mapping,
"committed_date": {"type": "date"},
"parent_shas": {"type": "keyword"},
"description": {"type": "text", "analyzer": "snowball"},
"files": {"type": "text", "analyzer": "file_path", "fielddata": True},
}
},
}
# create empty index
try:
client.indices.create(index=index, body=create_index_body)
except TransportError as e:
# ignore already existing index
if e.error == "resource_already_exists_exception":
pass
else:
raise
def parse_commits(head, name):
"""
Go through the git repository log and generate a document per commit
containing all the metadata.
"""
for commit in head.traverse():
yield {
"_id": commit.hexsha,
"repository": name,
"committed_date": datetime.fromtimestamp(commit.committed_date),
"committer": {
"name": commit.committer.name,
"email": commit.committer.email,
},
"authored_date": datetime.fromtimestamp(commit.authored_date),
"author": {"name": commit.author.name, "email": commit.author.email},
"description": commit.message,
"parent_shas": [p.hexsha for p in commit.parents],
# we only care about the filenames, not the per-file stats
"files": list(commit.stats.files),
"stats": commit.stats.total,
}
def load_repo(client, path=None, index="git"):
"""
Parse a git repository with all it's commits and load it into elasticsearch
using `client`. If the index doesn't exist it will be created.
"""
path = dirname(dirname(abspath(__file__))) if path is None else path
repo_name = basename(path)
repo = git.Repo(path)
create_git_index(client, index)
# we let the streaming bulk continuously process the commits as they come
# in - since the `parse_commits` function is a generator this will avoid
# loading all the commits into memory
for ok, result in streaming_bulk(
client,
parse_commits(repo.refs.master.commit, repo_name),
index=index,
chunk_size=50, # keep the batch sizes small for appearances only
):
action, result = result.popitem()
doc_id = "/%s/doc/%s" % (index, result["_id"])
# process the information from ES whether the document has been
# successfully indexed
if not ok:
print("Failed to %s document %s: %r" % (action, doc_id, result))
else:
print(doc_id)
# we manually update some documents to add additional information
UPDATES = [
{
"_type": "_doc",
"_id": "20fbba1230cabbc0f4644f917c6c2be52b8a63e8",
"_op_type": "update",
"doc": {"initial_commit": True},
},
{
"_type": "_doc",
"_id": "ae0073c8ca7e24d237ffd56fba495ed409081bf4",
"_op_type": "update",
"doc": {"release": "5.0.0"},
},
]
if __name__ == "__main__":
# get trace logger and set level
tracer = logging.getLogger("elasticsearch.trace")
tracer.setLevel(logging.INFO)
tracer.addHandler(logging.FileHandler("/tmp/es_trace.log"))
parser = argparse.ArgumentParser()
parser.add_argument(
"-H",
"--host",
action="store",
default="localhost:9200",
help="The elasticsearch host you wish to connect to. (Default: localhost:9200)",
)
parser.add_argument(
"-p",
"--path",
action="store",
default=None,
help="Path to git repo. Commits used as data to load into Elasticsearch. (Default: None",
)
args = parser.parse_args()
# instantiate es client, connects to localhost:9200 by default
es = Elasticsearch(args.host)
# we load the repo and all commits
load_repo(es, path=args.path)
# run the bulk operations
success, _ = bulk(es, UPDATES, index="git")
print("Performed %d actions" % success)
# we can now make docs visible for searching
es.indices.refresh(index="git")
# now we can retrieve the documents
initial_commit = es.get(index="git", id="20fbba1230cabbc0f4644f917c6c2be52b8a63e8")
print(
"%s: %s" % (initial_commit["_id"], initial_commit["_source"]["committed_date"])
)
# and now we can count the documents
print(es.count(index="git")["count"], "documents in index")
-96
View File
@@ -1,96 +0,0 @@
#!/usr/bin/env python
from __future__ import print_function
import logging
from dateutil.parser import parse as parse_date
from elasticsearch import Elasticsearch
def print_search_stats(results):
print("=" * 80)
print(
"Total %d found in %dms" % (results["hits"]["total"]["value"], results["took"])
)
print("-" * 80)
def print_hits(results):
" Simple utility function to print results of a search query. "
print_search_stats(results)
for hit in results["hits"]["hits"]:
# get created date for a repo and fallback to authored_date for a commit
created_at = parse_date(
hit["_source"].get("created_at", hit["_source"]["authored_date"])
)
print(
"/%s/%s/%s (%s): %s"
% (
hit["_index"],
hit["_type"],
hit["_id"],
created_at.strftime("%Y-%m-%d"),
hit["_source"]["description"].split("\n")[0],
)
)
print("=" * 80)
print()
# get trace logger and set level
tracer = logging.getLogger("elasticsearch.trace")
tracer.setLevel(logging.INFO)
tracer.addHandler(logging.FileHandler("/tmp/es_trace.log"))
# instantiate es client, connects to localhost:9200 by default
es = Elasticsearch()
print("Empty search:")
print_hits(es.search(index="git"))
print('Find commits that says "fix" without touching tests:')
result = es.search(
index="git",
body={
"query": {
"bool": {
"must": {"match": {"description": "fix"}},
"must_not": {"term": {"files": "test_elasticsearch"}},
}
}
},
)
print_hits(result)
print("Last 8 Commits for elasticsearch-py:")
result = es.search(
index="git",
body={
"query": {"term": {"repository": "elasticsearch-py"}},
"sort": [{"committed_date": {"order": "desc"}}],
"size": 8,
},
)
print_hits(result)
print("Stats for top 10 committers:")
result = es.search(
index="git",
body={
"size": 0,
"aggs": {
"committers": {
"terms": {"field": "committer.name.keyword"},
"aggs": {"line_stats": {"stats": {"field": "stats.lines"}}},
}
},
},
)
print_search_stats(result)
for committer in result["aggregations"]["committers"]["buckets"]:
print(
"%15s: %3d commits changing %6d lines"
% (committer["key"], committer["doc_count"], committer["line_stats"]["sum"])
)
print("=" * 80)
-2
View File
@@ -1,2 +0,0 @@
GitPython
python-dateutil