oyhk学习笔记
elasticsearch简称ES
jest
好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)
- 它是ES的java客户端,基于http restful...
- jest是开源的....其他就不清楚了,看源代码吧..哈.
如果对ES不了解请看:elasticsearch RESTful搜索引擎-简介
上一篇文章:elasticsearch RESTful搜索引擎-安装
费话不多说了,下面开始 ES -->> jest 入门
首先看看项目的目录结构
我一般习惯了用maven去管理我的项目...所以...看pom.xml吧
- <project xmlns="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层..自己去理解吧..
- *
- * @author hk
- *
- * 2013-1-12 下午11:27:37
- */
- 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;
- }
- }
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简单服务接口
- *
- * @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) {
- try {
- 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);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
最后,模拟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方式...
- *
- * @author hk
- *
- * 2013-1-12 下午11:49:02
- */
- public class SearchAction {
- private SearchService searchService = new SearchService();
- /**
- * 创建news索引
- */
- @Test
- public void buildSearchIndex() {
- searchService.builderSearchIndex();
- }
- /**
- * 搜索新闻
- */
- @Test
- public void 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的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)2
- 7 elasticsearch RESTful搜索引擎-(java jest 使用[入门])7 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)7
- 14 elasticsearch RESTful搜索引擎-(java jest 使用[入门])14 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)14
- 19 elasticsearch RESTful搜索引擎-(java jest 使用[入门])19 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)19
- 21 elasticsearch RESTful搜索引擎-(java jest 使用[入门])21 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)21
- 26 elasticsearch RESTful搜索引擎-(java jest 使用[入门])26 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)26
- 33 elasticsearch RESTful搜索引擎-(java jest 使用[入门])33 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)33
- 38 elasticsearch RESTful搜索引擎-(java jest 使用[入门])38 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)38
- 40 elasticsearch RESTful搜索引擎-(java jest 使用[入门])40 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)40
- 45 elasticsearch RESTful搜索引擎-(java jest 使用[入门])45 好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)45
搜索结果是,从10000条记录中搜索出10条记录...至于下一页的...再研究吧..这次就先不说了,等下次结合spring 时,做成一个Web项目的时候再说了(加油吧...)
源代码下载:http://blog.mkfree.com/posts/38
相关推荐
赠送jar包:elasticsearch-rest-high-level-client-6.8.3.jar;...标签:elasticsearch、client、rest、high、level、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵
本实例参考网上代码:http://www.ibm.com/developerworks/cn/java/j-javadev2-24/ 通过Jest和Junit单元测试的方法创建索引和检索 第一步:启动一个elasticsearch服务,...本实例基于: Jest 0.0.3,elasticsearch 0.20.5
本书介绍了ElasticSearch的使用、原理、系统优化与扩展应用等
spring-data-jest, Jest的Spring Data 实现 Spring Data Jest 基于on客户端的ElasticSearch的Spring Data 实现仅在 HTTP ( 例如AWS上) 可以访问的情况下使用 Spring Data 和ElasticSearch群集。
标签:elasticsearch、x、content、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
分布式搜索elasticsearch java API 之(一)--- 与集群交互 2 分布式搜索elasticsearch java API 之(二)--- put Mapping...分布式搜索elasticsearch java API 之(八)--- 使用More like this实现基于内容的推荐 12
标签:elasticsearch、client、rest、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...
赠送jar包:elasticsearch-rest-high-level-client-6.8.3.jar;...标签:elasticsearch、client、rest、high、level、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.htm
人工智能-项目实践-搜索引擎-SpringBoot+ElasticSearch全文搜索引擎
elasticsearch7.17.10-最新支持Java1.8版本
Elasticsearch(elasticsearch-8.2.0-aarch64.rpm),适用于Linux arm x86_64:它是一个高度可扩展的开源全文本搜索和分析引擎。它使您可以快速,近乎实时地存储,搜索和分析大量数据。它通常用作支持具有复杂搜索...
elasticsearch的java实现demo,包含增删改查,依赖springboot提供的elasticsearch包
Elasticsearch是一个分布式的RESTful搜索和分析引擎,可让您轻松地大规模存储,搜索和分析。 它使您可以执行和组合多种类型的搜索。 它可以无缝扩展,并提供令人难以置信的快速答案,并且您可以根据各种因素对搜索...
Elasticsearch(elasticsearch-8.2.0-windows-x86_64.zip),适用于Windows x86_64:它是一个高度可扩展的开源全文本搜索和分析引擎。它使您可以快速,近乎实时地存储,搜索和分析大量数据。它通常用作支持具有复杂...
标签:elasticsearch、x、content、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...
elasticsearch-analysis-ik-7.17.0
分布式搜索elasticsearch几个概念解析 3 分布式搜索elasticsearch单机与服务器环境搭建 4 分布式搜索elasticsearch中文分词集成 5 分布式搜索elasticsearch配置文件详解 8 分布式搜索elasticsearch安装步骤详解 12 ...