spring data elasticsearch elasticsearch
2.0.0.RELEASE 2.2.0
1.4.0.M1 1.7.3
1.3.0.RELEASE 1.5.2
1.2.0.RELEASE 1.4.4
1.1.0.RELEASE 1.3.2
1.0.0.RELEASE

https://github.com/helloworldtang/spring-data-elasticsearch

1、None of the configured nodes are available 或者

  org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream

 原因:spring data elasticSearch 的版本与Spring boot、Elasticsearch版本不匹配。

 解决:

Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
x <= 1.3.5 y <= 1.3.4 z <= 1.7.2*
x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**

    这是版本之间的对应关系。Spring boot 1.3.5默认的elasticsearch版本是1.5.2,此时启动1.7.2版本以下的Elasticsearch客户端连接正常。

  注:注意java的es默认连接端口是9300,9200是http端口,这两个在使用中应注意区分。

2、Caused by: java.lang.IllegalArgumentException: @ConditionalOnMissingBean annotations must specify at least one bean (type, name or annotation)

  原因:spring boot是1.3.x版本,而es采用了2.x版本。在es的2.x版本去除了一些类,而这些类在spring boot的1.3.x版本中仍然被使用,导致此错误。

  解决:依照问题1中的版本对应关系,启动特定版本的es即可。

http://www.cnblogs.com/slimer/p/5643820.html
The Integration Zone is brought to you in partnership with 3scale. Take control of your APIs and get a free t-shirt when you complete the 3step Challenge. 

We often use Elasticsearch to improve performance in our application, especially searching and caching, to make our application scale and adapt in real-time.

Elasticsearch is a flexible and powerful open source, distributed, real-time search and analytics engine. In this article, I would like to introduce how to use Elasticsearch in java applications: by using Spring Boot data Elasticsearch. Spring Boot now easy and powerful, and we can build fast Java and web applications with a simple configuration.

By following the steps below, you can start writing your first application.

Source code: https://github.com/herotl2005/spring-data-elasticsearch-sample

Requirement enviroment

1. Install Elasticsearch

2. Install Gradle

3. IDE Eclipse or Intellij  IDEA

Step by Step Coding

1. Gradle build

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:1.2.0.RELEASE'
compile 'org.springframework.data:spring-data-cassandra:1.1.1.RELEASE'
compile 'org.springframework:spring-test:4.1.2.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-logging:1.2.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-actuator:1.2.0.RELEASE'
}

2. Elasticsearch configuration

@Configuration@PropertySource(value = "classpath:elasticsearch.properties")
@EnableElasticsearchRepositories(basePackages = "co.paan.repository")
public class ElasticsearchConfiguration {@Resource
private Environment environment;
@Bean
public Client client() {TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port")));
client.addTransportAddress(address);
return client;
}@Beanpublic ElasticsearchOperations elasticsearchTemplate() {return new ElasticsearchTemplate(client());}
}

You put elasticsearch host and post in your application properties file.

elasticsearch.host = localhost# if you use you local elasticsearch host
elasticsearch.port = 9300

3. Data mapping object:

In this application, we have 2 entities data object mapping: Post and Tag

