ElasticSearch2.0 index中文乱码及解决问题

今天使用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);	
	}

	@Override
	public 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;
		}
	}

	@Override
	public 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"即可,代码如下(注意与上面代码的区别)

	@Override
	public 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后,浏览器查看结果如下:



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zlp1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值