Json_parse_exception in elastic search

{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unrecognized character escape '&' (code 38)\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@5c05c49d; line: 7, column: 188]"}]

we are using query structure following below code.

{
"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": {}
}
}
}
}
}

I see nothing wrong with the query you copied so maybe we need more backgroud on when you get the error...
The error message is quite clear and means that on line 7 offset 188, you have invalid json - in this case you have \& to be precise... Here backslash should be escaped to be valid json \\& if you get the error when you try to index a document
This invalid json with non-escaped backslash would give out the same error :

POST test/_doc
{
"myfield" : "test \&"
}

while indexing is not a problem , when user search with special characters getting this type of error.can you please suggest me

Not reproducing any error with the query you sent, what query do you run in dev tools to reproduce the error and which version are you using?

while indexing we are elastic press plugin in wordpress and we are using elastic search for website searching.

{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unrecognized character escape '&' (code 38)\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@5c05c49d; line: 7, column: 188]"}]

[11-Dec-2019 14:50:46 America/Los_Angeles] PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\ServerErrorResponseException: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected character ('í' (code 237)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7bcfef75; line: 6, column: 153]"}],
The above errors is generated in php error logs.

we are using below coding structure for elastic search.

$json = '{
"from": '.$data->from.',
"size": 30,
"query": {
"multi_match": {
"query": "' . $searchstring . '",
"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"
}
},
"post_filter": {
"term": {
"custom.pos_format": "aside"
}
}
}';

$params = [
'index' => 'devarrecoin-1',
'type' => 'post',
'body' => $json
];

$results = $client->search($params);

Note: wordpress elastic press plugin Version 2.8.2,
Elasticsearch-PHP Branch : 2.0
Elastic search version 5.6.16
can you please suggest me.

Can you please suggest me
iam using below query

json = '{ "from": 0, "size": 30, "query": { "multi_match": { "query": "test and operator &+_-@#%^!() with elastic search json parse exception",
"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"
}
},
"post_filter": {
"term": {
"custom.pos_format": "asid"
}
}
}';

$params = [
'index' => 'devarrecoin-1',
'type' => 'post',
'body' => $json
];

$results = $client->search($params);

{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unrecognized character escape '&' (code 38)\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@5c05c49d; line: 7, column: 188]"}]

[11-Dec-2019 14:50:46 America/Los_Angeles] PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\ServerErrorResponseException: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected character ('í' (code 237)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7bcfef75; line: 6, column: 153]"}],
The above errors is generated in php error logs.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.