博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch spring 集成
阅读量:7089 次
发布时间:2019-06-28

本文共 23604 字,大约阅读时间需要 78 分钟。

hot3.png

elasticsearch spring 集成

项目清单

elasticsearch服务下载包括其中插件和分词

http://download.csdn.net/detail/u014201191/8809619

项目源码

资源文件

app.properties

elasticsearch.esNodes=localhost:9300elasticsearch.cluster.name=heroscluster

app.xml

elasticseach.xml

log4j.properties

### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###log4j.rootLogger=info,logfilelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Threshold=infolog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%nlog4j.appender.logfile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.logfile.File=/app/logs/mq_consumer.loglog4j.appender.logfile.datePattern='.'yyyy-MM-dd'.'log4j.appender.logfile.append=truelog4j.appender.logfile.Threshold=debuglog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n

maven管理

4.0.0
com.elasticsearch
elasticsearch
war
0.0.1-SNAPSHOT
elasticsearch Maven Webapp
http://maven.apache.org
3.1.1.RELEASE
2.0.0
2.11
3.0
2.5.3
1.7
junit
junit
3.8.1
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-jdbc
${spring.version}
fr.pilato.spring
spring-elasticsearch
1.0.0
org.elasticsearch
elasticsearch
1.0.0
org.springframework.data
spring-data-elasticsearch
1.0.0.RELEASE
com.alibaba
druid
1.0.5
net.sf.json-lib
json-lib
2.4
jdk15
org.quartz-scheduler
quartz
2.2.1
org.slf4j
slf4j-log4j12
1.7.5
elasticsearch

Java.class

Bean配置

package com.sf.heros.mq.consumer.vo;import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldIndex;import org.springframework.data.elasticsearch.annotations.FieldType;import com.sf.heros.mq.consumer.utils.APP;//@Document(indexName = APP.ESProp.INDEX_NAME, type = APP.ESProp.TYPE_TASK_INFO, indexStoreType = APP.ESProp.INDEX_STORE_TYPE, shards = APP.ESProp.SHARDS, replicas = APP.ESProp.REPLICAS, refreshInterval = APP.ESProp.REFRESH_INTERVAL)@Document(indexName = APP.ESProp.INDEX_NAME, type = APP.ESProp.TYPE_TASK_INFO)public class TaskInfo {    @Id    @Field(index = FieldIndex.not_analyzed, store = true)    private String taskId;        @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)    private Integer userId;    @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)    private String taskContent;    @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)    private String taskArea;        @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)    private String taskTags;        @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)    private Integer taskState;    @Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)    private String updateTime;    @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)    private String userNickName;        public String getTaskId() {        return taskId;    }    public void setTaskId(String taskId) {        this.taskId = taskId;    }    public Integer getUserId() {        return userId;    }    public void setUserId(Integer userId) {        this.userId = userId;    }    public String getTaskContent() {        return taskContent;    }    public void setTaskContent(String taskContent) {        this.taskContent = taskContent;    }    public String getTaskArea() {        return taskArea;    }    public void setTaskArea(String taskArea) {        this.taskArea = taskArea;    }    public String getTaskTags() {		return taskTags;	}	public void setTaskTags(String taskTags) {		this.taskTags = taskTags;	}	public Integer getTaskState() {        return taskState;    }    public void setTaskState(Integer taskState) {        this.taskState = taskState;    }    public String getUpdateTime() {        return updateTime;    }    public void setUpdateTime(String updateTime) {        this.updateTime = updateTime;    }    public String getUserNickName() {        return userNickName;    }    public void setUserNickName(String userNickName) {        this.userNickName = userNickName;    }    @Override    public String toString() {        return "TaskInfo [taskId=" + taskId + ", userId=" + userId                + ", taskContent=" + taskContent + ", taskArea=" + taskArea                + ", taskState=" + taskState                + ", updateTime=" + updateTime + ", userNickName="                + userNickName + "]";    }	public TaskInfo(String taskId, Integer userId, String taskContent,			String taskArea, String taskTags, Integer taskState,			String updateTime, String userNickName) {		this.taskId = taskId;		this.userId = userId;		this.taskContent = taskContent;		this.taskArea = taskArea;		this.taskTags = taskTags;		this.taskState = taskState;		this.updateTime = updateTime;		this.userNickName = userNickName;	}    public TaskInfo() {		// TODO Auto-generated constructor stub	}}
其余的类在源码中下载,此处不列出了...

常量管理

