This article explores how to use INFINI Gateway's regex replacement capabilities to fix problematic query parameters. The techniques discussed here also apply to Opensaerch and Easysearch environments.
In a previous article, we covered the request_body_json_set processor for modifying query parameters. This piece will focus on the request_body_regex_replace processor, which offers a different approach to parameter adjustment.
Consider the following two search requests. If the goal is to modify only the second query's size parameter to 10, how would you approach this?
# Request A
GET test/_search
{
"size": 50,
"query": {
"match": {
"status": "active"
}
}
}
# Request B
GET test/_search
{
"size": 5000,
"query": {
"match": {
"status": "pending"
}
}
}
Using request_body_json_set would require conditional logic to target specific requests. The request_body_regex_replace processor, however, allows direct pattern matching and replacement for the specific data you need to modify.
request_body_regex_replace
The request_body_regex_replace filter in INFINI Gateway uses regular expressions to replace string content within the request body. For the queries above, you can add the request_body_regex_replace section to INFINI Gateway's default configuration as shown below.
flow:
- name: default_flow
filter:
- request_body_regex_replace:
pattern: '"size": 5000'
to: '"size": 10'
- elasticsearch:
elasticsearch: prod
max_connection_per_node: 1000
This configuration scans incoming search requests for the string "size": 5000 and replaces it with "size": 10.
Modification Results
After applying the request_body_regex_replace processor, the affected query appears as follows.
The modification was successful. As another practical example, modifying application query conditions typically requires code changes and can be time-consuming. The techniques shown here enable rapid query condition adjustments without touching application code.
Consider an application sending this search request:
GET products/_search
{
"query": {
"bool": {
"filter": [
{"term": {"type": "electronics" }}
],
"must": [
{"term": {"manufacturer": {"value": "acme" }}}
]
}
}
}
The query intends to retrieve electronics from manufacturer acme, but the type field incorrectly specifies "electronics" when it should be "computing". You can use request_body_regex_replace to correct the type field with this configuration:
flow:
- name: default_flow
filter:
- request_body_regex_replace:
pattern: '"type": "electronics"'
to: '"type": "computing"'
- elasticsearch:
elasticsearch: prod
max_connection_per_node: 1000
The configuration searches for "type": "electronics" in the request body and replaces it with "type": "computing".
As you can see, the request_body_regex_replace processor provides great flexibility. Evaluate your specific requirements to choose between request_body_regex_replace and request_body_json_set processors.