一:配置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);