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都不太了解。