elasticsearch有丰富的客户端,java客户端有Jest。其原文介绍如下:

Jest is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.

A sample Java application using Jest can be found on GitHub https://github.com/searchbox-io/java-jest-sample.

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

下面我们做一个很简单的实例,以下几个功能:

1)批量添加1000个user对象;

2)通过id进行查询;

3)通过name进行查询;

user对象定义:

package com.fox.c1;

import io.searchbox.annotations.JestId;

/**
* @author huangfox
* @date 2014年1月22日 下午5:31:52
*
*/
public class User {
@JestId
private Long id;
private String name;
private int age; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

  

简单实现一个工厂类,用来获得JestHttpClient,如下:

package com.fox.c1;

import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.http.JestHttpClient; /**
* @author huangfox
* @date 2014年1月22日 下午5:16:15
*
*/
public class ESFactory {
private static JestHttpClient client; private ESFactory() { } public synchronized static JestHttpClient getClient() {
if (client == null) {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(
"http://localhost:9200").multiThreaded(true).build());
client = (JestHttpClient) factory.getObject();
}
return client;
} public static void main(String[] args) {
JestHttpClient client = ESFactory.getClient();
System.out.println(client.getAsyncClient());
System.out.println(client.getServers());
client.shutdownClient();
}
}

  

测试类:

package com.fox.c1;

import io.searchbox.client.JestResult;
import io.searchbox.client.http.JestHttpClient;
import io.searchbox.core.Get;
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 org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder; /**
* @author huangfox
* @date 2014年1月22日 下午5:32:20
*
*/
public class ESTest { private JestHttpClient client = ESFactory.getClient(); /**
* indexing
*
* @param indexName
*/
public void index(String indexName) {
try {
// drop
DeleteIndex dIndex = new DeleteIndex(new DeleteIndex.Builder(
indexName));
client.execute(dIndex);
// create
CreateIndex cIndex = new CreateIndex(new CreateIndex.Builder(
indexName));
client.execute(cIndex);
// add doc
for (int i = 0; i < 1000; i++) { User user = new User();
user.setId(new Long(i));
user.setName("huang fox " + i);
user.setAge(i % 100);
Index index = new Index.Builder(user).index(indexName)
.type(indexName).build();
client.execute(index);
}
//
client.shutdownClient();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* getting by id
*
* @param indexName
* @param query
*/
public void get(String indexName, String query) {
Get get = new Get.Builder(indexName, query).build();
try {
JestResult rs = client.execute(get);
System.out.println(rs.getJsonString());
//
client.shutdownClient();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* query
*
* @param query
*/
public void search(String query) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryString(query));
searchSourceBuilder.field("name");
Search search = new Search.Builder(searchSourceBuilder.toString())
.build();
try {
JestResult rs = client.execute(search);
System.out.println(rs.getJsonString());
//
client.shutdownClient();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
ESTest t = new ESTest();
String indexName = "user";
// indexing
t.index(indexName);
// getting by id
t.get(indexName, "2");
// query
String query = "fox";
t.search(query);
} }

  

index 批量添加建议使用bulk,效率高(减少通讯次数):

public void indexBulk(String indexName) {
try {
// drop
DeleteIndex dIndex = new DeleteIndex(new DeleteIndex.Builder(
indexName));
client.execute(dIndex);
// create
CreateIndex cIndex = new CreateIndex(new CreateIndex.Builder(
indexName));
client.execute(cIndex);
// add doc
Bulk.Builder bulkBuilder = new Bulk.Builder();
for (int i = 0; i < 1000; i++) { User user = new User();
user.setId(new Long(i));
user.setName("huang fox " + i);
user.setAge(i % 100);
Index index = new Index.Builder(user).index(indexName)
.type(indexName).build();
bulkBuilder.addAction(index);
}
client.execute(bulkBuilder.build());
//
client.shutdownClient();
} catch (IOException e) {
e.printStackTrace();
}
}

  

elasticsearch 口水篇(3)java客户端 - Jest的更多相关文章

  1. elasticsearch 口水篇(4)java客户端 - 原生esClient

    上一篇(elasticsearch 口水篇(3)java客户端 - Jest)Jest是第三方客户端,基于REST Api进行调用(httpClient),本篇简单介绍下elasticsearch原生 ...

  2. Elasticsearch及java客户端jest使用

    本文使用Github中的Elasticsearch-rtf,已经集成了众多的插件,例如必须使用的中文分词等,可以简单的通过配置来启用中文分词.本文主要分为以下几部分: 1.配置和启用中文分词: 2.定 ...

  3. Elasticsearch的java客户端JEST的操作

    准备 把Elasticsearch安装好 安装百度上有很多资料. 导入必要的包 <parent> <groupId>org.springframework.boot</g ...

  4. elasticsearch 口水篇(7) Eclipse中部署ES源码、运行

    ES源码可以直接从svn下载 https://github.com/elasticsearch/elasticsearch 下载后,用Maven导入(import——>Existing Mave ...

  5. elasticsearch 口水篇(6) Mapping 定义索引

    前面我们感觉ES就想是一个nosql数据库,支持Free Schema. 接触过Lucene.solr的同学这时可能会思考一个问题——怎么定义document中的field?store.index.a ...

  6. elasticsearch 口水篇(2)CRUD Sense

    Sense 为了方便.直观的使用es的REST Api,我们可以使用sense.Sense是Chrome浏览器的一个插件,使用简单. 如图: Sense安装: https://chrome.googl ...

  7. elasticsearch 口水篇(1) 安装、插件

    一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...

  8. elasticsearch 口水篇(5)es分布式集群初探

    es有很多特性,分布式.副本集.负载均衡.容灾等. 我们先搭建一个很简单的分布式集群(伪),在同一机器上配置三个es,配置分别如下: cluster.name: foxCluster node.nam ...

  9. elasticsearch 口水篇(8)分词 中文分词 ik插件

    先来一个标准分词(standard),配置如下: curl -XPUT localhost:9200/local -d '{ "settings" : { "analys ...

随机推荐

  1. [LeetCode&Python] Problem 653. Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  2. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  3. input标签(按钮)

    按钮: <input type="button" name="..." value="..." /> <input typ ...

  4. vue查缺补漏题

    一.对于MVVM的理解? MVVM 是 Model-View-ViewModel 的缩写.Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑.View 代表UI 组件,它负责将数 ...

  5. personal的制作

    <style>  .personal{   float: right;   height: 40px;   line-height: 40px;   width: 200px;  }  . ...

  6. react 路由跳转问题

    1.采用Link方法跳转 <Link to="/Index2" > 不要用link,回不来,也不能next </Link> 2.用context控制路由跳转 ...

  7. lesson4Embedding-fastai

    dense layer:mnist识别中,需要十组dense权重矩阵来计算这十个输出内容,conv矩阵每一个元素乘以另一个矩阵的元素并相加,得到一个值,最后加上sigmoid(softmax在二元情况 ...

  8. 腾讯云JavaWeb环境配置

    腾讯云服务器Centos7系统配置javaWeb运行环境 java1.8 运行命令列表 yum list java-* 安装相应版本的jdk,一般含有devel的就是真正的jdk,如:java-1.8 ...

  9. 无用之学matplotlib,numpy,pandas

    一.matplotlib学习 matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建 例子1: # coding=utf- from ...

  10. ODOO区分测试库和正式库的简单方法

    ODOO区分测试库和正式库的简单方法.1. 打开 开发者模式,右上角能显示数据库名称,缺点是,太耗系统资源了,数据多的时候就明显感觉慢了.2. 安装社区的显示测试帐套的模块, 若是正式环境还是尽量少装 ...