3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)
阅读原文时间:2023年07月08日阅读:1

1.引入maven坐标


org.springframework.data spring-data-elasticsearch 3.0.5.RELEASE org.elasticsearch.plugin transport-netty4-client


org.elasticsearch elasticsearch 5.6.8


org.elasticsearch.client transport 5.6.8


org.apache.logging.log4j log4j-to-slf4j 2.9.1
org.slf4j slf4j-api 1.7.24
org.slf4j slf4j-simple 1.7.21
log4j log4j 1.2.12


junit junit 4.12
org.springframework spring-test 5.0.4.RELEASE


com.fasterxml.jackson.core jackson-core 2.8.1
com.fasterxml.jackson.core jackson-databind 2.8.1
com.fasterxml.jackson.core jackson-annotations 2.8.1

2.配置ElasticSearch


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd ">

_ _

_ _

_ _

** **

3.配置dao

@Repository
public interface ArticleRepository extends ElasticsearchRepository {

}

4.配置service

@Service public class ArticleServiceImpl implements ArticleService {  @Autowired___ _ private ArticleRepository articleRepository;_
@Autowired
** private ElasticsearchTemplate elasticsearchTemplate;**
//新建索引
public void creatIndex() {
//创建索引,并配置映射关系
elasticsearchTemplate.createIndex(Article.class);
//创建映射关系
// elasticsearchTemplate.putMapping(Article.class);
}
//保存文档
public void save(Article article) {
_ articleRepository.save(article);_
}
//更新文档,ealasticsearch根据id判断该数据是否存在若存在则进行修改
@Test
    public void update(){
        Article article = new Article();
        article.setId(1001);
        article.setTitle("elasticSearch 3.0版本发布…更新");
        article.setContent("ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");
        articleService.save(article);
    }
//删除文档
  public void delete(){
        Article article = new Article();
        article.setId(1001);
        articleService.delete(article);
    }
//分页查询
    @Test
    public void findAllPage(){
        Pageable pageable = PageRequest.of(1,10);//page,pagesize,初始页是从0开始
        Page

page = articleService.findAll(pageable);
        for(Article article:page.getContent()){
            System.out.println(article);
        }
    }

}_

3.3.2 常用查询命名规则

关键字

命名规则

解释

示例

and

findByField1AndField2

根据Field1和Field2获得数据

findByTitleAndContent

or

findByField1OrField2

根据Field1或Field2获得数据

findByTitleOrContent

is

findByField

根据Field获得数据 

findByTitle  

not

findByFieldNot

根据Field获得补集数据

findByTitleNot

between

findByFieldBetween

获得指定范围的数据

findByPriceBetween

lessThanEqual

findByFieldLessThan

获得小于等于指定值的数据

findByPriceLessThan

3.3.3 查询方法测试

1)dao层实现

public interface ArticleRepository extends ElasticsearchRepository {
//根据标题查询
_ List

findByTitle(String condition);_
//根据标题查询(含分页)
Page
findByTitle(String condition, Pageable pageable);
}

2)service实现

public interface ArticleService {
//根据标题查询
List

findByTitle(String condition);
//根据标题查询(含分页)
Page
findByTitle(String condition, Pageable pageable);
}

3)service层实现

@Service
public class ArticleServiceImpl implements ArticleService {

@Autowired  
private ArticleRepository articleRepository;

** public List

findByTitle(String condition) { return articleRepository.findByTitle(condition); }**
_ public Page
findByTitle(String condition, Pageable pageable) { return articleRepository.findByTitle(condition,pageable); }_

}

4)单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringDataESTest {

@Autowired  
_**private ArticleService articleService;**_

@Autowired  
_**private TransportClient client;**_

@Autowired  
**_private ElasticsearchTemplate elasticsearchTemplate;_**   
/\*\*条件查询\*/  
@Test  
public void findByTitle(){  
    String condition = "版本";  
   _ **List<Article> articleList = articleService.findByTitle(condition);**_  
    for(Article article:articleList){  
        System.out.println(article);  
    }  
}

/\*\*条件分页查询\*/  
@Test  
public void findByTitlePage(){  
    String condition = "版本";  
    Pageable pageable = PageRequest.of(2,10);  
  _ **Page<Article> page = articleService.findByTitle(condition,pageable);**_  
    for(Article article:page.getContent()){  
        System.out.println(article);  
    }  
}  

    /**条件分页查询*/
    @Test
    public void findByTitlePage(){
        String condition = "版本";
        Pageable pageable = PageRequest.of(2,10);
        Page

page = articleService.findByTitle(condition,pageable);
        for(Article article:page.getContent()){
            System.out.println(article);
        }
    }

}

5)使用Elasticsearch的原生查询对象进行查询。

@Test
public void findByNativeQuery() {
//创建一个SearchQuery对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
//设置查询条件,此处可以使用QueryBuilders创建多种查询
.withQuery(QueryBuilders.queryStringQuery("SpringData").defaultField("title"))
//还可以设置分页信息
.withPageable(PageRequest.of(0, 5))
//创建SearchQuery对象
** .build();**
//使用模板对象执行查询
List

articleList= elasticsearchTemplate.queryForList(searchQuery, Article.class);
_ for(Article list: articleList){ System.out.println(list); }_
}

6)总结 

自定义方法可以对搜索的内容先分词再进行查询,每个词之间条件都是and的关系,一个词分词后的片段必须都同时满足

原生方法是or的关系