@Document(indexName = "post", type = "post", shards = 1, replicas = 0)
public class Post {
@Idprivate String id;    private String title;//
@Field(type= FieldType.Nested)
private List<Tag> tags;   public String getId() {return id;
}public void setId(String id) {this.id = id;
}public String getTitle() {return title;
}public void setTitle(String title) {this.title = title;
}public List<Tag> getTags() {return tags;
}public void setTags(List<Tag> tags) {this.tags = tags;}
}
public class Tag {private String id;   private String name;   public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

4. Repository: we extends from ElasticsearchRepository

public interface PostRepository extends ElasticsearchRepository<Post, String>{Page<Post> findByTagsName(String name, Pageable pageable);
}

5. Data access service

public interface PostService {Post save(Post post);Post findOne(String id);Iterable<Post> findAll();Page<Post> findByTagsName(String tagName, PageRequest pageRequest);
}
@Servicepublic class PostServiceImpl implements PostService{@Autowired
private PostRepository postRepository;
@Override
public Post save(Post post) {postRepository.save(post);        return post;}@Overridepublic Post findOne(String id) {return postRepository.findOne(id);}@Overridepublic Iterable<Post> findAll() {return postRepository.findAll();}@Overridepublic Page<Post> findByTagsName(String tagName, PageRequest pageRequest) {return postRepository.findByTagsName(tagName, pageRequest);}
}

6. Testing and the result

@Testpublic void testFindByTagsName() throws Exception {
Tag tag = new Tag();
tag.setId("1");
tag.setName("tech");
Tag tag2 = new Tag();
tag2.setId("2");
tag2.setName("elasticsearch");
Post post = new Post();
post.setId("1");
post.setTitle("Bigining with spring boot application and elasticsearch");
post.setTags(Arrays.asList(tag, tag2));
postService.save(post);
Post post2 = new Post();
post2.setId("1");
post2.setTitle("Bigining with spring boot application");
post2.setTags(Arrays.asList(tag));
postService.save(post);
Page<Post> posts  = postService.findByTagsName("tech", new PageRequest(0,10));Page<Post> posts2  = postService.findByTagsName("tech", new PageRequest(0,10));Page<Post> posts3  = postService.findByTagsName("maz", new PageRequest(0,10));assertThat(posts.getTotalElements(), is(1L));
assertThat(posts2.getTotalElements(), is(1L));
assertThat(posts3.getTotalElements(), is(0L));
}

7. You can find detail project at github: https://github.com/herotl2005/spring-data-elasticsearch-sample

The Integration Zone is brought to you in partnership with 3scale. Learn how API providers have changed the way we think about integration in The Platform Vision of API Giants.

转载地址:https://dzone.com/articles/first-step-spring-boot-and

http://blog.csdn.net/hong0220/article/details/50583409

spring data elasticsearch 查询方式:

1、通过名字解析

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface BookRepository extends Repository<Book, String>
{
    List<Book> findByNameAndPrice(String name, Integer price);
}
根据上面的方法名会生成下面的Elasticsearch查询语句
"bool" :
    "must" :
        [
            "field" : {"name" "?"} },
            "field" : {"price" "?"} }
        ]
    }
}

2、@query注解

1
2
3
4
public interface BookRepository extends ElasticsearchRepository<Book, String> {
    @Query("{"bool" : {"must" : {"field" : {"name" : "?0"}}}}")
    Page<Book> findByName(String name,Pageable pageable);
}

3、构建Filter

1
2
3
4
5
6
7
8
9
10
11
使用过滤器可以提高查询速度+
private ElasticsearchTemplate elasticsearchTemplate;
SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withQuery(matchAllQuery())
    .withFilter(boolFilter().must(termFilter("id", documentId)))
    .build();
Page<SampleEntity> sampleEntities =
    elasticsearchTemplate.queryForPage(searchQuery,SampleEntity.class);

利用Scan和Scroll处理大结果集

Elasticsearch在处理大结果集时可以使用scan和scroll。在Spring Data Elasticsearch中,可以向下面那样使用ElasticsearchTemplate来使用scan和scroll处理大结果集。

Example 39. Using Scan and Scroll(使用scan和scroll)

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
SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withQuery(matchAllQuery())
    .withIndices("test-index")
    .withTypes("test-type")
    .withPageable(new PageRequest(0,1))
    .build();
String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
boolean hasRecords = true;
while (hasRecords){
    Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>()
    {
        @Override
        public Page<SampleEntity> mapResults(SearchResponse response) {
            List<SampleEntity> chunk = new ArrayList<SampleEntity>();
            for(SearchHit searchHit : response.getHits()){
                if(response.getHits().getHits().length <= 0) {
                    return null;
                }
                SampleEntity user = new SampleEntity();
                user.setId(searchHit.getId());
                user.setMessage((String)searchHit.getSource().get("message"));
                chunk.add(user);
            }
            return new PageImpl<SampleEntity>(chunk);
        }
    });
    if(page != null) {
        sampleEntities.addAll(page.getContent());
        hasRecords = page.hasNextPage();
    }
    else{
        hasRecords = false;
    }
    }
}

