`

[转]Spring Data Elasticsearch

阅读更多

Quick Start

Maven configuration

Add the Maven dependency:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

If you'd rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-elasticsearch</artifactId>
  <version>2.1.0.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>http://repo.spring.io/libs-snapshot</url>
</repository>
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 1.1.1

ElasticsearchRepository

A default implementation of ElasticsearchRepository, aligning to the generic Repository Interfaces, is provided. Spring can do the Repository implementation for you depending on method names in the interface definition.

The ElasticsearchCrudRepository extends PagingAndSortingRepository

    public interface ElasticsearchCrudRepository<T, ID extends Serializable> extends ElasticsearchRepository<T, ID>, PagingAndSortingRepository<T, ID> {
    }

Extending ElasticsearchRepository for custom methods

    public interface BookRepository extends Repository<Book, String> {

        List<Book> findByNameAndPrice(String name, Integer price);

        List<Book> findByNameOrPrice(String name, Integer price);

        Page<Book> findByName(String name,Pageable page);

        Page<Book> findByNameNot(String name,Pageable page);

        Page<Book> findByPriceBetween(int price,Pageable page);

        Page<Book> findByNameLike(String name,Pageable page);

        @Query("{\"bool\" : {\"must\" : {\"term\" : {\"message\" : \"?0\"}}}}")
        Page<Book> findByMessage(String message, Pageable pageable);
    }

Indexing a single document with Repository

        @Autowired
        private SampleElasticsearchRepository repository;

        String documentId = "123456";
        SampleEntity sampleEntity = new SampleEntity();
        sampleEntity.setId(documentId);
        sampleEntity.setMessage("some message");

        repository.save(sampleEntity);

Indexing multiple Document(bulk index) using Repository

        @Autowired
        private SampleElasticsearchRepository repository;

        String documentId = "123456";
        SampleEntity sampleEntity1 = new SampleEntity();
        sampleEntity1.setId(documentId);
        sampleEntity1.setMessage("some message");

        String documentId2 = "123457"
        SampleEntity sampleEntity2 = new SampleEntity();
        sampleEntity2.setId(documentId2);
        sampleEntity2.setMessage("test message");

        List<SampleEntity> sampleEntities = Arrays.asList(sampleEntity1, sampleEntity2);

        //bulk index
        repository.save(sampleEntities);

ElasticsearchTemplate

ElasticsearchTemplate is the central support class for elasticsearch operations.

Indexing a single document using Elasticsearch Template

        String documentId = "123456";
        SampleEntity sampleEntity = new SampleEntity();
        sampleEntity.setId(documentId);
        sampleEntity.setMessage("some message");
        IndexQuery indexQuery = new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
        elasticsearchTemplate.index(indexQuery);

Indexing multiple Document(bulk index) using Elasticsearch Template

        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;

        List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
        //first document
        String documentId = "123456";
        SampleEntity sampleEntity1 = new SampleEntity();
        sampleEntity1.setId(documentId);
        sampleEntity1.setMessage("some message");

        IndexQuery indexQuery1 = new IndexQueryBuilder().withId(sampleEntity1.getId()).withObject(sampleEntity1).build();
        indexQueries.add(indexQuery1);

        //second document
        String documentId2 = "123457";
        SampleEntity sampleEntity2 = new SampleEntity();
        sampleEntity2.setId(documentId2);
        sampleEntity2.setMessage("some message");

        IndexQuery indexQuery2 = new IndexQueryBuilder().withId(sampleEntity2.getId()).withObject(sampleEntity2).build()
        indexQueries.add(indexQuery2);

        //bulk index
        elasticsearchTemplate.bulkIndex(indexQueries);

Searching entities using Elasticsearch Template

        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(queryString(documentId).field("id"))
        .build();
        Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery,SampleEntity.class);

XML Namespace

You can set up repository scanning via xml configuration, which will happily create your repositories.

Using Node Client

<?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:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
       xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <elasticsearch:node-client id="client" local="true"/>

    <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client"/>
    </bean>

</beans>

Using Transport Client

<?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:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
       xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <elasticsearch:repositories base-package="com.xyz.acme"/>

    <elasticsearch:transport-client id="client" cluster-nodes="ip:9300,ip:9300" cluster-name="elasticsearch" />

    <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client"/>
    </bean>

</beans>
分享到:
评论

相关推荐

    解决spring-data-elasticsearch 5.4.0 不支持 5.4.1的elasticsearch问题

    在使用Spring Data Elasticsearch框架时,可能会遇到一个常见的问题,即版本兼容性问题。Spring Data Elasticsearch 5.4.0设计时可能并未考虑到与Elasticsearch 5.4.1的完全兼容,导致在升级Elasticsearch到5.4.1后...

    Spring Data Elasticsearch API(Spring Data Elasticsearch 开发文档).CHM

    Spring Data Elasticsearch API(Spring Data Elasticsearch 开发文档).CHM。 官网 Spring Data Elasticsearch API

    spring-data-elasticsearch

    Spring Data Elasticsearch是Spring Data项目的一部分,该项目旨在简化数据访问层的开发,提高开发效率。Spring Data Elasticsearch专门用于简化Elasticsearch数据的访问,Elasticsearch是一个高度可扩展的开源全文...

    spring-data-elasticsearch中文使用文档

    **Spring-Data-Elasticsearch中文使用文档** Spring Data Elasticsearch 是一个强大的Java库,它使得在Elasticsearch数据库中操作数据变得更加简单。这个框架是Spring Data项目的一部分,它为Elasticsearch提供了...

    spring data elasticsearch

    **Spring Data Elasticsearch 深入解析** Spring Data Elasticsearch 是一个由 Spring 社区开发的库,它为使用 Elasticsearch(一个高性能、分布式全文搜索引擎)提供了一种简单且强大的方式。Spring Data Elastic...

    spring data elasticsearch示例

    本示例程序主要是对spring data elasticsearch的实践,包含接口声明查询、注解查询和自定义repository查询。运行TestCase时候,请先将配置文件中ES服务端ip:port配置替换成真是的服务端地址

    spring data elasticsearch示例.zip

    本示例程序主要是对spring data elasticsearch的实践,包含接口声明查询、注解查询和自定义repository查询。运行TestCase时候,请先将配置文件中ES服务端ip:port配置替换成真是的服务端地址

    springdata_es_demo.zip

    本教程将深入剖析"springdata_es_demo.zip"提供的源码,帮助读者掌握如何利用SpringData Elasticsearch进行数据操作。 首先,我们需要了解SpringData Elasticsearch的基本概念。SpringData Elasticsearch是Spring ...

    Spring-Data-Elasticsearch 中文文档

    Spring-Data-Elasticsearch 中文文档

    Java进阶教程数据层全栈方案SpringData高级应用视频教程

    5.SpringData ElasticSearch实现CRUD操作 第九章 SpringData MongDB 1.SpringData MongDB简介 2.MongDB环境搭建 3.MongDB基础知识回顾 4.SpringData MongDB入门案例 5.SpringData MongDB实现CRUD操作 第十章 综合...

    spring-data-elasticsearch基本添删改查,maven项目

    在IT行业中,Spring Data Elasticsearch是一个非常重要的框架,它允许开发者轻松地与Elasticsearch数据库进行交互,实现数据的增删改查操作。本项目是一个基于Maven构建的示例,旨在帮助我们理解如何在实际应用中...

    SpringBoot整合Spring Data Elasticsearch的过程详解

    "SpringBoot整合Spring Data Elasticsearch的过程详解" SpringBoot整合Spring Data Elasticsearch是将SpringBoot框架与Elasticsearch搜索引擎进行整合,以实现数据的搜索和索引功能。下面将详细介绍SpringBoot整合...

    spring-data-elasticsearch api 离线文档

    spring-data-elasticsearch api 离线文档, spring-data-elasticsearch2.0.2spring-data-elasticsearch api spring-data-elasticsearch api 离线文档

    spring-data-elasticsearch英文版

    Spring Data Elasticsearch是Spring Data项目的一部分,它为Elasticsearch搜索引擎提供了一个集成的解决方案。Elasticsearch是一个开源的搜索引擎,它构建于Apache Lucene之上,旨在提供一个快速、可靠和可扩展的...

    Spring 各种例子:Spring Data Elasticsearch,JDBC,JPA,LDAP,MongoDB

    2. Spring Data Elasticsearch:展示了基本文本搜索、地理空间搜索和分面搜索的示例。使用了High Level REST Client作为模板和仓库的后端。 - example:展示了使用基本文本搜索、地理空间搜索和分面搜索的示例。 -...

    spring-data-elasticsearch-reference

    Spring Data Elasticsearch项目是一个将Spring的核心概念应用于使用Elasticsearch搜索引擎开发解决方案的项目。在本参考手册中,我们主要关注Spring Data 2.0.11版本,它包含了对默认RepositoryFactoryBean的支持。 ...

    Spring Data JPA中文文档[1.4.3]_springdatajpa_erlang_waitxpf_

    10. **Elasticsearch 集成**:虽然不是 Spring Data JPA 的核心功能,但 Spring Data 项目包含了对 Elasticsearch 的支持,使得存储和检索大数据变得更加容易。 通过阅读《Spring Data JPA 中文文档[1.4.3]》PDF ...

    spring整合elasticsearch-2.3.5

    本示例程序主要是spring 整合elasticsearch-2.3.5的实践,测试时先将配置文件es.properties中ES服务端es.ip,es.port, es.cluster 配置替换成自己的服务器信息

    Spring-data-elasctisearch使用笔记

    Spring Data Elasticsearch 是一个用于与 Elasticsearch 进行集成的库,它提供了与 Spring Data 框架一致的编程模型,使得在 Spring 应用中操作 Elasticsearch 更加简便。本文将深入探讨如何利用 Spring Data ...

Global site tag (gtag.js) - Google Analytics