Git Product home page Git Product logo

fazaza / spring-data-elasticsearch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-projects/spring-data-elasticsearch

0.0 0.0 0.0 21.46 MB

Provide support to increase developer productivity in Java when using Elasticsearch. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.

Home Page: https://spring.io/projects/spring-data-elasticsearch/

Java 97.86% CSS 2.14%

spring-data-elasticsearch's Introduction

icon?job=spring data elasticsearch%2Fmaster&subject=Moore%20(master) icon?job=spring data elasticsearch%2F3.1.x&subject=Lovelace%20(3.1 icon?job=spring data elasticsearch%2F2.1.x&subject=Ingalls%20(2.1

Spring Data Elasticsearch

Spring Data implementation for ElasticSearch

Spring Data makes it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services as well as provide improved support for relational database technologies.

The Spring Data Elasticsearch project provides integration with the elasticsearch search engine.

Quick Start

This section is just short introduction, for more information refer to the reference documentation.

Versions

The following table shows the Elasticsearch versions that are used by Spring Data Elasticsearch:

Spring Data Elasticsearch Elasticsearch

3.2.x

6.7.2

3.1.x

6.2.2

3.0.x

5.5.0

2.1.x

2.4.0

2.0.x

2.2.0

1.3.x

1.5.2

Maven configuration

Add the Maven dependency:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>x.y.z.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>x.y.z.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/libs-snapshot</url>
</repository>

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. For a detailed information about Spring Data, repositories and the supported query methods check the reference documentation.

    @NoRepositoryBean
    public interface ElasticsearchRepository<T, ID> extends ElasticsearchCrudRepository<T, ID> {

        <S extends T> S index(S entity);

        Iterable<T> search(QueryBuilder query);

        Page<T> search(QueryBuilder query, Pageable pageable);

        Page<T> search(SearchQuery searchQuery);

        Page<T> searchSimilar(T entity, String[] fields, Pageable pageable);

        void refresh();

        Class<T> getEntityClass();
    }

    @NoRepositoryBean
    public interface ElasticsearchCrudRepository<T, ID> extends PagingAndSortingRepository<T, ID> {
    }
Extending ElasticsearchRepository with custom methods:
    public interface BookRepository extends ElasticsearchRepository<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 using a Repository:
        @Autowired
        private SampleElasticsearchRepository repository;

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

        repository.save(sampleEntity);
Indexing multiple documents (bulk index) using a 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 and ElasticsearchRestTemplate

ElasticsearchTemplate and ElasticsearchRestTemplate are the central support classes for Elasticsearch operations, both implement the ElasticsearchOperations interface that defines the methods to operate on an Elasticsearch cluster.

ElasticsearchTemplate uses a TransportClient, whereas ElasticsearchRestTemplate uses the RestHighLevelClient. The TransportClient is deprecated in Elasticsearch 7, but until it is removed from Elasticsearch, the ElasticsearchTemplate will be supported as well.

Indexing a single document using ElasticsearchTemplate:
        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 documents (bulk index) using ElasticsearchTemplate:
        @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 ElasticsearchTemplate:
        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;

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

Reactive Elasticsearch

The ReactiveElasticsearchClient, introduced in Spring Data Elasticsearch 3.2, is a non official driver based on WebClient. It uses the request/response objects provided by the Elasticsearch core project.

@Configuration
public class Config {

  @Bean
  ReactiveElasticsearchClient client() {

    ClientConfiguration clientConfiguration = ClientConfiguration.builder()
      .connectedTo("localhost:9200", "localhost:9291")
      .build();

    return ReactiveRestClients.create(clientConfiguration);
  }
}

// ...

Mono<IndexResponse> response = client.index(request ->

  request.index("spring-data")
    .type("elasticsearch")
    .id(randomID())
    .source(singletonMap("feature", "reactive-client"))
    .setRefreshPolicy(IMMEDIATE)
);

The reactive client response, especially for search operations, is bound to the from (offset) & size (limit) options of the request.

ReactiveElasticsearchOperations is the gateway to executing high level commands against an Elasticsearch cluster using the ReactiveElasticsearchClient. The easiest way of setting up the ReactiveElasticsearchTemplate is via AbstractReactiveElasticsearchConfiguration.

@Configuration
public class Config extends AbstractReactiveElasticsearchConfiguration {

    @Bean
    @Override
    public ReactiveElasticsearchClient reactiveElasticsearchClient() {
        // ...
    }
}

If needed the ReactiveElasticsearchTemplate can be configured with default RefreshPolicy and IndicesOptions that get applied to the related requests by overriding the defaults of refreshPolicy() and indicesOptions().

template.save(new Person("Bruce Banner", 42))
    .doOnNext(System.out::println)
    .flatMap(person -> template.findById(person.id, Person.class))
    .doOnNext(System.out::println)
    .flatMap(person -> template.delete(person))
    .doOnNext(System.out::println)
    .flatMap(id -> template.count(Person.class))
    .doOnNext(System.out::println)
    .subscribe();

The above outputs the following sequence on the console.

> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> QjWCWWcBXiLAnp77ksfR
> 0

XML Namespace

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

Using TransportClient
<?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 https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
		http://www.springframework.org/schema/beans https://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>
Using RestClient
<?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 https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

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

    <elasticsearch:rest-client id="restClient" hosts="http://localhost:9200"/>

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


</beans>

Contributing to Spring Data

Here are some ways for you to get involved in the community:

  • Get involved with the Spring community on Stack OverFlow. Please help out on the forum by responding to questions and joining the debate.

  • Create JIRA tickets for bugs and new features and comment and vote on the ones that you are interested in.

  • Github is for social coding: if you want to write code, we encourage contributions through pull requests from forks of this repository. If you want to contribute code this way, please reference a JIRA ticket as well covering the specific issue you are addressing.

  • Watch for upcoming articles on Spring by subscribing to springframework.org

Before we accept a pull request we will need you to sign the Contributor License Agreement. Signing the contributor’s agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. If you forget to do so, you’ll be reminded when you submit a pull request. Active contributors might be asked to join the core team, and given the ability to merge pull requests.

Code formatting for Eclipse and Intellij

Running CI tasks locally

Since this pipeline is purely Docker-based, it’s easy to:

  • Debug what went wrong on your local machine.

  • Test out a a tweak to your test.sh script before sending it out.

  • Experiment against a new image before submitting your pull request.

All of these use cases are great reasons to essentially run what the CI server does on your local machine.

Important
To do this you must have Docker installed on your machine.
  1. docker run -it --mount type=bind,source="$(pwd)",target=/spring-data-elasticsearch-github adoptopenjdk/openjdk8:latest /bin/bash

    This will launch the Docker image and mount your source code at spring-data-elasticsearch-github.

  2. cd spring-data-elasticsearch-github

    Next, run your tests from inside the container:

  3. ./mvnw clean dependency:list test -Dsort (or whatever profile you need to test out)

Since the container is binding to your source, you can make edits from your IDE and continue to run build jobs.

If you need to test the build.sh script, do this:

  1. docker run -it --mount type=bind,source="$(pwd)",target=/spring-data-elasticsearch-github adoptopenjdk/openjdk8:latest /bin/bash

    This will launch the Docker image and mount your source code at spring-data-elasticsearch-github.

  2. cd spring-data-elasticsearch-github

    Next, try to package everything up from inside the container:

  3. ./mvnw -Pci,snapshot -Dmaven.test.skip=true clean deploy

Important
This will attempt to deploy to artifactory, but without credentials, it will fail, leaving you simply with a built artifact.
Note
Docker containers can eat up disk space fast! From time to time, run docker system prune to clean out old images.

spring-data-elasticsearch's People

Contributors

mohsinh avatar odrotbohm avatar mp911de avatar akonczak avatar christophstrobl avatar spring-builds avatar xhaggi avatar kevinleturc avatar sothawo avatar fmarchand avatar puug avatar hobbut avatar fhopf avatar stuartstevenson avatar michalj avatar gregturn avatar amwaheed avatar dandonovsc avatar tedliang avatar rizwanidrees avatar pnowak85 avatar mehiel avatar pulse00 avatar rpuch avatar henszey avatar spyna avatar szabobalint-virgo avatar thiagolocatelli avatar tsachev avatar hyysguyang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.