ElasticSearch2.0 index中文乱码及解决问题
来源:互联网 发布:淘宝卖眼药水 编辑:程序博客网 时间:2024/06/05 18:30
今天使用HttpClient 在ElasticSearch2.0上index documents时,由于document中包含有中文,浏览器打开一看,发现中文乱码,index的代码如下:
util.upload("test2", "水岸线接地网都!(!@!@u你好这是一份测试文档wdjqwdqu","c:\\test.txt","lpchou",new Date(),1,"lpchou")
/** * 上传文档,成功返回文档id,否则返回null * */public String upload(String name,String content,String path,String author,Date date,int level,String accessUserName){String idString=null;idString=indexDocument(name, content, path, author, date, level, accessUserName);return idString;}
/** * 对Document建立索引,返回索引id * */private String indexDocument(String name,String content,String path,String author,Date date,int level,String accessUserName){Map<String,Object> documentMap=new HashMap<String,Object>();documentMap.put(FIELD_NAME,name);documentMap.put(FIELD_CONTENT,content);documentMap.put(FIELD_PATH,path);documentMap.put(FIELD_AUTHOR,author);documentMap.put(FIELD_DATE,date.getTime());documentMap.put(FIELD_LEVEL,level);documentMap.put(FIELD_ACCESS_USER_NAME,accessUserName);return mElasticSerachService.indexContent(mIndex, mType, documentMap);}
@Overridepublic String indexContent(String index, String type,Map<String, Object> content) {ObjectMapper objectMapper=new ObjectMapper();try {String jsonString=objectMapper.writeValueAsString(content);return indexContent(index, type, jsonString);} catch (JsonProcessingException e) {e.printStackTrace();return null;}}@Overridepublic String indexContent(String index, String type, String jsonContent) {String indexUri="http://"+mMasterNodeUrl+":"+mMasterNodePort+"/"+index+"/"+type+"/";StringEntity stringEntity=null;try {stringEntity = new StringEntity(jsonContent);} catch (UnsupportedEncodingException e) {e.printStackTrace();return null;}System.out.println(stringEntity.toString());JsonNode rootJsonNode=sendHttpPost(indexUri, stringEntity);if(rootJsonNode!=null){String id=rootJsonNode.get("_id").asText();return id;}return null;}
结果是能够进行index并得到id,但是打开浏览器一看,发现中文乱码,汉字全用"?"代替了,图中第一个中文没乱码因为我是用的JAVA API index的,后两个乱码的用的HttpClient的HttpPost来index的。
解决如下:
原因是我在index到ElasticSearch里的时候需要指定编码,方法为在使用HttpPost的时候需要传入一个StringEntity的参数,在构造StringEntity的时候设置编码为"UTF-8"即可,代码如下(注意与上面代码的区别)
@Overridepublic String indexContent(String index, String type, String jsonContent) {String indexUri="http://"+mMasterNodeUrl+":"+mMasterNodePort+"/"+index+"/"+type+"/";StringEntity stringEntity=null;stringEntity = new StringEntity(jsonContent,"UTF-8");System.out.println(stringEntity.toString());JsonNode rootJsonNode=sendHttpPost(indexUri, stringEntity);if(rootJsonNode!=null){String id=rootJsonNode.get("_id").asText();return id;}return null;}可以看到,在构造StringEntity的时候,如果指定了编码,连异常都没有了。再次Index后,浏览器查看结果如下:
1 0
- ElasticSearch2.0 index中文乱码及解决问题
- 中文乱码解决问题
- myEclipse中文乱码解决问题
- QT5.2中文乱码解决问题
- QT5.2中文乱码解决问题
- QT5.2中文乱码解决问题
- 中文乱码解决问题(转载)
- Git GUI中文乱码解决问题
- BeautifulSoup中文乱码解决问题 python 爬虫 乱码
- BeautifulSoup中文乱码解决问题 python 爬虫 乱码
- 访问Tomcat7及以下版本时(HttpServeltRequest请求时)的中文乱码解决问题
- 比较简便的中文乱码解决问题
- struts+ajax中文参数乱码解决问题
- J2EE编辑中的中文乱码解决问题
- spring3 mvc rest返回中文乱码? 解决问题
- Elasticsearch2.0搭建集群及配置
- Linux-001-nginx index 中文乱码
- 乱码解决问题
- react 入门知识
- iOS笔记19
- 走过电竞之路的程序员
- 关于调整互联网、电话订票起售时间的公告
- 一个简单的溢出程序解析
- ElasticSearch2.0 index中文乱码及解决问题
- c++命令结束进程树
- 指向指针的指针(2)
- SpringMVC配置及初级入门11_30
- 自己实现的一个list双链表
- 在中间件维护的时候应该注意tns的地方 -------运维日志7
- UI第十四天:UI⾼级可视化设计
- 【Android】RecyclerView的OnItemClickListener点击事件处理
- 微信开发数据收发原理及消息数据格式