使用elasticsearch 2.1 和 java客户端 jest 建立springmvc项目
2016-05-24 16:00
726 查看
pom.xml 添加依赖 ,楼主的 maven仓库好像有点问题 ,好多包下不到,....
完毕........................ jest github
https://github.com/searchbox-io/Jest/tree/master/jest
<span style="white-space:pre"> </span><dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>0.1.2</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.1.2</version> </dependency>jestClient自动化配置如下
import io.searchbox.client.JestClient; import io.searchbox.client.JestClientFactory; import io.searchbox.client.config.HttpClientConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * * @Description:构造jestClient配置 * @author:yzxia * @time:2016-5-24 下午1:53:02 */ @Configuration public class SpringConfiguration { //202.105.136.18 SpringConfiguration public @Bean HttpClientConfig httpClientConfig() { String connectionUrl = "http://127.0.0.1:9200"; HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(connectionUrl).multiThreaded(true).build(); return httpClientConfig; } public @Bean JestClient jestClient() { JestClientFactory factory = new JestClientFactory(); factory.setHttpClientConfig(httpClientConfig()); return factory.getObject(); } }//搜索类 有搜索和批量添加功能
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 io.searchbox.indices.IndicesExists; import java.io.IOException; import java.util.List; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mkfree.soso.model.News; /** * * @Description:简单添加索引 简单查找索引 * @author:yzxia * @time:2016-5-24 下午4:03:11 */ @Service public class SearchService { @Autowired private JestClient jestClient; int num = 100000; String indexName= "news"; /** * * @Description:批量插入索引 * void * @exception: * @author: yzxia * @time:2016-5-24 下午4:03:31 */ public void builderSearchIndex() { long start = System.currentTimeMillis(); try { // 如果索引存在,删除索引 IndicesExists indicesExists = new IndicesExists.Builder(indexName).build(); JestResult isIndexExist = jestClient.execute(indicesExists); if (isIndexExist != null && !isIndexExist.isSucceeded()) { System.out.println("===索引不存在"); //不存在 不处理 }else{ DeleteIndex index = new DeleteIndex.Builder(indexName).build(); JestResult result = jestClient.execute(index); System.out.println("===索引删除成功"); } //创建索引 JestResult result = jestClient.execute(new CreateIndex.Builder(indexName).build()); if (result == null || !result.isSucceeded()) { throw new Exception(result.getErrorMessage() + "创建索引失败!"); } Bulk.Builder bulk = new Bulk.Builder(); // 添加添加100万条假数据去服务端(ES) for (int i = 0; i < num; i++) { News news = new News(); news.setId(i + 1); news.setTitle("你好啊" + (i + 1)); news.setContent("你好吗?噢噢噢噢哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦" + (i + 1)); Index index = new Index.Builder(news).index(indexName) .type(indexName).build(); bulk.addAction(index); } jestClient.execute(bulk.build()); } catch (Exception e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("创建索引时间:数据量是 " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒"); } /** * 搜索新闻 * * @param param * @return */ public List<News> searchsNews(String param) { try { long start = System.currentTimeMillis(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.queryStringQuery(param)); Search search = new Search.Builder(searchSourceBuilder.toString()) // multiple index or types can be added. .addIndex("news") .addIndex("news") .build(); JestResult result = jestClient.execute(search); long end = System.currentTimeMillis(); System.out.println("在" + num + "条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒"); return result.getSourceAsObjectList(News.class); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } }
完毕........................ jest github
https://github.com/searchbox-io/Jest/tree/master/jest
相关文章推荐
- Java注解一谈
- java注解框架
- 转发下Spring英文文档 学习编程English也要学好 因为要看原版的英文文档
- java的类变量、实例变量、局部变量和参数变量
- Java多线程看着一篇足够了!
- Java_通过反射调用类中的方法
- Java 接口
- java基础第十二天
- Java-集合
- Java_你应该知道的26种设计模式
- Java 基础之日期操作总结
- [自编号1-2]将spring framework源码导入Spring Tool Suite中
- [转]Java 远程通讯技术及原理分析
- IDEA新建javaWeb以及Servlet简单实现
- java中notify()和notifyall的区别
- java调用第三方包的例子
- Java 通过JDBC连接Mysql数据库的方法和实例
- [Java]ThreadPoolExecutor源码分析
- Java基础第十一天_集合
- 【转】增加eclipse的运行内存 -- 不错!!