http://www.cnblogs.com/rainwang/p/5725214.html

Spring Boot + Elasticsearch相关推荐

  1. win7 x64 基于spring boot+elasticsearch+Redis+mysql+mybatis进行搜索引擎web开发--爬取IThome热评(一)

    因为工作需要,我准备在win7 x64系统上基于springboot +elasticsearch+redis搭建全文索引服务器. 1. elasticsearch安装比较方便,直接去官网下载了ela ...

  2. elasticsearch_spring-data-elasticsearch 快速入门-Spring Boot+Elasticsearch

    Spring Data家族对Elasticsearch也进行了封装支持,使用spring-data-elasticsearch,就像使用spring-data-jpa.spring-data-solr ...

  3. Spring Boot+ElasticSearch常用的聚合搜索

    ElasticSearch是一款开源的并且非常优秀的搜索引擎,常用于网站全文检索.日志分析等.本文将介绍一些基于日志的常用的聚合操作 Transport Client 与 Node Client 如果 ...

  4. Spring Boot Elasticsearch 入门

    1. 概述 如果胖友之前有用过 Elasticsearch 的话,可能有过被使用的 Elasticsearch 客户端版本搞死搞活.如果有,那么一起握个抓.所以,我们在文章的开始,先一起理一理这块. ...

  5. Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

    运行环境:JDK 7 或 8,Maven 3.0+ 技术栈:SpringBoot 1.5+,ElasticSearch 2.3.2 本文提纲 一.ES 的使用场景 二.运行 springboot-el ...

  6. 万字长文:详解 Spring Boot 中操作 ElasticSearch

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 超级小豆丁 来源 | http://www.m ...

  7. Spring Boot 集成 ElasticSearch,实现高性能搜索

    1.ElasticSearch介绍 Elasticsearch 是java开发的,基于 Lucene 的搜索引擎.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful Web接口.Elast ...

  8. Spring boot学习整理

    目录: Springboot结合hbase Springboot结合elasticsearch Springboot结合RestTemplate处理Http请求 Springboot的maven相关 ...

  9. Spring Data Elasticsearch案例详解

    一.Elasticsearch 工作原理 1.1 文档存储的路由 当索引到一个文档(如:报价系统),具体的文档数据(如:报价数据)会存储到一个分片.具体文档数据会被切分,并分别存储在分片 1 或者 分 ...

最新文章

  1. java jdbc 表存在_使用JDBC查询是否存在某表或视图,按月动态生成表
  2. 从各方面数据来看《猎场》为什么收官后热度依旧
  3. 高通 8x12 添加 TP和按键
  4. 迎.NET6,今日先送200P全新视频教程 / 源码
  5. mysql区分大小写搜索
  6. 引入react文件报错_react.js引入router文件后报错
  7. 熵的理解(玻尔兹曼分布)
  8. sql语句count_带COUNT()函数SQL SELECT语句
  9. 前用户sudo免密码
  10. 第2篇:Python 基础语法
  11. 安卓逆向 -- Xposed模块编写
  12. css气泡图片上下浮动
  13. 怎么恢复大疆无人机TF卡误删除格式化的MP4和MOV视频
  14. 狂神说Linux笔记
  15. php实现展现量cookie,[转载]展现量、点击量、点击率;访客数、访问次数、浏览量的区别与作用...
  16. OpenCV-Python (官方)中文教程(部分三)
  17. 笔记本计算机排名2015,2015性价比高的笔记本电脑排行榜
  18. STM32 HAL库学习笔记1-HAL库简介
  19. 国外变电站3d可视化技术发展_变电站3D可视化运维管理!
  20. 预习博客(HTML标签+认识浏览器)

热门文章

  1. echop红包发放规则添加
  2. 用户接口设计的20个原则
  3. mapx实现热点效果
  4. NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
  5. 关于zipfile解压出现的字符编码问题
  6. 2018年4月26日笔记
  7. 打包phar文件过大的问题。
  8. 第二次scrum冲刺
  9. arguments转换为数组格式
  10. 解决Couldn't resolve host 'mirrorlist.centos.org