Jest是Elasticsearch HTTP Rest接口的java client。
官方地址:https://github.com/searchbox-io/Jest
参考资料:http://blog.mkfree.com/posts/38#
http://download.csdn.net/download/foamflower/5272726
http://www.ibm.com/developerworks/cn/java/j-javadev2-24/index.html
IBM还有一个讲Jest的文章
感觉使用Jest还是挺方便的,官方提供了一个sample,而且源码相对来说比较容易懂。
使用的一些包:
使用Jest建立一个简单的“搜索引擎”,首先建立一个连接Client:
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;
public class _JestClient
{
private static JestClient client ;
/**
* 创建Client
* @return
*/
public static JestClient initJestClient(){
// Configuration
ClientConfig clientConfig = new ClientConfig();
LinkedHashSet<String> servers = new LinkedHashSet<String>();
servers.add("http://localhost:9200/");
clientConfig.getProperties().put(ClientConstants.SERVER_LIST, servers);
//为什么这个会错呀?
//clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false);
clientConfig.getProperties().put(ClientConstants.IS_MULTI_THREADED, false);
// Construct a new Jest client according to configuration via factory
JestClientFactory factory = new JestClientFactory();
factory.setClientConfig(clientConfig);
if(client == null){
client = factory.getObject();
} else{
System.out.println("client null");
}
return client;
}
/**
* 关闭Client
*/
public void closeJestClient(){
if(null != client)
((io.searchbox.client.JestClient) client).shutdownClient();
}
}
创建索引
import java.io.IOException;
import io.searchbox.client.JestClient;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
/**
*
* @author jk
*
*/
public class NewsIndex {
public void indexNews(JestClient client) throws IOException
{
/**
* if want't use bulk api:
* Index index = new Index.Builder(new Object()).index("articles").type("article").build();
* elasticSearchClient.execute(index);
*
*/
Long start = System.currentTimeMillis();
// Use bulk
Bulk bulk = new Bulk("news","news");
News news1 = new News();
news1.setId(1L);
news1.setTitle("Kimiy Boy Girl");
news1.setContent("The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. " +
"The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. " +
"It was written in stages between 1937 and 1949, much of it during World War II.[1] It is the third best-selling novel ever written, with over 150 million copies sold.");
bulk.addIndex(new Index.Builder(news1).build());
System.out.println("create");
for(int i=2; i<100000; i++){
News news = new News();
news.setId((long)i);
news.setTitle("Robert Anthony Salvatore");
news.setContent("Homeland follows the story of Drizzt from around the time and circumstances of his birth and his upbringing amongst the drow (dark elves). " +
"The book takes the reader into Menzoberranzan, the drow home city. From here, the reader follows Drizzt on his quest to follow his principles in a land where such " +
"feelings are threatened by all his family including his mother Matron Malice. In an essence, the book introduces Drizzt Do'Urden," +
" one of Salvatore's more famous characters from the Icewind Dale Trilogy.");
bulk.addIndex(new Index.Builder(news).build());
}
try {
//Delete news index if it is exists
DeleteIndex deleteIndex = new DeleteIndex("news");
client.execute(deleteIndex);
// Create articles index
CreateIndex createIndex = new CreateIndex("news");
client.execute(createIndex);
client.execute(bulk);
Long end = System.currentTimeMillis();
System.out.println("time:"+(end-start)+"mm");
} catch (Exception e) {
e.printStackTrace();
}
}
}
搜索
import java.util.List;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Search;
public class NewsSearch {
//Count
public void search(JestClient client){
long start = System.currentTimeMillis();
try {
String query = "{\"query\":{\"term\":{\"title\":\"Robert\"}}}";
Search search = new Search(query);
// multiple index or types can be added.
search.addIndex("news");
System.out.println("index exist is "+search.isIndexExist("news"));
JestResult result = client.execute(search);
List<News> list = result.getSourceAsObjectList(News.class);
System.out.println("list:"+list.size());
for (News news : list) {
System.out.println("search result is 【Id:" + news.getId()+",Title:"+news.getTitle()+",Content:"+news.getContent()+"】");
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("times out:"+(end-start)+"mm");
}
public void search1(JestClient client, 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("news");
JestResult result = client.execute(search);
List list = result.getSourceAsObjectList(News.class);
long end = System.currentTimeMillis();
System.out.println("在100万条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒");
for (int i = 0; i < list.size(); i++) {
News news = (News) list.get(i);
System.out.println(news.getId() + " " + news.getTitle() + " " + news.getContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试
import java.io.IOException;
import io.searchbox.client.JestClient;
/**
* 测试
* @author jk
*
*/
public class Test {
public static void main(String[] args) throws IOException{
//create client
JestClient client = _JestClient.initJestClient();
//create index
NewsIndex ni = new NewsIndex();
ni.indexNews(client);
//search
NewsSearch ns = new NewsSearch();
ns.search1(client,"Robert");
}
}
建立索引和搜索的效率:不知道DEBUG是什么意思?
建立索引
DEBUG - JestClientFactory.getObject(37) | Creating HTTP client based on configuration
DEBUG - JestClientFactory.getObject(63) | Default http client is created without multi threaded option
INFO - JestClientFactory.getObject(89) | Node Discovery Disabled...
DEBUG - AbstractAction.buildURI(160) | Created uri: news/news
create
DEBUG - AbstractAction.buildURI(160) | Created uri: news
DEBUG - JestHttpClient.constructHttpMethod(125) | DELETE method created based on client request
DEBUG - AbstractJestClient.createNewElasticSearchResult(68) | Request and operation succeeded
DEBUG - AbstractAction.buildURI(160) | Created uri: news
DEBUG - JestHttpClient.constructHttpMethod(121) | PUT method created based on client request
DEBUG - AbstractJestClient.createNewElasticSearchResult(68) | Request and operation succeeded
DEBUG - JestHttpClient.constructHttpMethod(116) | POST method created based on client request
DEBUG - AbstractJestClient.createNewElasticSearchResult(68) | Request and operation succeeded
time:198335mm
搜索
DEBUG - JestClientFactory.getObject(37) | Creating HTTP client based on configuration
DEBUG - JestClientFactory.getObject(63) | Default http client is created without multi threaded option
INFO - JestClientFactory.getObject(89) | Node Discovery Disabled...
DEBUG - Search.getURI(111) | Created URI for search action is : news/news/_search
DEBUG - JestHttpClient.constructHttpMethod(116) | POST method created based on client request
DEBUG - AbstractJestClient.createNewElasticSearchResult(68) | Request and operation succeeded
在100万条记录中,搜索新闻,共用时间 -->> 473 毫秒
1 Kimiy Boy Girl The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.[1] It is the third best-selling novel ever written, with over 150 million copies sold.
使用了Jest一定程度上也降低了作为初学者了解JAVA API的痛苦,很多ES的JAVA API都不太了解。
相关推荐
这是github上的jest项目
vue项目使用Jest测试入门.docx
该文档是使用Elasticsearch 6.8 和 jest 6.3 来封装的ES客户端调用代码,其中Jest常用的基本操作,可以满足日常开发需要
使用esbuild的Jest变压器 借助此转换器,您可以使用和转换(ts,js,tsx和jsx)文件 安装 npm install --save-dev esbuild-jest esbuild 设置Jest配置文件 esbuild-jest转换器应在您的Jest配置文件中使用,如下所示...
spring-data-jest, Jest的Spring Data 实现 Spring Data Jest 基于on客户端的ElasticSearch的Spring Data 实现仅在 HTTP ( 例如AWS上) 可以访问的情况下使用 Spring Data 和ElasticSearch群集。
Jest快照测试.md
开玩笑的python 开玩笑地执行python测试。...-save-dev jest将jest-python添加为jest运行程序在你的package.json " jest " : { " runner " : " jest-python " , " testMatch " : [ " **/test_*.py " ], " modul
java使用Jest Client操作Elasticsearch。自己修改了一些,测试完美运行。
jest-6.3.1.zip包含了jest-6.3.1.jar、jest-common-6.3.1.jar包。 欢迎大家下载,大家也可关注我的博客,欢迎一起交流,如有疑问请留言!
1、jest使用示例 2、java通过Jdbc连接hive 3、将hive数据导入到elasticsearch
jest 单元测试教程
test-vue-app 使用Jest测试没有配置的Vue.js组件
Jest是Elasticsearch 的Java Http Rest 客户端,ElasticSearch已经具备应用于Elasticsearch内部的Java API,但是Jest弥补了ES自有API缺少Elasticsearch Http Rest接口客户端的不足
jest-lite:实现在浏览器中运行Jest
Jest来自facebook的JavaScript测试,Jest虚拟化JavaScript环境,提供了浏览器模拟,并在workers上并行运行测试
开玩笑的夹具 帮助程序创建与jest一起使用的基于文件的装置。安装及使用npm i jest-fixture --save-devimport { environment , prepare , file , packageJson } from 'jest-fixture'import { build } from '../my-...
jest-runner-vscode 使用Jest测试框架运行VS代码测试目录安装纱yarn add jest jest-runner-vscode --devnpm npm i jest jest-runner-vscode --save-dev建立示例launch.json { " version " : " 0.2.0 " , " ...
NULL 博文链接:https://m635674608.iteye.com/blog/2233223
jest-preset-angular:Angular项目Jest配置的预设
Angular-jest-preset-angular.zip,角度项目的jest配置预设.jest-preset-angular,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发...