Skip to content

Instantly share code, notes, and snippets.

@peschlowp
Created October 8, 2014 13:23
Show Gist options
  • Save peschlowp/8d0a277f22163c0c6185 to your computer and use it in GitHub Desktop.
Save peschlowp/8d0a277f22163c0c6185 to your computer and use it in GitHub Desktop.
Elasticsearch Sense contents to demonstrate how transactions with respect to search visibility may be implemented with nested objects. Just copy paste into Sense and run the statements top to bottom.
# Delete the index, just in case it still exists.
DELETE /myindex
# Create the index with a mapping declaring a nested field.
PUT /myindex
{
"mappings": {
"document": {
"properties": {
"common_id": {
"type": "string",
"index": "not_analyzed"
},
"completed": {
"type": "boolean"
},
"parts": {
"type": "nested",
"properties": {
"data": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
# Indexing now turns into an update right from the start, with an upsert for the initial add. Scripting may require you to set script.disable_dynamic: false in elasticsearch.yml depending on the scripting language and Elasticsearch version you are using.
# Index two documents with common ID 1 as parent.
POST /myindex/document/1/_update
{
"script": "ctx._source.parts += part",
"params": {
"part": {
"data": "part1"
}
},
"upsert": {
"common_id": "1",
"completed": false,
"parts": [
{
"data": "part1"
}
]
}
}
POST /myindex/document/1/_update
{
"script": "ctx._source.parts += part",
"params": {
"part": {
"data": "part2"
}
},
"upsert": {
"common_id": "1",
"completed": false,
"parts": [
{
"data": "part2"
}
]
}
}
# Check the document.
GET myindex/document/1
# Try to search for one of the nested documents. This will fail because completed is not true yet.
GET /myindex/document/_search
{
"query": {
"filtered": {
"query": {
"nested": {
"path": "parts",
"query": {
"match": {
"data": "part1"
}
}
}
},
"filter": {
"term": {
"completed": true
}
}
}
}
}
# Index a third one, completing the group.
POST /myindex/document/1/_update
{
"script": "ctx._source.parts += part; ctx._source.completed = true",
"params": {
"part": {
"data": "part3"
}
},
"upsert": {
"common_id": "1",
"completed": false,
"parts": [
{
"data": "part3"
}
]
}
}
# Try to search for one of the nested documents. This will succeed now.
GET /myindex/document/_search
{
"query": {
"filtered": {
"query": {
"nested": {
"path": "parts",
"query": {
"match": {
"data": "part1"
}
}
}
},
"filter": {
"term": {
"completed": true
}
}
}
}
}
# Index a document for another group.
POST /myindex/document/2/_update
{
"script": "ctx._source.parts += part",
"params": {
"part": {
"data": "part1"
}
},
"upsert": {
"common_id": "2",
"completed": false,
"parts": [
{
"data": "part1"
}
]
}
}
# Try to search for one part across groups. This will only show parts of completed groups.
GET /myindex/document/_search
{
"query": {
"filtered": {
"query": {
"nested": {
"path": "parts",
"query": {
"match": {
"data": "part1"
}
}
}
},
"filter": {
"term": {
"completed": true
}
}
}
}
}
# Complete the second group.
POST /myindex/document/2/_update
{
"script": "ctx._source.parts += part; ctx._source.completed = true",
"params": {
"part": {
"data": "part2"
}
},
"upsert": {
"common_id": "2",
"completed": false,
"parts": [
{
"data": "part2"
}
]
}
}
# Now the part of both finished groups will be shown.
GET /myindex/document/_search
{
"query": {
"filtered": {
"query": {
"nested": {
"path": "parts",
"query": {
"match": {
"data": "part1"
}
}
}
},
"filter": {
"term": {
"completed": true
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment