使用elasticsearch 2.1 和 java客户端 jest 建立springmvc项目

2 篇文章 0 订阅
1 篇文章 0 订阅

pom.xml  添加依赖 ,楼主的 maven仓库好像有点问题 ,好多包下不到,....

<span style="white-space:pre">		</span><dependency>
		  <groupId>io.searchbox</groupId>
		  <artifactId>jest</artifactId>
		  <version>0.1.2</version>
		</dependency>
		<dependency>
		  <groupId>org.elasticsearch</groupId>
		  <artifactId>elasticsearch</artifactId>
		  <version>2.1.2</version>
		</dependency>
jestClient自动化配置如下
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 
 * @Description:构造jestClient配置
 * @author:yzxia
 * @time:2016-5-24 下午1:53:02
 */
@Configuration
public class SpringConfiguration {
//202.105.136.18  SpringConfiguration
	public @Bean HttpClientConfig httpClientConfig() {
		String connectionUrl = "http://127.0.0.1:9200";
		HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(connectionUrl).multiThreaded(true).build();
		return httpClientConfig;
	}

	public @Bean JestClient jestClient() {
		JestClientFactory factory = new JestClientFactory();
		factory.setHttpClientConfig(httpClientConfig());
		return factory.getObject();
	}
}
//搜索类 有搜索和批量添加功能
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 io.searchbox.indices.IndicesExists;

import java.io.IOException;
import java.util.List;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mkfree.soso.model.News;

/**
 * 
 * @Description:简单添加索引  简单查找索引
 * @author:yzxia
 * @time:2016-5-24 下午4:03:11
 */
@Service
public class SearchService {

	@Autowired
	private JestClient jestClient;
	int num = 100000;
	String indexName= "news";

	/**
	 * 
	 * @Description:批量插入索引
	 * void
	 * @exception:
	 * @author: yzxia
	 * @time:2016-5-24 下午4:03:31
	 */
	public void builderSearchIndex() {

		long start = System.currentTimeMillis();
		try {
			// 如果索引存在,删除索引
			IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
			JestResult isIndexExist = jestClient.execute(indicesExists);
			if (isIndexExist != null && !isIndexExist.isSucceeded()) {
				System.out.println("===索引不存在");
				//不存在 不处理
			}else{
				DeleteIndex index = new DeleteIndex.Builder(indexName).build();
				JestResult result = jestClient.execute(index);
				System.out.println("===索引删除成功");
			}

			//创建索引
			JestResult result = jestClient.execute(new CreateIndex.Builder(indexName).build());
			if (result == null || !result.isSucceeded()) {
				throw new Exception(result.getErrorMessage() + "创建索引失败!");
			}
			Bulk.Builder bulk = new Bulk.Builder();
			// 添加添加100万条假数据去服务端(ES)
			for (int i = 0; i < num; i++) {
				News news = new News();
				news.setId(i + 1);
				news.setTitle("你好啊" + (i + 1));
				news.setContent("你好吗?噢噢噢噢哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦"
						+ (i + 1));
				 Index index = new Index.Builder(news).index(indexName)
	                     .type(indexName).build();
				 bulk.addAction(index);
			}
			
			jestClient.execute(bulk.build());

		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("创建索引时间:数据量是  " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒");
	}

	/**
	 * 搜索新闻
	 * 
	 * @param param
	 * @return
	 */
	public List<News> searchsNews(String param) {
		try {
			long start = System.currentTimeMillis();
			SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
			searchSourceBuilder.query(QueryBuilders.queryStringQuery(param));

			Search search = new Search.Builder(searchSourceBuilder.toString())
			// multiple index or types can be added.
			.addIndex("news")
			.addIndex("news")
			.build();
			JestResult result = jestClient.execute(search);
			long end = System.currentTimeMillis();
			System.out.println("在" + num + "条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒");
			return result.getSourceAsObjectList(News.class);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}


完毕........................ jest github

https://github.com/searchbox-io/Jest/tree/master/jest 




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值