扩展阅读
当前位置: 编程语言>其它
elasticsearch RESTful项目举例说明: java jest
发布时间:2014-1-12
本文导语: 首先看看项目的目录结构我一般习惯了用maven去管理我的项目,看pom.xml吧 4.0.0com.mkfreeES-jest0.0.1-SNAPSHOTwario.searchboxjest0.0.2org.elasticsearchelasticsearch0.20.2junitjunit4.10sonatypeSonatype Groupshttps://oss.sonatype.org/content/groups/public/ 1.配置jest客户端Ini...
4.0.0 com.mkfree ES-jest 0.0.1-SNAPSHOT war io.searchbox jest 0.0.2 org.elasticsearch elasticsearch 0.20.2 junit junit 4.10 sonatype Sonatype Groups https://oss.sonatype.org/content/groups/public/
package com.mkfree.jest.config; import io.searchbox.client.JestClient; import io.searchbox.client.JestClientFactory; import io.searchbox.client.config.ClientConfig; import io.searchbox.client.config.ClientConstants; import java.util.LinkedHashSet; /** * 初始化连接es服务端,这里相当于dao层 */ public class InitES { /** * 静态,单例... */ private static JestClient JestClient; /** * 配置jest客户端,到时使用spring时,可以用配置方式 ,现在暂时使用new ... * * @return */ private static ClientConfig clientConfig() { // es的服务端地址,暂时我是用我虚拟机的(ubuntu)做服务器 String connectionUrl = "http://192.168.56.101:9200";// 一般都是9200端口 ClientConfig clientConfig = new ClientConfig(); // 当你用集群时,就有可能会有多个es的服务端,这里我暂时没有集群 LinkedHashSetservers = new LinkedHashSet(); servers.add(connectionUrl); clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers); clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false); return clientConfig; } /** * 获取一个jest的对象 * * @return */ public static JestClient jestClient() { JestClientFactory factory = new JestClientFactory(); factory.setClientConfig(clientConfig()); if (JestClient != null) { JestClient = factory.getObject(); } return JestClient; } }
News 新闻类
package com.mkfree.jest.domain; import io.searchbox.annotations.JestId; /** * 虚拟news 搜索文章 * * @author hk * * 2013-1-12 下午11:38:29 */ public class News { @JestId private int id; private String title; private String content; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
package com.mkfree.jest.service; import io.searchbox.client.JestClient; import io.searchbox.client.JestResult; import io.searchbox.core.Bulk; import io.searchbox.core.Index; import io.searchbox.core.Search; import io.searchbox.indices.CreateIndex; import io.searchbox.indices.DeleteIndex; import java.io.IOException; import java.util.List; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import com.mkfree.jest.config.InitES; import com.mkfree.jest.domain.News; /** * es简单服务接口 * * @author hk * * 2013-1-12 下午11:47:16 */ public class SearchService { private static JestClient jestClient = InitES.jestClient(); /** * 创建es news索引 */ public void builderSearchIndex() { int num = 10000; long start = System.currentTimeMillis(); try { // 如果索引存在,删除索引 DeleteIndex deleteIndex = new DeleteIndex("news"); jestClient.execute(deleteIndex); // 创建索引 CreateIndex createIndex = new CreateIndex("news"); jestClient.execute(createIndex); // Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称) Bulk bulk = new Bulk("news", "article"); // 添加添加100万条假数据去服务端(ES) for (int i = 0; i < num; i++) { News news = new News(); news.setId(i + 1); news.setTitle("elasticsearch RESTful搜索引擎-(java jest 使用[入门])" + (i + 1)); news.setContent("好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)" + (i + 1)); bulk.addIndex(new Index.Builder(news).build()); } jestClient.execute(bulk); } catch (Exception e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("创建索引时间:数据量是 " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒"); } /** * 搜索新闻 * * @param param * @return */ public ListsearchsNews(String param) { long start = System.currentTimeMillis(); QueryBuilder queryBuilder = QueryBuilders.queryString(param); Search search = new Search(Search.createQueryWithBuilder(queryBuilder.toString())); search.addIndex("news"); search.addType("article"); JestResult result = jestClient.execute(search); long end = System.currentTimeMillis(); System.out.println("在100万条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒"); return result.getSourceAsObjectList(News.class); }