Skip to content

Instantly share code, notes, and snippets.

@hackery
Last active November 19, 2018 16:37
Show Gist options
  • Save hackery/261932d8726298156acfc00c395e0f9e to your computer and use it in GitHub Desktop.
Save hackery/261932d8726298156acfc00c395e0f9e to your computer and use it in GitHub Desktop.
Elasticsearch: reindexing to replace null value
#DELETE jmb-test
PUT jmb-test/transactions/1
{
"user" : "jamie"
}
PUT jmb-test/transactions/2
{
"user" : "kay"
}
PUT jmb-test/transactions/3
{
}
GET jmb-test/_search?q=!_exists_:user
# shows document 3
GET jmb-test/_search
{
"aggs": {
"site": {
"terms": {
"field": "user.keyword",
"size": 10,
"missing": "MISSING USER"
}
}
}
}
# Shows a separate bucket for MISSING USER
PUT _template/jmb-test-reindex
{
"index_patterns": ["jmb-test-reindex"],
"mappings": {
"transactions": {
"properties": {
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"null_value": "MISSING"
}
}
}
}
}
}
}
POST _reindex
{
"source": {
"index": "jmb-test"
},
"dest": {
"index": "jmb-test-reindex"
}
}
GET jmb-test-reindex
# Shows the expected mapping in place
GET jmb-test-reindex/_search?q=!_exists_:user
# still shows doc#3 as having the field missing
GET jmb-test-reindex/_search?q=user.keyword:MISSING
# 0 docs returned
GET jmb-test-reindex/_search
{
"aggs": {
"user": {
"terms": {
"field": "user.keyword",
"size": 10,
"missing": "MISSING USER"
}
}
}
}
# Shows again a separate bucket for MISSING USER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment