springdata -----操作ES
一:配置springdata-Es
elasticseach-JPA.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
">
<context:component-scan base-package="com.sf.heros.mq.consumer"/> 扫描包
<elasticsearch:transport-client id="client" cluster-name="elasticsearch"
cluster-nodes="127.0.0.1:9300" client-transport-sniff="false"/> 配置链接端口(默认的是9200,但是我们需要与之交互,要用到配置文件里所写那个端口,我的是9300)
<elasticsearch:repositories base-package="com.sf.heros.mq.consumer.dao" springdata操作ES的 接口类
elasticsearch-template-ref="elasticsearchTemplate"/>
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" /> ES操作类
</bean> </beans> 2:接口类
UserDao。java(增删改查方法 与其它springdata一致,findBy等等语法参考springdata)
public interface UserDao extends ElasticsearchRepository<User,Integer> {
List<User> findByArticleInfId(Integer aid);
} 3:实体类
@Document(indexName = "article_inf_index", type = "articleInf")
@Setting(settingPath = "elasticsearch-analyser.json")
public class User implements Serializable {
//elasticsearch
@org.springframework.data.annotation.Id
private Integer articleInfId; @Field(type = FieldType.String)//使用ngram进行单字分词
private String articleTitle; @Field(type = FieldType.Date, store = true, format = DateFormat.custom, pattern ="yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd HH:mm:ss")
private Date releaseTime; public Integer getArticleInfId() {
return articleInfId;
} public void setArticleInfId(Integer articleInfId) {
this.articleInfId = articleInfId;
} public String getArticleTitle() {
return articleTitle;
} public void setArticleTitle(String articleTitle) {
this.articleTitle = articleTitle;
} public Date getReleaseTime() {
return releaseTime;
} public void setReleaseTime(Date releaseTime) {
this.releaseTime = releaseTime;
} @Override
public String toString() {
return "User{" +
"articleInfId=" + articleInfId +
", articleTitle='" + articleTitle + '\'' +
", releaseTime=" + releaseTime +
'}';
}
} 4:serverce
@Service("testService")
public class ElasticsearchService {
@Autowired
private UserDao userDao;
public void add(User user){
userDao.save(user);
} public List<User> search(Integer aid){
Pageable pageable=new Pageable() {
@Override
public int getNumberOfPages() {
return 0;
} @Override
public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
return null;
} @Override
public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
return null;
}
};
return this.userDao.findByArticleInfId(aid);
}
public User searchByQuery(User user){
BoolQueryBuilder qb= QueryBuilders. boolQuery();
qb.must(QueryBuilders.matchQuery("articleInfId",12));
Iterable<User> search = userDao.search(qb);
return search.iterator().next();
}
}
BoolQueryBuilder方法:
1.matchAllQuery() matchAllQuery()方法用来匹配全部文档
2.matchQuery("filedname","value")匹配单个字段,匹配字段名为filedname,值为value的文档
3.multiMatchQuery(Object text, String... fieldNames)多个字段匹配某一个值
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music", "name", "interest");//搜索name中或interest中包含有music的文档(必须与music一致)
4.wildcardQuery()模糊查询 ?匹配单个字符,*匹配多个字符
5.使用BoolQueryBuilder进行复合查询
使用must:
- //模糊查询
- WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(
- "name", "*jack*");//搜索名字中含有jack的文档
- WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(
- "interest", "*read*");//搜索interest中含有read的文档
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- //name中必须含有jack,interest中必须含有read,相当于and
- boolQueryBuilder.must(queryBuilder1);
- boolQueryBuilder.must(queryBuilder2);
使用 should:
- WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(
- "name", "*jack*");//搜索名字中含有jack的文档
- WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(
- "interest", "*read*");//搜索interest中含有read的文档
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- //name中含有jack或者interest含有read,相当于or
- boolQueryBuilder.should(queryBuilder1);
- boolQueryBuilder.should(queryBuilder2);
springdata -----操作ES的更多相关文章
- JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 1
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7703679.html ------------------------------------ ...
- JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 2
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7704914.html ------------------------------------ ...
- JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 3
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7718741.html ------------------------------------ ...
- JPA + SpringData 操作数据库 ---- 深入了解 SpringData
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7735616.html ------------------------------------ ...
- Elasticsearch笔记五之java操作es
Java操作es集群步骤1:配置集群对象信息:2:创建客户端:3:查看集群信息 1:集群名称 默认集群名为elasticsearch,如果集群名称和指定的不一致则在使用节点资源时会报错. 2:嗅探功能 ...
- SpringBoot整合SpringDataElasticSearch操作ES
(1).添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- SpringBoot整合Jest操作ES
(1).添加依赖 <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</a ...
- SpringBoot操作ES进行各种高级查询
SpringBoot整合ES 创建SpringBoot项目,导入 ES 6.2.1 的 RestClient 依赖和 ES 依赖.在项目中直接引用 es-starter 的话会报容器初始化异常错误,导 ...
- 07_Kibana界面操作ES
Kibana界面的API操作ES 1.创建索引 1.1 指定分片数量和备份数量 1.2 创建默认 2. 查看索引 2.1 查看单个索引设置 2.2 查看所有索引设置 3.文档管理 3.1 添加文档 3 ...
随机推荐
- [Java.web]Web应用结构
以Web应用放在 Tomcat\webapps\ 目录下为例 day01 目录 | |------------- html.jsp.css.js 文件等 |------------- ...
- Log4j.xml配置(rolling示例)
Log4j.xml配置(很详细) <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4 ...
- enq:TM-contention
enq:TM-contention 2011-08-04 15:55:17 分类: Linux 7.1 enq:TM-contention 执行dml期间,为防止对与dml相关的对象进 ...
- Dev GridControl 选择行及绑定/获取List对象
GridControl绑定List对象一般是为了获取焦点行时直接获得该行代表的List对象,或者为了实现嵌套表格,对第一中情况,起始不用绑定List,绑定DataTable也可以实现获取对应List对 ...
- K2 Blackpearl中从数据库直接删除流程实例之K2ServerLog表
转:http://www.cnblogs.com/dannyli/archive/2012/11/29/2794845.html /********************************** ...
- libaco: 一个极速的轻量级 C 非对称协程库 🚀 (10 ns/ctxsw + 一千万协程并发仅耗内存 2.8GB + Github Trending)
0 Name 简介 libaco - 一个极速的.轻量级.C语言非对称协程库. 这个项目的代号是Arkenstone
- Spring高级话题-@Enable***注解的工作原理
出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 & ...
- Abstract(抽象)
谈到抽象,就先谈谈面向对象语言的三大特性,也是人们口中常说的封装.继承.多态. 封装:什么是封装,按到我的理解,封装就是把某些类的相关属性和方法封装,对内实现数据影城,对外提供稳定接口. 继承:从字面 ...
- <a>标签中的href="javascript:;"就是去掉a标签的默认行为
<a>标签中的href="javascript:;"是什么意思? 例子:<a href="javascript:;">我的大学</ ...
- UNITY UI字体模糊的原因
根本原因:像素少. 解决办法:字体的 font size将像素设置大些,然后用scale来缩放大小