Hi Team,
We are using elastic search cloud. we got below error. Please help me in resolving this.
PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unrecognized character escape '0' (code 48)
at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); line: 7, column: 76]"}],"type":"json_parse_exception","reason":"Unrecognized character escape '0' (code 48)
at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); line: 7, column: 76]"},"status":400} in D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php:632
Stack trace:
#0 D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php(317): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array)
#1 D:\home\site\wwwroot\elastic-search\elastic\vendor\react\promise\src\FulfilledPromise.php(28): Elasticsearch\Connections\Connection->Elasticse in D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 632
we are using below code for before passing a value to query string, Here we taken a string value given by users.
$string = the input is given by users
$str = htmlentities($string, ENT_COMPAT, "UTF-8");
$str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde|laquo|shy|not|ordf|raquo|cedil|lig);/','$1',$str);
$string = html_entity_decode($str);
$string = form_safe_json($string);
$searchstringmsg = escapeElasticReservedChars($string);
function escapeElasticReservedChars($string) {
// echo $string;
$regex = "/[\+\#\$\%\@\_\-\=\&\|\!\(\)\{\}\[\]\^\"\~\*\<\>\?\:\\\/]/";
$str = preg_replace($regex, addslashes('\$0'), $string);
return addslashes($str);
}
function form_safe_json($json) {
$json = empty($json) ? '' : $json ;
$search = array('\',"\n","\r","\f","\t","\b","'") ;
$replace = array('\\',"\n", "\r","\f","\t","\b", "'");
$json = str_replace($search,$replace,$json);
return $json;
}
{
"query": {
"multi_match": {
"query": "' . $searchstringmsg . '",
"type": "most_fields",
"fields": [
"post_title",
"post_content",
"custom.cat_name",
"custom.series_name",
"custom.topic_name",
"custom.genre_name",
"post_author.display_name",
"post_meta.wps_subtitle",
"terms.post_tag.name"
],
"fuzziness": "1"
}
},
"aggs": {
"top-sites": {
"terms": {
"field": "custom.pos_format.keyword"
},
"aggs": {
"top_tags_hits": {
"top_hits": {}
}
}
}
}
}
Thanks,
Jayasree