elasticsearch入门教程二(基于restful 针对5.1版本)
2017-01-13 13:14
375 查看
采用restful的好处就是可以跨越一切语言的障碍,遵循规范即可,还是以JAVA进行举例
pom.xml中包含的支持类
最重要的代码非常简单。(倒是测试花了不少时间)
MyStruct 内容
下面介绍一些细节
使用post 进行查询的代码如下
完整的文档地址如下
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
pom.xml中包含的支持类
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.apache.log4j</groupId> <artifactId>com.springsource.org.apache.log4j</artifactId> <version>1.2.15</version> </dependency>
最重要的代码非常简单。(倒是测试花了不少时间)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.ObjectMapper; import jdk.nashorn.api.scripting.JSObject; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.converter.BufferedImageHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import sun.java2d.cmm.Profile; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.net.URI; import java.util.*; import java.util.List; public class Driver { public static void main(String[] args) { RestTemplate rest2 = new RestTemplate();//这就是Spring提供的模板类,大大方面我们的书写 //下面两个分别是JSON转换类,和图片转换类,也是Spring提供 MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); BufferedImageHttpMessageConverter bufferedImageHttpMessageConverter = new BufferedImageHttpMessageConverter(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(mappingJackson2HttpMessageConverter); messageConverters.add(bufferedImageHttpMessageConverter); rest2.setMessageConverters(messageConverters); MyStruct myStruct = rest2.getForObject("http://localhost:9200/twitter/tweet/2", MyStruct.class); //这个可以直接把返回的JSON内容放到结构体了 Map<String, Object> vars = new HashMap<String, Object>(); vars = rest2.getForObject("http://localhost:9200/twitter/tweet/2", HashMap.class); //不想弄结构体也可以放map里 // 获取图片也是非常简单,下面一行搞定,后面都是显示 BufferedImage bufferedImage= rest2.getForObject("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg", BufferedImage.class); JFrame frame = new JFrame( " photos"); frame.setLayout(new GridLayout(2, 1)); frame.add(new JLabel(new ImageIcon(bufferedImage))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
MyStruct 内容
@SuppressWarnings("unused") @JsonIgnoreProperties(ignoreUnknown = true) public class MyStruct { public String _index; public String _type; public String _id; public Integer _version; public Boolean found; public MySource _source; }
public class MySource { public String user; public Date postDate; public String message; }
下面介绍一些细节
使用post 进行查询的代码如下
Map<String, Object> varsRequest = new HashMap<String, Object>(); Map<String, Object> queryContent = new HashMap<String, Object>(); Map<String, Object> matchContent = new HashMap<String, Object>(); queryContent.put("match_all", matchContent); varsRequest.put("query", queryContent); String strTemp4 = varsRequest.toString(); Map<String, Object> varsResponse = rest2.postForObject("http://localhost:9200/twitter/tweet/_search?pretty", varsRequest, HashMap.class);
完整的文档地址如下
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
相关文章推荐
- elasticsearch入门教程一(基于JAVA client 针对5.1版本)
- 基于ActionScript 3 的Flex Air 教程 入门(一)
- 基于PHP的cURL快速入门教程 (小偷采集程序)
- Jigloo 开发 SWT 的入门教程(修改版本)
- Android教程入门:不同平台版本支持
- 版本控制入门插图教程
- 针对HTML源代码进行SEO布局-SEO最佳入门教程
- 针对小白的Python入门教程——Python使用说明书
- 使用 ADD-ON SDK 开发 基于 Html JQuery 和 CSS 的 firefox 插件入门教程1: 创建一个简单的 Add-on
- Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程
- 版本控制工具 GIT入门教程
- [转载]Jigloo 开发 SWT 的入门教程(修改版本)
- 针对新手ubuntu图形化入门教程知识
- Oracle 从入门到精通视频教程(11G版本)
- Arduino教程(入门篇)——各版本驱动安装方法
- 基于PHP的cURL快速入门教程 (小偷采集程序)
- phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍、补充。 Ste
- 版本管理工具Git入门教程
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
- 【视频教程】JEECG 入门视频教程大全+历史版本代码下载