第一章 ElasticSearch编程操作 1.1 创建工程,导入坐标,pom.xml坐标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <dependencies > <dependency > <groupId > org.elasticsearch</groupId > <artifactId > elasticsearch</artifactId > <version > 5.6.8</version > </dependency > <dependency > <groupId > org.elasticsearch.client</groupId > <artifactId > transport</artifactId > <version > 5.6.8</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-to-slf4j</artifactId > <version > 2.9.1</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.24</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-simple</artifactId > <version > 1.7.21</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > 1.2.12</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > </dependency > </dependencies > 
1.2 创建索引index 1 2 3 4 5 6 7 8 9 10 11 12 @Test public  void  test1 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"blog2" ).get();
1.3 创建映射mapping 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 @Test public  void  test3 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"article" )"properties" )"id" )"type" , "integer" ).field("store" , "yes" )"title" )"type" , "string" ).field("store" , "yes" ).field("analyzer" , "ik_smart" )"content" )"type" , "string" ).field("store" , "yes" ).field("analyzer" , "ik_smart" )"blog2" )"article" ).source(builder);
1.4 建立文档document 1.4.1 建立文档(通过XContentBuilder) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public  void  test4 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"id" , 1 )"title" , "ElasticSearch是一个基于Lucene的搜索服务器" )"content" ,"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。" )"blog2" , "article" , "1" ).setSource(builder).get();
1.4.2 建立文档(使用Jackson转换实体) 1)创建Article实体
1 2 3 4 5 6 public  class  Article  private  Integer id;private  String title;private  String content;
2)添加jackson坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-core</artifactId > <version > 2.8.1</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.8.1</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-annotations</artifactId > <version > 2.8.1</version > </dependency > 
3)代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Test public  void  test5 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));new  Article();2 );"搜索工作其实很快乐" );"我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。" );new  ObjectMapper();"blog2" , "article" , article.getId().toString())
1.5 查询文档操作 1.5.1关键词查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @Test public  void  testTermQuery ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"blog2" )"article" )"content" , "搜索" )).get();"查询结果有:"  + hits.getTotalHits() + "条" );while  (iterator.hasNext()) {"title:"  + searchHit.getSource().get("title" ));
2.5.2  字符串查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @Test public  void  testStringQuery ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"blog2" )"article" )"搜索" )).get();"查询结果有:"  + hits.getTotalHits() + "条" );while  (iterator.hasNext()) {"title:"  + searchHit.getSource().get("title" ));
2.5.2 使用文档ID查询文档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @Test public  void  testIdQuery ()  throws  Exception "blog1" )"article" )"test002" ))while (hitIterator.hasNext()) {
2.6 查询文档分页操作 2.6.1 批量插入数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  @Test public  void  test9 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));new  ObjectMapper();for  (int  i = 1 ; i <= 100 ; i++) {new  Article();"搜索工作其实很快乐" );"我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。" );"blog2" , "article" , article.getId().toString())
2.6.2 分页查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @Test public  void  test10 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"blog2" ).setTypes("article" )0 ).setSize(5 );"查询结果有:"  + hits.getTotalHits() + "条" );while  (iterator.hasNext()) {"id:"  + searchHit.getSource().get("id" ));"title:"  + searchHit.getSource().get("title" ));"content:"  + searchHit.getSource().get("content" ));"-----------------------------------------" );
2.7 查询结果高亮操作 2.7.1 什么是高亮显示 在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
京东商城搜索”笔记本”
2.7.2 高亮显示的html分析 通过开发者工具查看高亮数据的html代码实现:
ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹
2.7.3 高亮显示代码实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 @Test public  void  test11 ()  throws  Exception"cluster.name" , "my-elasticsearch" ).build();new  PreBuiltTransportClient(settings)new  InetSocketTransportAddress(InetAddress.getByName("127.0.0.1" ), 9300 ));"blog2" ).setTypes("article" )"title" , "搜索" ));new  HighlightBuilder();"<font style='color:red'>" );"</font>" );"title" );"共搜到:" +searchHits.getTotalHits()+"条结果!" );for (SearchHit hit:searchHits){"String方式打印文档搜索内容:" );"Map方式打印高亮内容" );"遍历高亮集合,打印高亮片段:" );"title" ).getFragments();for  (Text str : text) {
第三章 Spring Data ElasticSearch 使用 3.1 Spring Data ElasticSearch简介 3.1.1 什么是Spring Data Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。
Spring Data的官网:http://projects.spring.io/spring-data/ 
3.1.2 什么是Spring Data ElasticSearch Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。
官方网站:http://projects.spring.io/spring-data-elasticsearch/  
3.2 Spring Data ElasticSearch入门 1)导入Spring Data ElasticSearch坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 <?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.itheima</groupId > <artifactId > itheima_elasticsearch_demo3</artifactId > <version > 1.0-SNAPSHOT</version > <dependencies > <dependency > <groupId > org.elasticsearch</groupId > <artifactId > elasticsearch</artifactId > <version > 5.6.8</version > </dependency > <dependency > <groupId > org.elasticsearch.client</groupId > <artifactId > transport</artifactId > <version > 5.6.8</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-to-slf4j</artifactId > <version > 2.9.1</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.24</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-simple</artifactId > <version > 1.7.21</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > 1.2.12</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-core</artifactId > <version > 2.8.1</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.8.1</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-annotations</artifactId > <version > 2.8.1</version > </dependency > <dependency > <groupId > org.springframework.data</groupId > <artifactId > spring-data-elasticsearch</artifactId > <version > 3.0.5.RELEASE</version > <exclusions > <exclusion > <groupId > org.elasticsearch.plugin</groupId > <artifactId > transport-netty4-client</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > 5.0.4.RELEASE</version > </dependency > </dependencies > </project > 
2)创建applicationContext.xml配置文件,引入elasticsearch命名空间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?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:elasticsearch ="http://www.springframework.org/schema/data/elasticsearch"      xsi:schemaLocation ="          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         " ></beans > 
3)编写实体Article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package  com.itheima.domain;public  class  Article  private  Integer id;private  String title;private  String content;public  Integer getId ()  return  id;public  void  setId (Integer id)  this .id = id;public  String getTitle ()  return  title;public  void  setTitle (String title)  this .title = title;public  String getContent ()  return  content;public  void  setContent (String content)  this .content = content;@Override public  String toString ()  return  "Article [id="  + id + ", title="  + title + ", content="  + content + "]" ;
4)编写Dao
1 2 3 4 5 6 7 8 9 package  com.itheima.dao;import  com.itheima.domain.Article;import  org.springframework.data.elasticsearch.repository.ElasticsearchRepository;@Repository public  interface  ArticleRepository  extends  ElasticsearchRepository <Article , Integer > 
5)编写Service
1 2 3 4 5 6 7 8 9 package  com.itheima.service;import  com.itheima.domain.Article;public  interface  ArticleService  public  void  save (Article article) 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package  com.itheima.service.impl;import  com.itheima.dao.ArticleRepository;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.stereotype.Service;@Service public  class  ArticleServiceImpl  implements  ArticleService  @Autowired private  ArticleRepository articleRepository;public  void  save (Article article)  
6) 配置applicationContext.xml 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?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:elasticsearch ="http://www.springframework.org/schema/data/elasticsearch"         xsi:schemaLocation ="          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         " ><elasticsearch:repositories  base-package ="com.itheima.dao" /> <context:component-scan  base-package ="com.itheima.service" /> <elasticsearch:transport-client  id ="client"  cluster-nodes ="localhost:9300"  cluster-name ="my-elasticsearch" /> <bean  id ="elasticsearchTemplate"  class ="org.springframework.data.elasticsearch.core.ElasticsearchTemplate" > <constructor-arg  name ="client"  ref ="client" > </constructor-arg > </bean > </beans > 
7)配置实体
基于spring data elasticsearch注解配置索引、映射和实体的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package  com.itheima.domain;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.FieldType;@Document (indexName="blog3" ,type="article" )public  class  Article  @Id @Field (store=true , index = false ,type = FieldType.Integer)private  Integer id;@Field (index=true ,analyzer="ik_smart" ,store=true ,searchAnalyzer="ik_smart" ,type = FieldType.text)private  String title;@Field (index=true ,analyzer="ik_smart" ,store=true ,searchAnalyzer="ik_smart" ,type = FieldType.text)private  String content;public  Integer getId ()  return  id;public  void  setId (Integer id)  this .id = id;public  String getTitle ()  return  title;public  void  setTitle (String title)  this .title = title;public  String getContent ()  return  content;public  void  setContent (String content)  this .content = content;@Override public  String toString ()  return  "Article [id="  + id + ", title="  + title + ", content="  + content + "]" ;
1 2 3 4 5 6 7 8 9 10 11 其中,注解解释如下:@Document (indexName="blob3" ,type="article" ):@Id :主键的唯一标识@Field (index=true,analyzer="ik_smart" ,store=true,searchAnalyzer="ik_smart" ,type = FieldType.text)type : 数据类型
8)创建测试类SpringDataESTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package  com.itheima.test;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.elasticsearch.client.transport.TransportClient;import  org.junit.Test;import  org.junit.runner.RunWith;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import  org.springframework.test.context.ContextConfiguration;import  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@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  createIndex () .class ) ;.class ) ;@Test public  void  saveArticle () new  Article();100 );"测试SpringData ElasticSearch" );"Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装 \n"  +"    Spring Data为Elasticsearch Elasticsearch项目提供集成搜索引擎" );
3.3 Spring Data ElasticSearch的常用操作 3.3.1 增删改查方法测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package  com.itheima.service;import  com.itheima.domain.Article;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.Pageable;public  interface  ArticleService  public  void  save (Article article) public  void  delete (Article article) public  Iterable<Article> findAll () public  Page<Article> findAll (Pageable pageable) 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package  com.itheima.service.impl;import  com.itheima.dao.ArticleRepository;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.Pageable;import  org.springframework.stereotype.Service;@Service public  class  ArticleServiceImpl  implements  ArticleService  @Autowired private  ArticleRepository articleRepository;public  void  save (Article article)  public  void  delete (Article article)  public  Iterable<Article> findAll ()  return  iter;public  Page<Article> findAll (Pageable pageable)  return  articleRepository.findAll(pageable);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 package  com.itheima.test;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.elasticsearch.client.transport.TransportClient;import  org.junit.Test;import  org.junit.runner.RunWith;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.PageRequest;import  org.springframework.data.domain.Pageable;import  org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import  org.springframework.test.context.ContextConfiguration;import  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@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  createIndex () .class ) ;.class ) ;@Test public  void  saveArticle () new  Article();100 );"测试SpringData ElasticSearch" );"Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装 \n"  +"    Spring Data为Elasticsearch Elasticsearch项目提供集成搜索引擎" );@Test public  void  save () new  Article();1001 );"elasticSearch 3.0版本发布" );"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口" );@Test public  void  update () new  Article();1001 );"elasticSearch 3.0版本发布...更新" );"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口" );@Test public  void  delete () new  Article();1001 );@Test public  void  save100 () for (int  i=1 ;i<=100 ;i++){new  Article();"elasticSearch 3.0版本发布..,更新" );"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口" );@Test public  void  findAllPage () 1 ,10 );for (Article article:page.getContent()){
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层实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package  com.itheima.dao;import  com.itheima.domain.Article;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.Pageable;import  org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import  java.util.List;public  interface  ArticleRepository  extends  ElasticsearchRepository <Article , Integer > List<Article> findByTitle (String condition)  ;Page<Article> findByTitle (String condition, Pageable pageable)  ;
2)service层实现
1 2 3 4 5 6 public  interface  ArticleService  List<Article> findByTitle (String condition)  ;Page<Article> findByTitle (String condition, Pageable pageable)  ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package  com.itheima.service.impl;import  com.itheima.dao.ArticleRepository;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.Pageable;import  org.springframework.stereotype.Service;import  java.util.List;@Service public  class  ArticleServiceImpl  implements  ArticleService  @Autowired private  ArticleRepository articleRepository;public  List<Article> findByTitle (String condition)  return  articleRepository.findByTitle(condition);public  Page<Article> findByTitle (String condition, Pageable pageable)  return  articleRepository.findByTitle(condition,pageable);
3)测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package  com.itheima.test;import  com.itheima.domain.Article;import  com.itheima.service.ArticleService;import  org.elasticsearch.client.transport.TransportClient;import  org.junit.Test;import  org.junit.runner.RunWith;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.data.domain.Page;import  org.springframework.data.domain.PageRequest;import  org.springframework.data.domain.Pageable;import  org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import  org.springframework.test.context.ContextConfiguration;import  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import  java.util.List;@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 () "版本" ;for (Article article:articleList){@Test public  void  findByTitlePage () "版本" ;2 ,10 );for (Article article:page.getContent()){
3.3.4使用Elasticsearch的原生查询对象进行查询。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Test public  void  findByNativeQuery ()  new  NativeSearchQueryBuilder()"备份节点上没有数据" ).defaultField("title" ))1 , 5 )).class )                 .forEach (a -> System .out .println (a )) ;