-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mapping python code examples (#2596)
- Loading branch information
1 parent
9656f83
commit 5c9b470
Showing
195 changed files
with
4,243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// mapping/types/geo-shape.asciidoc:300 | ||
|
||
[source, python] | ||
---- | ||
resp = client.index( | ||
index="example", | ||
body={ | ||
"location": { | ||
"type": "Polygon", | ||
"orientation": "LEFT", | ||
"coordinates": [ | ||
[ | ||
[-177, 10], | ||
[176, 15], | ||
[172, 0], | ||
[176, -15], | ||
[-177, -10], | ||
[-177, 10], | ||
] | ||
], | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// mapping/types/shape.asciidoc:199 | ||
|
||
[source, python] | ||
---- | ||
resp = client.index( | ||
index="example", | ||
body={ | ||
"location": { | ||
"type": "polygon", | ||
"coordinates": [ | ||
[ | ||
[1000, -1001], | ||
[1001, -1001], | ||
[1001, -1000], | ||
[1000, -1000], | ||
[1000, -1001], | ||
] | ||
], | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// mapping/types/flattened.asciidoc:205 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.create( | ||
index="my-index-000001", | ||
body={ | ||
"mappings": { | ||
"properties": { | ||
"title": {"type": "text"}, | ||
"labels": {"type": "flattened"}, | ||
} | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// mapping/types/object.asciidoc:46 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.create( | ||
index="my-index-000001", | ||
body={ | ||
"mappings": { | ||
"properties": { | ||
"region": {"type": "keyword"}, | ||
"manager": { | ||
"properties": { | ||
"age": {"type": "integer"}, | ||
"name": { | ||
"properties": { | ||
"first": {"type": "text"}, | ||
"last": {"type": "text"}, | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// mapping/runtime.asciidoc:1379 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.put_mapping( | ||
index="my-index-000001", | ||
body={ | ||
"runtime": { | ||
"http.client_ip": { | ||
"type": "ip", | ||
"script": "\n String clientip=grok('%{COMMONAPACHELOG}').extract(doc[\"message\"].value)?.clientip;\n if (clientip != null) emit(clientip); \n ", | ||
} | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// mapping/types/parent-join.asciidoc:76 | ||
|
||
[source, python] | ||
---- | ||
resp = client.index( | ||
index="my-index-000001", | ||
id="1", | ||
refresh=True, | ||
body={ | ||
"my_id": "1", | ||
"text": "This is a question", | ||
"my_join_field": "question", | ||
}, | ||
) | ||
print(resp) | ||
resp = client.index( | ||
index="my-index-000001", | ||
id="2", | ||
refresh=True, | ||
body={ | ||
"my_id": "2", | ||
"text": "This is another question", | ||
"my_join_field": "question", | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// mapping/runtime.asciidoc:471 | ||
|
||
[source, python] | ||
---- | ||
resp = client.bulk( | ||
index="my-index-000001", | ||
refresh="true", | ||
body=[ | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516729294000, | ||
"model_number": "QVKC92Q", | ||
"measures": {"voltage": 5.2}, | ||
}, | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516642894000, | ||
"model_number": "QVKC92Q", | ||
"measures": {"voltage": 5.8}, | ||
}, | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516556494000, | ||
"model_number": "QVKC92Q", | ||
"measures": {"voltage": 5.1}, | ||
}, | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516470094000, | ||
"model_number": "QVKC92Q", | ||
"measures": {"voltage": 5.6}, | ||
}, | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516383694000, | ||
"model_number": "HG537PU", | ||
"measures": {"voltage": 4.2}, | ||
}, | ||
{"index": {}}, | ||
{ | ||
"@timestamp": 1516297294000, | ||
"model_number": "HG537PU", | ||
"measures": {"voltage": 4}, | ||
}, | ||
], | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// mapping/runtime.asciidoc:175 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.create( | ||
index="my-index-000001", | ||
body={ | ||
"mappings": { | ||
"dynamic": "runtime", | ||
"properties": {"@timestamp": {"type": "date"}}, | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// mapping/types/text.asciidoc:181 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.create( | ||
index="idx", | ||
body={ | ||
"mappings": { | ||
"_source": {"mode": "synthetic"}, | ||
"properties": { | ||
"text": { | ||
"type": "text", | ||
"fields": {"raw": {"type": "keyword"}}, | ||
} | ||
}, | ||
} | ||
}, | ||
) | ||
print(resp) | ||
resp = client.index( | ||
index="idx", | ||
id="1", | ||
body={ | ||
"text": [ | ||
"the quick brown fox", | ||
"the quick brown fox", | ||
"jumped over the lazy dog", | ||
] | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// mapping/types/parent-join.asciidoc:104 | ||
|
||
[source, python] | ||
---- | ||
resp = client.index( | ||
index="my-index-000001", | ||
id="3", | ||
routing="1", | ||
refresh=True, | ||
body={ | ||
"my_id": "3", | ||
"text": "This is an answer", | ||
"my_join_field": {"name": "answer", "parent": "1"}, | ||
}, | ||
) | ||
print(resp) | ||
resp = client.index( | ||
index="my-index-000001", | ||
id="4", | ||
routing="1", | ||
refresh=True, | ||
body={ | ||
"my_id": "4", | ||
"text": "This is another answer", | ||
"my_join_field": {"name": "answer", "parent": "1"}, | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// mapping/types/date_nanos.asciidoc:160 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.create( | ||
index="idx", | ||
body={ | ||
"mappings": { | ||
"_source": {"mode": "synthetic"}, | ||
"properties": {"date": {"type": "date_nanos"}}, | ||
} | ||
}, | ||
) | ||
print(resp) | ||
resp = client.index( | ||
index="idx", | ||
id="1", | ||
body={ | ||
"date": ["2015-01-01T12:10:30.000Z", "2014-01-01T12:10:30.000Z"] | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// mapping/types/geo-shape.asciidoc:165 | ||
|
||
[source, python] | ||
---- | ||
resp = client.index( | ||
index="example", | ||
body={ | ||
"location": { | ||
"type": "Point", | ||
"coordinates": [-77.03653, 38.897676], | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// mapping/types/aggregate-metric-double.asciidoc:205 | ||
|
||
[source, python] | ||
---- | ||
resp = client.search( | ||
index="stats-index", | ||
body={"query": {"term": {"agg_metric": {"value": 702.3}}}}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// mapping/runtime.asciidoc:339 | ||
|
||
[source, python] | ||
---- | ||
resp = client.indices.put_mapping( | ||
index="my-index-000001", | ||
body={ | ||
"runtime": { | ||
"measures.start": {"type": "long"}, | ||
"measures.end": {"type": "long"}, | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// mapping/types/range.asciidoc:130 | ||
|
||
[source, python] | ||
---- | ||
resp = client.search( | ||
index="range_index", | ||
body={ | ||
"query": { | ||
"range": { | ||
"time_frame": { | ||
"gte": "2015-10-31", | ||
"lte": "2015-11-01", | ||
"relation": "within", | ||
} | ||
} | ||
} | ||
}, | ||
) | ||
print(resp) | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// mapping/types/flattened.asciidoc:73 | ||
|
||
[source, python] | ||
---- | ||
resp = client.search( | ||
index="bug_reports", | ||
body={"query": {"term": {"labels": "urgent"}}}, | ||
) | ||
print(resp) | ||
---- |
Oops, something went wrong.