Last active
November 21, 2019 04:00
-
-
Save peschlowp/f234b868f00427e4f70d 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 parent-child relationships. Just copy paste into Sense and run the statements top to bottom.
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
# Transactions with parent-child relationships. | |
# Delete the index, just in case it still exists. | |
DELETE /myindex | |
# Create the index with a mapping for the actual documents and a mapping for the parent transaction type. | |
PUT /myindex | |
{ | |
"mappings": { | |
"transaction": { | |
"properties": { | |
"id": { | |
"type": "string", | |
"index": "not_analyzed" | |
} | |
} | |
}, | |
"document": { | |
"_parent": { | |
"type": "transaction" | |
}, | |
"properties": { | |
"transaction_id": { | |
"type": "string", | |
"index": "not_analyzed" | |
}, | |
"data": { | |
"type": "string", | |
"index": "not_analyzed" | |
} | |
} | |
} | |
} | |
} | |
# Index two documents with transaction 1 as parent. | |
PUT /myindex/document/1?parent=1 | |
{ | |
"transaction_id": "1", | |
"data": "part1" | |
} | |
PUT /myindex/document/2?parent=1 | |
{ | |
"transaction_id": "1", | |
"data": "part2" | |
} | |
# Try to search for one of the documents, including a check for the existence of the parent document. This will fail because transaction 1 does not exist yet. | |
GET /myindex/document/_search | |
{ | |
"query": { | |
"filtered": { | |
"query": { | |
"match": { | |
"data": "part1" | |
} | |
}, | |
"filter": { | |
"has_parent": { | |
"type": "transaction", | |
"query": {"match_all": {}} | |
} | |
} | |
} | |
} | |
} | |
# Index another document of the same transaction. | |
PUT /myindex/document/3?parent=1 | |
{ | |
"transaction_id": "1", | |
"data": "part3" | |
} | |
# Assuming that the third document completes the document group, create the transaction to make all three documents visible for search. | |
PUT /myindex/transaction/1 | |
{ | |
"id": "1" | |
} | |
# Again try to search for one of the documents. This will succeed now. | |
GET /myindex/document/_search | |
{ | |
"query": { | |
"filtered": { | |
"query": { | |
"match": { | |
"data": "part1" | |
} | |
}, | |
"filter": { | |
"has_parent": { | |
"type": "transaction", | |
"query": {"match_all": {}} | |
} | |
} | |
} | |
} | |
} | |
# We can also retrieve all documents belonging to some transaction. | |
GET /myindex/document/_search | |
{ | |
"query": { | |
"filtered": { | |
"filter": { | |
"has_parent": { | |
"type": "transaction", | |
"filter": { | |
"term": { | |
"id": "1" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
# Index documents of another transaction. | |
PUT /myindex/document/10?parent=2 | |
{ | |
"transaction_id": "2", | |
"data": "part1" | |
} | |
PUT /myindex/document/11?parent=2 | |
{ | |
"transaction_id": "2", | |
"data": "part2" | |
} | |
PUT /myindex/document/12?parent=2 | |
{ | |
"transaction_id": "2", | |
"data": "part3" | |
} | |
# The second transaction is not visible, so searching for all "part1" documents will only show the one for transaction 1. | |
GET /myindex/document/_search | |
{ | |
"query": { | |
"filtered": { | |
"query": { | |
"match": { | |
"data": "part1" | |
} | |
}, | |
"filter": { | |
"has_parent": { | |
"type": "transaction", | |
"query": {"match_all": {}} | |
} | |
} | |
} | |
} | |
} | |
# Create the second transaction. | |
PUT /myindex/transaction/2 | |
{ | |
"id": "2" | |
} | |
# Sarch will show us the "part1" documents of all completed transactions. | |
GET /myindex/document/_search | |
{ | |
"query": { | |
"filtered": { | |
"query": { | |
"match": { | |
"data": "part1" | |
} | |
}, | |
"filter": { | |
"has_parent": { | |
"type": "transaction", | |
"query": {"match_all": {}} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment