`
381895649
  • 浏览: 227758 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多

 

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

 

本文章来自:http://blog.mkfree.com/posts/38

  • 大小: 134 KB
  • 大小: 14.4 KB
  • 大小: 158.4 KB
  • 大小: 26.3 KB
1
1
分享到:
评论
3 楼 zhaobin87 2014-03-12  
千万级别的es测试了没呢?
2 楼 381895649 2013-01-14  
java10000 写道
整个浏览下来感觉还不错,但是我觉得数据量还是有点小,1w的场景实在是太少见,至少也来个百万级。



迟点做成一个web项目时,模拟数据量上千万级别...再看看效率方面如何
1 楼 java10000 2013-01-14  
整个浏览下来感觉还不错,但是我觉得数据量还是有点小,1w的场景实在是太少见,至少也来个百万级。

相关推荐

Global site tag (gtag.js) - Google Analytics