码迷,mamicode.com
首页 > 编程语言 > 详细

springdata -----操作ES

时间:2018-03-01 19:56:48      阅读:793      评论:0      收藏:0      [点我收藏+]

标签:扫描   -name   val   should   搜索   index   tostring   sea   new   

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

  1. //模糊查询  
  2. WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(  
  3.             "name", "*jack*");//搜索名字中含有jack的文档  
  4. WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(  
  5.             "interest", "*read*");//搜索interest中含有read的文档  
  6.   
  7. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();  
  8. //name中必须含有jack,interest中必须含有read,相当于and  
  9. boolQueryBuilder.must(queryBuilder1);  
  10. boolQueryBuilder.must(queryBuilder2);  

     使用 should:

        

  1. WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(  
  2.             "name", "*jack*");//搜索名字中含有jack的文档  
  3. WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(  
  4.             "interest", "*read*");//搜索interest中含有read的文档  
  5.   
  6. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();  
  7. //name中含有jack或者interest含有read,相当于or  
  8. boolQueryBuilder.should(queryBuilder1);  
  9. boolQueryBuilder.should(queryBuilder2); 

 



 

springdata -----操作ES

标签:扫描   -name   val   should   搜索   index   tostring   sea   new   

原文地址:https://www.cnblogs.com/anxbb/p/8489612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!