package com.sf.heros.mq.consumer.utils;import java.util.HashMap;import java.util.Map;public interface APP {    public static final Map
map = new HashMap
(); public static final String CLOSED_MSG = "#################closed####################"; public static final long DELIVERIED_TAG = -1; class ESProp { public static final String INDEX_NAME = "heros"; public static final String DAIDONGXI_INDEX_NAME = "daidongxi"; public static final String TYPE_NEWS_INFO = "news_info"; public static final String TYPE_PRODUCT_INFO = "product_info"; public static final String TYPE_STORY_INFO = "story_info"; public static final String TYPE_TASK_INFO = "task_info"; public static final String TYPE_USER_INFO = "user_info"; public static final String TYPE_BRANDCASE_INFO = "brandcase_info"; public static final String INDEX_STORE_TYPE = "memory"; public static final int SHARDS = 2; public static final int REPLICAS = 1; public static final String REFRESH_INTERVAL = "-1"; }}

增删改类

/** *@Pr锛歨eros *@Date: 2014-5-4 涓婂崍9:21:27 *@Author: seaphy *@Copyright: 漏 2012 sf-express.com Inc. All rights reserved *娉ㄦ剰锛氭湰鍐呭浠呴檺浜庨『涓伴�熻繍鍏徃鍐呴儴浼犻槄锛岀姝㈠娉勪互鍙婄敤浜庡叾浠栫殑鍟嗕笟鐩殑 */package com.sf.heros.mq.consumer.service;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;import org.elasticsearch.action.ActionFuture;import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;import org.elasticsearch.client.Client;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import org.springframework.data.elasticsearch.core.query.IndexQuery;import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;import com.sf.heros.mq.consumer.utils.APP;import com.sf.heros.mq.consumer.vo.BrandCaseInfo;import com.sf.heros.mq.consumer.vo.NewsInfo;import com.sf.heros.mq.consumer.vo.TaskInfo;import com.sf.heros.mq.consumer.vo.UserInfo;/** * @author seaphy * @date 2014-5-4 */public class ElasticsearchService {    private static final Logger logger = Logger.getLogger(ElasticsearchService.class);    @Autowired    private ElasticsearchTemplate elasticsearchTemplate;    @Autowired    private Client esClient;    public void init() {    	if (!elasticsearchTemplate.indexExists(APP.ESProp.INDEX_NAME)) {			elasticsearchTemplate.createIndex(APP.ESProp.INDEX_NAME);		}    	elasticsearchTemplate.putMapping(TaskInfo.class);    	elasticsearchTemplate.putMapping(NewsInfo.class);    }    public boolean update(List
taskInfoList) { List
queries = new ArrayList
(); for (TaskInfo taskInfo : taskInfoList) { IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build(); queries.add(indexQuery); } elasticsearchTemplate.bulkIndex(queries); return true; } public boolean insertOrUpdateTaskInfo(List
taskInfoList) { List
queries = new ArrayList
(); for (TaskInfo taskInfo : taskInfoList) { IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build(); queries.add(indexQuery); } elasticsearchTemplate.bulkIndex(queries); return true; } public boolean insertOrUpdateNewsInfo(List
newsInfos) { List
queries = new ArrayList
(); for (NewsInfo newsInfo : newsInfos) { IndexQuery indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build(); queries.add(indexQuery); } elasticsearchTemplate.bulkIndex(queries); return true; } public boolean insertOrUpdateNewsInfo(NewsInfo newsInfo) { try { IndexQuery indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build(); elasticsearchTemplate.index(indexQuery); return true; } catch (Exception e) { logger.error("insert or update news info error.", e); return false; } } public boolean insertOrUpdateTaskInfo(TaskInfo taskInfo) { try { IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build(); elasticsearchTemplate.index(indexQuery); return true; } catch (Exception e) { logger.error("insert or update task info error.", e); return false; } } public boolean insertOrUpdateUserInfo(UserInfo userInfo) { try { IndexQuery indexQuery = new IndexQueryBuilder().withId(userInfo.getUserId()).withObject(userInfo).build(); elasticsearchTemplate.index(indexQuery); return true; } catch (Exception e) { logger.error("insert or update user info error.", e); return false; } } public
boolean deleteById(String id, Class
clzz) { try { elasticsearchTemplate.delete(clzz, id); return true; } catch (Exception e) { logger.error("delete " + clzz + " by id " + id + " error.", e); return false; } } /** * 检查健康状态 * @author 高国藩 * @date 2015年6月15日 下午6:59:47 * @return */ public boolean ping() { try { ActionFuture
health = esClient.admin().cluster().health(new ClusterHealthRequest()); ClusterHealthStatus status = health.actionGet().getStatus(); if (status.value() == ClusterHealthStatus.RED.value()) { throw new RuntimeException("elasticsearch cluster health status is red."); } return true; } catch (Exception e) { logger.error("ping elasticsearch error.", e); return false; } } public boolean insertOrUpdateBrandCaseInfo(BrandCaseInfo brandCaseInfo) { try { IndexQuery indexQuery = new IndexQueryBuilder() .withId(brandCaseInfo.getId()).withObject(brandCaseInfo).build(); elasticsearchTemplate.index(indexQuery); return true; } catch (Exception e) { logger.error("insert or update brandcase info error.", e); return false; } }}

