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. Spring Boot + Elasticsearch 实现索引批量写入

    在使用Eleasticsearch进行索引维护的过程中,如果你的应用场景需要频繁的大批量的索引写入,再使用上篇中提到的维护方法的话显然效率是低下的,此时推荐使用bulkIndex来提升效率.批写入数据 ...

  2. Spring Boot + Elasticsearch实现大批量数据集下中文的精确匹配-案例剖析

    缘由 数据存储在MYSQ库中,数据基本维持不变,但数据量又较大(几千万)放在MYSQL中查询效率上较慢,寻求一种简单有效的方式提高查询效率,MYSQL并不擅长大规模数据量下的数据查询. 技术方案 考虑 ...

  3. 搭建spring boot+elasticsearch+activemq服务

    目前时间是:2017-01-24 本文不涉及activemq的安装 需求 activemq实时传递数据至服务 elasticsearch做索引 对外开放查询接口 完成全文检索 环境 jdk:1.8 s ...

  4. Spring Boot + Elasticsearch 实现索引的日常维护

    全文检索的应用越来越广泛,几乎成了互联网应用的标配,商品搜索.日志分析.历史数据归档等等,各种场景都会涉及到大批量的数据,在全文检索方面,方案无外乎Lucene.Solr.Elasticsearch三 ...

  5. Spring Boot + Elasticsearch 使用示例

    本文分别使用 Elasticsearch Repository 和 ElasticsearchTemplate 实现 Elasticsearch 的简单的增删改查 一.Elastic Stack El ...

  6. 。。。。。。不带http https : 不报错 spring boot elasticsearch rest

    ......不带http https  : 不报错 先telnet http://onf:8080/getES653/道路桥梁正在“理疗”%20这14条道路纳入市政中修 @GetMapping(&qu ...

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

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...

  8. Spring Boot 2.x 学习专栏

    Spring Boot 2.0 入门指南 Spring Boot 2.0 返回JSP页面实战 Spring Boot 2.0 热部署指南 Spring Boot 2.0 整合FreeMarker模板引 ...

  9. Spring boot学习整理

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

随机推荐

  1. Android为ListView的Item设置不同的布局

    MainActivity如下: package cc.testlistview; import java.util.ArrayList; import java.util.HashMap; impor ...

  2. visul svn+花生壳

    1.服务器端 工具:visul svn+花生壳 花色壳:注册域名 visul svn:配置http://www.cnblogs.com/bluewelkin/p/3479105.html 外网访问,端 ...

  3. 第四篇:SQL

    前言 确实,关于SQL的学习资料,各类文档在网上到处都是.但它们绝大多数的出发点都局限在旧有关系数据库里,内容近乎千篇一律.而在当今大数据的浪潮下,SQL早就被赋予了新的责任和意义. 本篇中,笔者将结 ...

  4. 优雅退出 Android 应用程序的 6 种方式

    我们先来看看几种常见的退出方法(不优雅的方式) 一.容器式 建立一个全局容器,把所有的Activity存储起来,退出时循环遍历finish所有Activity import java.util.Arr ...

  5. My.Ioc 代码示例——属性和方法注入

    在 My.Ioc 中,我们可以指定让容器在构建好对象实例之后,自动为我们调用对象的公共方法或是为对象的公共属性赋值.在解析对象实例时,容器将根据我们在注册对象时指定的方法调用或属性赋值的先后顺序,调用 ...

  6. 关于uploadify 没有显示进度条!!!!

    如果你也弄了很久不知道为什么不出现上传进度条!,那就一定要看这里了! 我注释了 queueID 属性后 就出现了!!!!! 就是这么简答! //添加界面的附件管理 $('#file_upload'). ...

  7. asp.net中调用COM组件发布IIS时常见错误 80070005解决方案

    很多人在.net开发Web站点中引用了COM组件,调试时一切正常,但一发布到IIS下就提示如下错误: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005 ...

  8. jquery get checkbox inside element(td).

    <td id="skill"><input name="skill" type="checkbox" value=&quo ...

  9. OC - 10.使用Quartz2D绘制个性头像

      效果图 将一张图片剪切成圆形 在图片周围显示指定宽度和颜色的边框 实现思路 效果图中主要由不同尺寸的两大部分组成 蓝色的背景区域,尺寸等于图片的尺寸加上边框的尺寸 图片区域,尺寸等于图片的尺寸 绘 ...

  10. iOS真机测试种可能遇到的问题

    1. Reason- image not found 用模拟器是没有问题的,不过在真机好像是有问题,不确定是否是所有机型. 崩溃日志   1 2 3 4 5   dyld: Library not l ...