一阵子不用es又忘光光了,犯了个极其弱智的错误。。。
 
想查询name是shuai的记录,于是写下了下面这个语句:

curl -X POST --header “Content-Type: application/json” http://127.0.0.1:9200/aaa/aaa/_search -d “{“query”: {“term”: {“name”: “shuai”}}}”

 
执行后报错:

{“error”:{“root_cause”:[{“type”:“json_parse_exception”,“reason”:“Unexpected character (‘q’ (code 113)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@16e75957; line: 1, column: 3]”}],“type”:“json_parse_exception”,“reason”:“Unexpected character (‘q’ (code 113)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@16e75957; line: 1, column: 3]”},“status”:500}

 
晃眼一看以为是json格式错误,检查了好几遍json也没找到哪里格式错了
最后仔细一看才反应过来,原来是 json里的双引号没转义。。。

修改后如下:

curl -X POST --header “Content-Type: application/json” http://127.0.0.1:9200/aaa/aaa/_search -d “{“query”: {“term”: {“name”: “shuai”}}}”

执行返回成功
 
  或者利用单引号里的内容不转义的特性:json的最外层用单引号,其余双引号

curl -X POST --header “Content-Type: application/json” http://127.0.0.1:9200/aaa/aaa/_search -d ‘{“query”: {“term”: {“name”: “shuai”}}}’