查询类

package com.sf.daidongxi.web.service;import java.util.ArrayList;import java.util.Collection;import java.util.List;import java.util.Map;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.apache.lucene.queries.TermFilter;import org.apache.lucene.queryparser.xml.builders.FilteredQueryBuilder;import org.elasticsearch.action.search.SearchRequestBuilder;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.action.search.SearchType;import org.elasticsearch.client.Client;import org.elasticsearch.index.query.BoolFilterBuilder;import org.elasticsearch.index.query.FilterBuilder;import org.elasticsearch.index.query.FilterBuilders;import org.elasticsearch.index.query.MatchQueryBuilder;import org.elasticsearch.index.query.QueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.index.query.QueryStringQueryBuilder;import org.elasticsearch.index.query.RangeFilterBuilder;import org.elasticsearch.index.query.TermsQueryBuilder;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.sort.SortOrder;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import sun.misc.Contended;public class ElasticsearchService implements InitializingBean {	private static final Logger logger = Logger			.getLogger(ElasticsearchService.class);	@Autowired	private Client client;	private String esIndexName = "heros";	@Autowired	private ElasticsearchTemplate elasticsearchTemplate;	@Autowired	private Client esClient;	/** 查询 id */	public List
queryId(String type, String[] fields, String content, String sortField, SortOrder order, int from, int size) { SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); QueryStringQueryBuilder queryString = QueryBuilders.queryString("\"" + content + "\""); for (String k : fields) { queryString.field(k); } queryString.minimumShouldMatch("10"); reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString)) .setExplain(true); if (StringUtils.isNotEmpty(sortField) && order != null) { reqBuilder.addSort(sortField, order); } if (from >= 0 && size > 0) { reqBuilder.setFrom(from).setSize(size); } SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); ArrayList
results = new ArrayList
(); for (SearchHit hit : hits) { results.add(hit.getId()); } return results; } /** * 查询得到结果为Map集合 * * @author 高国藩 * @date 2015年6月15日 下午8:46:13 * @param type * 表 * @param fields * 字段索引 * @param content * 查询的值 * @param sortField * 排序的字段 * @param order * 排序的規則 * @param from * 分頁 * @param size * @return */ public List
> queryForObject(String type, String[] fields, String content, String sortField, SortOrder order, int from, int size) { SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); QueryStringQueryBuilder queryString = QueryBuilders.queryString("\"" + content + "\""); for (String k : fields) { queryString.field(k); } queryString.minimumShouldMatch("10"); reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString)) .setExplain(true); if (StringUtils.isNotEmpty(sortField) && order != null) { reqBuilder.addSort(sortField, order); } if (from >= 0 && size > 0) { reqBuilder.setFrom(from).setSize(size); } SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); List
> results = new ArrayList
>(); for (SearchHit hit : hits) { results.add(hit.getSource()); } return results; } /** * QueryBuilders 所有查询入口 */ public List
> queryForObjectEq(String type, String[] fields, String content, String sortField, SortOrder order, int from, int size) { SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); QueryStringQueryBuilder queryString = QueryBuilders.queryString("\"" + content + "\""); for (String k : fields) { queryString.field(k); } queryString.minimumShouldMatch("10"); reqBuilder.setQuery(QueryBuilders.boolQuery().must(queryString)) .setExplain(true); if (StringUtils.isNotEmpty(sortField) && order != null) { reqBuilder.addSort(sortField, order); } if (from >= 0 && size > 0) { reqBuilder.setFrom(from).setSize(size); } SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); List
> results = new ArrayList
>(); for (SearchHit hit : hits) { results.add(hit.getSource()); } return results; } /** * 多个文字记不清是那些字,然后放进去查询 * * @author 高国藩 * @date 2015年6月16日 上午9:56:08 * @param type * @param field * @param countents * @param sortField * @param order * @param from * @param size * @return */ public List
> queryForObjectNotEq(String type, String field, Collection
countents, String sortField, SortOrder order, int from, int size) { SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); List
contents = new ArrayList
(); for (String content : countents) { contents.add("\"" + content + "\""); } TermsQueryBuilder inQuery = QueryBuilders.inQuery(field, contents); inQuery.minimumShouldMatch("10"); reqBuilder.setQuery(QueryBuilders.boolQuery().mustNot(inQuery)) .setExplain(true); if (StringUtils.isNotEmpty(sortField) && order != null) { reqBuilder.addSort(sortField, order); } if (from >= 0 && size > 0) { reqBuilder.setFrom(from).setSize(size); } SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); List
> results = new ArrayList
>(); for (SearchHit hit : hits) { results.add(hit.getSource()); } return results; } /** * Filters 查询方式 * * 1. 1)QueryBuilders.queryString 获得基本查询 * 2)FilteredQueryBuilder query = QueryBuilders.filteredQuery(queryString,FilterBuilder) * 3)通过上面封装成为查询,将这个query插入到reqBuilder中;完成操作 * * 2.在 reqBuilder.setQuery(query); * * 3.介绍在2)中的FilterBuilder各种构造方式-参数都可以传String类型即可 * FilterBuilders.rangeFilter("taskState").lt(20) 小于 、 lte(20) 小于等于 * FilterBuilders.rangeFilter("taskState").gt(20)) 大于 、 gte(20) 大于等于 * FilterBuilders.rangeFilter("taskState").from(start).to(end)) 范围,也可以指定日期,用字符串就ok了 * @author 高国藩 * @date 2015年6月15日 下午10:06:05 * @param type * @param field * @param countents * @param sortField * @param order * @param from * @param size * @return */ public List
> queryForObjectForElasticSerch(String type, String field, String content,int start,int end) { SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); QueryStringQueryBuilder queryString = QueryBuilders.queryString("\"" + content + "\""); queryString.field(field); queryString.minimumShouldMatch("10"); reqBuilder.setQuery(QueryBuilders.filteredQuery(queryString, FilterBuilders.rangeFilter("taskState").from(start).to(end))) .setExplain(true); SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); List
> results = new ArrayList
>(); for (SearchHit hit : hits) { results.add(hit.getSource()); } return results; } public void afterPropertiesSet() throws Exception { System.out.println("init..."); }}

