0
点赞
收藏
分享

微信扫一扫

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


 

elasticsearch简称ES

jest

好吧下面我介绍下jest(第三方工具)

  • 它是ES的java客户端,基于http restful...
  • jest是开源的

 

首先看看项目的目录结构


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

 

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mkfree</groupId><artifactId>ES-jest</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><!-- jest --><dependency><groupId>io.searchbox</groupId><artifactId>jest</artifactId><version>0.0.2</version></dependency><!-- elasticsearch  --><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>0.20.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency></dependencies><repositories><!-- 添加 sonatype仓库--><repository><id>sonatype</id><name>Sonatype Groups</name><url>https://oss.sonatype.org/content/groups/public/</url></repository></repositories></project>

 

1.配置jest客户端

InitES类

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层..自己去理解吧..
*
*
*
*
*/publicclassInitES{/**
* 静态,单例...
*/privatestaticJestClientJestClient;/**
* 配置jest客户端,到时使用spring时,可以用配置方式 ,现在暂时使用new ...
*
* @return
*/privatestaticClientConfig clientConfig(){// es的服务端地址,暂时我是用我虚拟机的(ubuntu)做服务器String connectionUrl ="http://192.168.56.101:9200";// 一般都是9200端口ClientConfig clientConfig =newClientConfig();// 当你用集群时,就有可能会有多个es的服务端,这里我暂时没有集群LinkedHashSetservers=newLinkedHashSet();
servers.add(connectionUrl);
clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED,false);return clientConfig;}/**
* 获取一个jest的对象
*
* @return
*/publicstaticJestClient jestClient(){JestClientFactory factory =newJestClientFactory();
factory.setClientConfig(clientConfig());if(JestClient!=null){JestClient= factory.getObject();}returnJestClient;}}

News 新闻类

package com.mkfree.jest.domain;import io.searchbox.annotations.JestId;/**
* 虚拟news 搜索文章
*
*
*
*
*/publicclassNews{@JestIdprivateint id;privateString title;privateString content;publicint getId(){return id;}publicvoid setId(int id){this.id = id;}publicString getTitle(){return title;}publicvoid setTitle(String title){this.title = title;}publicString getContent(){return content;}publicvoid setContent(String content){this.content = content;}}

SearchService 搜索服务接口

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简单服务接口
*
*
*
*
*/publicclassSearchService{privatestaticJestClient jestClient =InitES.jestClient();/**
* 创建es news索引
*/publicvoid builderSearchIndex(){int num =10000;long start =System.currentTimeMillis();try{// 如果索引存在,删除索引DeleteIndex deleteIndex =newDeleteIndex("news");
jestClient.execute(deleteIndex);// 创建索引CreateIndex createIndex =newCreateIndex("news");
jestClient.execute(createIndex);// Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)Bulk bulk =newBulk("news","article");// 添加添加100万条假数据去服务端(ES)for(int i =0; i < num; i++){News news =newNews();
news.setId(i +1);
news.setTitle("elasticsearch RESTful搜索引擎-(java jest 使用[入门])"+(i +1));
news.setContent("好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)"+(i +1));
bulk.addIndex(newIndex.Builder(news).build());}
jestClient.execute(bulk);}catch(Exception e){
e.printStackTrace();}longend=System.currentTimeMillis();System.out.println("创建索引时间:数据量是 "+ num +"记录,共用时间 -->> "+(end- start)+" 毫秒");}/**
* 搜索新闻
*
* @param param
* @return
*/publicListsearchsNews(String param){try{long start =System.currentTimeMillis();QueryBuilder queryBuilder =QueryBuilders.queryString(param);Search search =newSearch(Search.createQueryWithBuilder(queryBuilder.toString()));
search.addIndex("news");
search.addType("article");JestResult result = jestClient.execute(search);longend=System.currentTimeMillis();System.out.println("在100万条记录中,搜索新闻,共用时间 -->> "+(end- start)+" 毫秒");return result.getSourceAsObjectList(News.class);}catch(IOException e){
e.printStackTrace();}catch(Exception e){
e.printStackTrace();}returnnull;}}

最后,模拟action SearchAction

package com.mkfree.jest.action;import java.util.List;import org.junit.Test;import com.mkfree.jest.domain.News;import com.mkfree.jest.service.SearchService;/**
* 简单搜索控制器,暂时用junit去代替...(大家可以想想,怎么实现成web),下一篇会结合spring springmvc jest做成web方式...
*
*
*
*
*/publicclassSearchAction{privateSearchService searchService =newSearchService();/**
* 创建news索引
*/@Testpublicvoid buildSearchIndex(){
searchService.builderSearchIndex();}/**
* 搜索新闻
*/@Testpublicvoid searchNews(){String param ="个人";Listnews= searchService.searchsNews(param);System.out.println("id 标题 内容");for(int i =0; i < news.size(); i++){News article = news.get(i);System.out.println(article.getId()+" "+ article.getTitle()+" "+ article.getContent());}}}

以后就是全部的代码了...好了,下面我们执行创建索引
运行buildSearchIndex();现在我们是虚拟10000条记录
结果:

创建索引时间:数据量是10000记录,共用时间-->>4749毫秒

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

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


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


创建的索引ES默认存放了data目录下,多了...


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

下面执行搜索 searchNews();
结果:

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

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




举报

相关推荐

0 条评论