项目清单
elasticsearch服务下载包括其中插件和分词
http://download.csdn.net/detail/u014201191/8809619
项目源码
资源文件
app.properties
[html] view
plaincopy
- elasticsearch.esNodes=localhost:9300
- elasticsearch.cluster.name=heroscluster
app.xml
[html] view
plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
- http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <context:annotation-config />
- <!-- 自动扫描所有注解该路径 -->
- <!-- <context:component-scan base-package="com.sf.heros.mq.*" /> -->
- <context:property-placeholder location="classpath:/app.properties" />
- <import resource="elasticseach.xml" />
- </beans>
elasticseach.xml
[html] view
plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
- http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <util:map id="esproperties">
- <entry key="cluster.name" value="${elasticsearch.cluster.name}" />
- </util:map>
- <elasticsearch:client id="client" properties="esproperties"
- esNodes="${elasticsearch.esNodes}" />
- <bean name="elasticsearchTemplate"
- class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
- <constructor-arg name="client" ref="client" />
- </bean>
- <bean name="elasticsearchService" class="com.sf.heros.mq.consumer.service.ElasticsearchService"
- init-method="init" />
- <bean name="es" class="com.sf.daidongxi.web.service.ElasticsearchService"></bean>
- </beans>
log4j.properties
[html] view
plaincopy
- ### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###
- log4j.rootLogger=info,logfile
- log4j.appender.console=org.apache.log4j.ConsoleAppender
- log4j.appender.console.Threshold=info
- log4j.appender.console.layout=org.apache.log4j.PatternLayout
- log4j.appender.console.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n
- log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.logfile.File=/app/logs/mq_consumer.log
- log4j.appender.logfile.datePattern='.'yyyy-MM-dd'.'
- log4j.appender.logfile.append=true
- log4j.appender.logfile.Threshold=debug
- log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
- log4j.appender.logfile.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n
maven管理
[html] view
plaincopy
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>elasticsearch Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <spring.version>3.1.1.RELEASE</spring.version>
- <findbugs.annotations>2.0.0</findbugs.annotations>
- <checkstyle.maven.plugin>2.11</checkstyle.maven.plugin>
- <pmd.maven.plugin>3.0</pmd.maven.plugin>
- <findbugs.maven.plugin>2.5.3</findbugs.maven.plugin>
- <java.version>1.7</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- spring begin -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring end -->
- <!-- elasticsearch package -->
- <dependency>
- <groupId>fr.pilato.spring</groupId>
- <artifactId>spring-elasticsearch</artifactId>
- <version>1.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>1.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-elasticsearch</artifactId>
- <version>1.0.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.5</version>
- </dependency>
- <!--json-lib -->
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>
- <!-- quartz job -->
- <dependency>
- <groupId>org.quartz-scheduler</groupId>
- <artifactId>quartz</artifactId>
- <version>2.2.1</version>
- </dependency>
- <!-- log4j -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.5</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>elasticsearch</finalName>
- </build>
- </project>
Java.class
Bean配置
[html] view
plaincopy
- 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
- }
- }
其余的类在源码中下载,此处不列出了...
常量管理
[html] view
plaincopy
- package com.sf.heros.mq.consumer.utils;
- import java.util.HashMap;
- import java.util.Map;
- public interface APP {
- public static final Map<String, String> map = new HashMap<String, String>();
- 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";
- }
- }
增删改类
[html] view
plaincopy
- /**
- *@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<TaskInfo> taskInfoList) {
- List<IndexQuery> queries = new ArrayList<IndexQuery>();
- 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<TaskInfo> taskInfoList) {
- List<IndexQuery> queries = new ArrayList<IndexQuery>();
- 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<NewsInfo> newsInfos) {
- List<IndexQuery> queries = new ArrayList<IndexQuery>();
- 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 <T> boolean deleteById(String id, Class<T> 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<ClusterHealthResponse> 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;
- }
- }
- }
查询类
[html] view
plaincopy
- 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<String> 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<String> results = new ArrayList<String>();
- 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<Map<String, Object>> 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<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
- for (SearchHit hit : hits) {
- results.add(hit.getSource());
- }
- return results;
- }
- /**
- * QueryBuilders 所有查询入口
- */
- public List<Map<String, Object>> 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<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
- 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<Map<String, Object>> queryForObjectNotEq(String type,
- String field, Collection<String> countents, String sortField,
- SortOrder order, int from, int size) {
- SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
- .setTypes(type).setSearchType(SearchType.DEFAULT)
- .setExplain(true);
- List<String> contents = new ArrayList<String>();
- 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<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
- 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<Map<String, Object>> 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<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
- for (SearchHit hit : hits) {
- results.add(hit.getSource());
- }
- return results;
- }
- public void afterPropertiesSet() throws Exception {
- System.out.println("init...");
- }
- }
测试
[html] view
plaincopy
- 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<TaskInfo> taskInfoList = new ArrayList<TaskInfo>();
- 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<Map<String, Object>> 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<Map<String, Object>> al = service.queryForObjectForElasticSerch("task_info", "taskContent", "高",19,20);
- for (int i = 0; i < al.size(); i++) {
- System.out.println(al.get(i));
- }
- }
- }
时间: 2024-10-14 20:01:14