测试

package com.sf.heros.mq.consumer;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.List;import java.util.Map;import org.apache.log4j.Logger;import org.elasticsearch.search.sort.SortOrder;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.sf.heros.mq.consumer.service.ElasticsearchService;import com.sf.heros.mq.consumer.utils.APP;import com.sf.heros.mq.consumer.vo.TaskInfo;public class AppMain {	private static final Logger logger = Logger.getLogger(AppMain.class);	public void start() {		ClassPathXmlApplicationContext context = null;		try {			context = new ClassPathXmlApplicationContext("classpath:app.xml");		} catch (Exception e) {			logger.error("An error occurred, applicationContext will close.", e);			if (context != null) {				context.close();			}			context = null;			logger.error(APP.CLOSED_MSG);		}	}	/**	 * 插入	* @author 高国藩	* @date 2015年6月16日 上午10:14:21	 */	@Test	public void insertNo() {		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(				"classpath:app.xml");		ElasticsearchService service = context				.getBean(ElasticsearchService.class);		List
taskInfoList = new ArrayList
(); for (int i = 0; i < 20; i++) { taskInfoList.add(new TaskInfo(String.valueOf((i + 5)), i + 5, "高国藩" + i, "taskArea", "taskTags", i + 5, "1996-02-03", "霍华德")); } service.insertOrUpdateTaskInfo(taskInfoList); } /** * 查询 * @author 高国藩 * @date 2015年6月16日 上午10:14:21 */ @Test public void serchNo() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:app.xml"); com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context .getBean("es"); List
> al = service.queryForObject("task_info", new String[] { "taskContent", "taskArea" }, "高国藩", "taskArea", SortOrder.DESC, 0, 2); for (int i = 0; i < al.size(); i++) { System.out.println(al.get(i)); } } /** * filter查询 * @author 高国藩 * @date 2015年6月16日 上午10:14:21 */ @Test public void serchFilter() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:app.xml"); com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context .getBean("es"); List
> al = service.queryForObjectForElasticSerch("task_info", "taskContent", "高",19,20); for (int i = 0; i < al.size(); i++) { System.out.println(al.get(i)); } }}

源码项目下载

http://download.csdn.net/detail/u014201191/8812073

转载于:https://my.oschina.net/gaoguofan/blog/753401

你可能感兴趣的文章