`
m635674608
  • 浏览: 4905174 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

elasticsearch RESTful搜索引擎-(java jest 使用[入门])

 
阅读更多

elasticsearch简称ES

jest

好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)

  • 它是ES的java客户端,基于http restful...
  • jest是开源的....其他就不清楚了,看源代码吧..哈.

如果对ES不了解请看:elasticsearch RESTful搜索引擎-简介

上一篇文章:elasticsearch RESTful搜索引擎-安装

费话不多说了,下面开始 ES -->> jest 入门

 

首先看看项目的目录结构

我一般习惯了用maven去管理我的项目...所以...看pom.xml吧 

 

Xml代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.mkfree</groupId>  
  5.     <artifactId>ES-jest</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <dependencies>  
  9.         <!-- jest -->  
  10.         <dependency>  
  11.             <groupId>io.searchbox</groupId>  
  12.             <artifactId>jest</artifactId>  
  13.             <version>0.0.2</version>  
  14.         </dependency>  
  15.         <!-- elasticsearch  -->  
  16.         <dependency>  
  17.             <groupId>org.elasticsearch</groupId>  
  18.             <artifactId>elasticsearch</artifactId>  
  19.             <version>0.20.2</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>junit</groupId>  
  23.             <artifactId>junit</artifactId>  
  24.             <version>4.10</version>  
  25.         </dependency>  
  26.     </dependencies>  
  27.     <repositories>  
  28.         <!-- 添加 sonatype仓库-->  
  29.         <repository>  
  30.             <id>sonatype</id>  
  31.             <name>Sonatype Groups</name>  
  32.             <url>https://oss.sonatype.org/content/groups/public/</url>  
  33.         </repository>  
  34.     </repositories>  
  35. </project>  

 1.配置jest客户端

 

InitES类

 

Java代码   收藏代码
  1. package com.mkfree.jest.config;  
  2.   
  3. import io.searchbox.client.JestClient;  
  4. import io.searchbox.client.JestClientFactory;  
  5. import io.searchbox.client.config.ClientConfig;  
  6. import io.searchbox.client.config.ClientConstants;  
  7.   
  8. import java.util.LinkedHashSet;  
  9.   
  10. /** 
  11.  * 初始化连接es服务端,这里相当于dao层..自己去理解吧.. 
  12.  *  
  13.  * @author hk 
  14.  *  
  15.  *         2013-1-12 下午11:27:37 
  16.  */  
  17. public class InitES {  
  18.   
  19.     /** 
  20.      * 静态,单例... 
  21.      */  
  22.     private static JestClient JestClient;  
  23.   
  24.     /** 
  25.      * 配置jest客户端,到时使用spring时,可以用配置方式 ,现在暂时使用new ... 
  26.      *  
  27.      * @return 
  28.      */  
  29.     private static ClientConfig clientConfig() {  
  30.         // es的服务端地址,暂时我是用我虚拟机的(ubuntu)做服务器  
  31.         String connectionUrl = "http://192.168.56.101:9200";// 一般都是9200端口  
  32.         ClientConfig clientConfig = new ClientConfig();  
  33.         // 当你用集群时,就有可能会有多个es的服务端,这里我暂时没有集群  
  34.         LinkedHashSetservers = new LinkedHashSet();  
  35.         servers.add(connectionUrl);  
  36.         clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);  
  37.         clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false);  
  38.         return clientConfig;  
  39.     }  
  40.   
  41.     /** 
  42.      * 获取一个jest的对象 
  43.      *  
  44.      * @return 
  45.      */  
  46.     public static JestClient jestClient() {  
  47.         JestClientFactory factory = new JestClientFactory();  
  48.         factory.setClientConfig(clientConfig());  
  49.         if (JestClient != null) {  
  50.             JestClient = factory.getObject();  
  51.         }  
  52.         return JestClient;  
  53.     }  
  54. }  

 

 News 新闻类

 

Java代码   收藏代码
  1. package com.mkfree.jest.domain;  
  2.   
  3. import io.searchbox.annotations.JestId;  
  4.   
  5. /** 
  6.  * 虚拟news 搜索文章 
  7.  *  
  8.  * @author hk 
  9.  *  
  10.  *         2013-1-12 下午11:38:29 
  11.  */  
  12. public class News {  
  13.   
  14.     @JestId  
  15.     private int id;  
  16.     private String title;  
  17.     private String content;  
  18.   
  19.     public int getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(int id) {  
  24.         this.id = id;  
  25.     }  
  26.   
  27.     public String getTitle() {  
  28.         return title;  
  29.     }  
  30.   
  31.     public void setTitle(String title) {  
  32.         this.title = title;  
  33.     }  
  34.   
  35.     public String getContent() {  
  36.         return content;  
  37.     }  
  38.   
  39.     public void setContent(String content) {  
  40.         this.content = content;  
  41.     }  
  42.   
  43. }  

 

 SearchService 搜索服务接口

 

Java代码   收藏代码
  1. package com.mkfree.jest.service;  
  2.   
  3. import io.searchbox.client.JestClient;  
  4. import io.searchbox.client.JestResult;  
  5. import io.searchbox.core.Bulk;  
  6. import io.searchbox.core.Index;  
  7. import io.searchbox.core.Search;  
  8. import io.searchbox.indices.CreateIndex;  
  9. import io.searchbox.indices.DeleteIndex;  
  10.   
  11. import java.io.IOException;  
  12. import java.util.List;  
  13.   
  14. import org.elasticsearch.index.query.QueryBuilder;  
  15. import org.elasticsearch.index.query.QueryBuilders;  
  16.   
  17. import com.mkfree.jest.config.InitES;  
  18. import com.mkfree.jest.domain.News;  
  19.   
  20. /** 
  21.  * es简单服务接口 
  22.  *  
  23.  * @author hk 
  24.  *  
  25.  *         2013-1-12 下午11:47:16 
  26.  */  
  27. public class SearchService {  
  28.   
  29.     private static JestClient jestClient = InitES.jestClient();  
  30.   
  31.     /** 
  32.      * 创建es news索引 
  33.      */  
  34.     public void builderSearchIndex() {  
  35.         int num = 10000;  
  36.         long start = System.currentTimeMillis();  
  37.         try {  
  38.             // 如果索引存在,删除索引  
  39.             DeleteIndex deleteIndex = new DeleteIndex("news");  
  40.             jestClient.execute(deleteIndex);  
  41.   
  42.             // 创建索引  
  43.             CreateIndex createIndex = new CreateIndex("news");  
  44.             jestClient.execute(createIndex);  
  45.             // Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)  
  46.             Bulk bulk = new Bulk("news""article");  
  47.             // 添加添加100万条假数据去服务端(ES)  
  48.             for (int i = 0; i < num; i++) {  
  49.                 News news = new News();  
  50.                 news.setId(i + 1);  
  51.                 news.setTitle("elasticsearch RESTful搜索引擎-(java jest 使用[入门])" + (i + 1));  
  52.                 news.setContent("好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)"  
  53.                         + (i + 1));  
  54.                 bulk.addIndex(new Index.Builder(news).build());  
  55.             }  
  56.             jestClient.execute(bulk);  
  57.         } catch (Exception e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.         long end = System.currentTimeMillis();  
  61.         System.out.println("创建索引时间:数据量是  " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒");  
  62.     }  
  63.   
  64.     /** 
  65.      * 搜索新闻 
  66.      *  
  67.      * @param param 
  68.      * @return 
  69.      */  
  70.     public ListsearchsNews(String param) {  
  71.         try {  
  72.             long start = System.currentTimeMillis();  
  73.             QueryBuilder queryBuilder = QueryBuilders.queryString(param);  
  74.             Search search = new Search(Search.createQueryWithBuilder(queryBuilder.toString()));  
  75.             search.addIndex("news");  
  76.             search.addType("article");  
  77.             JestResult result = jestClient.execute(search);  
  78.             long end = System.currentTimeMillis();  
  79.             System.out.println("在100万条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒");  
  80.             return result.getSourceAsObjectList(News.class);  
  81.         } catch (IOException e) {  
  82.             e.printStackTrace();  
  83.         } catch (Exception e) {  
  84.             e.printStackTrace();  
  85.         }  
  86.         return null;  
  87.     }  
  88. }  

 

 最后,模拟action SearchAction

 

Java代码   收藏代码
  1. package com.mkfree.jest.action;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. import com.mkfree.jest.domain.News;  
  8. import com.mkfree.jest.service.SearchService;  
  9.   
  10. /** 
  11.  * 简单搜索控制器,暂时用junit去代替...(大家可以想想,怎么实现成web),下一篇会结合spring springmvc jest做成web方式... 
  12.  *  
  13.  * @author hk 
  14.  *  
  15.  *         2013-1-12 下午11:49:02 
  16.  */  
  17. public class SearchAction {  
  18.   
  19.     private SearchService searchService = new SearchService();  
  20.   
  21.     /** 
  22.      * 创建news索引 
  23.      */  
  24.     @Test  
  25.     public void buildSearchIndex() {  
  26.         searchService.builderSearchIndex();  
  27.     }  
  28.   
  29.     /** 
  30.      * 搜索新闻 
  31.      */  
  32.     @Test  
  33.     public void searchNews() {  
  34.         String param = "个人";  
  35.         Listnews = searchService.searchsNews(param);  
  36.         System.out.println("id   标题                                           内容");  
  37.         for (int i = 0; i < news.size(); i++) {  
  38.             News article = news.get(i);  
  39.             System.out.println(article.getId() + "   " + article.getTitle() + "   " + article.getContent());  
  40.         }  
  41.     }  
  42. }  

 以后就是全部的代码了...好了,下面我们执行创建索引

 

运行buildSearchIndex();现在我们是虚拟10000条记录
结果:

 

Java代码   收藏代码
  1. 创建索引时间:数据量是  10000记录,共用时间 -->> 4749 毫秒  

 效率方面感觉还好吧...

 

现在我们看回服务器输出的日志信息是什么..

红色框里,看到删除news索引后重新创建news索引,现在看看服务器那边的目录结构

创建的索引ES默认存放了data目录下,多了一个nodes的目录..ES的索引文件就保存在这里...概念性的理解我不多说了,我也不是很熟悉,慢慢研究...

下面执行搜索 searchNews();

结果:

 

Java代码   收藏代码
  1. 10000条记录中,搜索新闻,共用时间 -->> 260 毫秒  
  2. id   标题                                                        内容  
  3. 2   elasticsearch RESTful搜索引擎-(java jest 使用[入门])2         好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)2  
  4. 7   elasticsearch RESTful搜索引擎-(java jest 使用[入门])7         好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)7  
  5. 14   elasticsearch RESTful搜索引擎-(java jest 使用[入门])14       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)14  
  6. 19   elasticsearch RESTful搜索引擎-(java jest 使用[入门])19       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)19  
  7. 21   elasticsearch RESTful搜索引擎-(java jest 使用[入门])21       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)21  
  8. 26   elasticsearch RESTful搜索引擎-(java jest 使用[入门])26       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)26  
  9. 33   elasticsearch RESTful搜索引擎-(java jest 使用[入门])33       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)33  
  10. 38   elasticsearch RESTful搜索引擎-(java jest 使用[入门])38       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)38  
  11. 40   elasticsearch RESTful搜索引擎-(java jest 使用[入门])40       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)40  
  12. 45   elasticsearch RESTful搜索引擎-(java jest 使用[入门])45       好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)45  

 

 搜索结果是,从10000条记录中搜索出10条记录...至于下一页的...再研究吧..这次就先不说了,等下次结合spring 时,做成一个Web项目的时候再说了(加油吧...)

源代码下载:http://blog.mkfree.com/posts/38

 

本文章来自:http://blog.mkfree.com/posts/38

 

 

http://jkhhuse.iteye.com/blog/1871575

http://oyhk.iteye.com/blog/1769211

 

http://www.searchly.com/documentation/developer-api-guide/java-jest/

 

 

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/search.html#msearch

http://www.cnblogs.com/huangfox/p/3542858.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics