# NOT RUN {
x <- connect(errors = "complete")
##### Elasticsearch < v5
if (x$es_ver() < 500) {
# typical usage
## create an index first
if (index_exists(x, "myindex")) index_delete(x, "myindex")
mapping <- '{
"mappings": {
"mytype": {
"properties": {
"message": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
}
}'
index_create(x, "myindex", body = mapping)
## register a percolator
perc_body = '{
"query" : {
"match" : {
"message" : "bonsai tree"
}
}
}'
percolate_register(x, index = "myindex", type = "mytype",
id = 1, body = perc_body)
## register another
perc_body2 <- '{
"query" : {
"match" : {
"message" : "jane doe"
}
}
}'
percolate_register(x, index = "myindex", type = "mytype",
id = 2, body = perc_body2)
## match a document to a percolator
doc <- '{
"query": {
"percolate": {
"field": "query",
"document": {
"message" : "A new bonsai tree in the office"
}
}
}
}'
percolate_match(x, index = "myindex", type = "mytype", body = doc)
## List percolators - for an index, no type, can't do across indices
percolate_list(x, index = "myindex")$hits$hits
## Percolate counter
percolate_count(x, index = "myindex", type = "mytype", body = doc)$total
## delete a percolator
percolate_delete(x, index = "myindex", id = 2)
} # end ES < 5
##### Elasticsearch >= v5
if (x$es_ver() >= 500 && x$es_ver() <= 700) {
if (index_exists(x, "myindex")) index_delete(x, "myindex")
body <- '{
"mappings": {
"mytype": {
"properties": {
"message": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
}
}'
# create the index with mapping
index_create(x, "myindex", body = body)
## register a percolator
z <- '{
"query" : {
"match" : {
"message" : "bonsai tree"
}
}
}'
percolate_register(x, index = "myindex", type = "mytype", id = 1, body = z)
## register another
x2 <- '{
"query" : {
"match" : {
"message" : "the office"
}
}
}'
percolate_register(x, index = "myindex", type = "mytype", id = 2, body = x2)
## match a document to a percolator
query <- '{
"query" : {
"percolate" : {
"field": "query",
"document": {
"message": "A new bonsai tree in the office"
}
}
}
}'
percolate_match(x, index = "myindex", body = query)
} # end ES >= 5
##### Elasticsearch >= v7
if (x$es_ver() >= 700) {
if (index_exists(x, "myindex")) index_delete(x, "myindex")
body <- '{
"mappings": {
"properties": {
"message": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
}'
# create the index with mapping
index_create(x, "myindex", body = body)
## register a percolator
z <- '{
"query" : {
"match" : {
"message" : "bonsai tree"
}
}
}'
percolate_register(x, index = "myindex", id = 1, body = z)
## register another
x2 <- '{
"query" : {
"match" : {
"message" : "the office"
}
}
}'
percolate_register(x, index = "myindex", id = 2, body = x2)
## match a document to a percolator
query <- '{
"query" : {
"percolate" : {
"field": "query",
"document": {
"message": "A new bonsai tree in the office"
}
}
}
}'
percolate_match(x, index = "myindex", body = query)
} # end ES >= 7
# }
Run the code above in your browser using DataLab