In recent projects, elatic search is used to do data statistics, which I haven't studied before, just to learn now, we use spring data elatic search to use elatic search, which will be very simple to use. Let's summarize and record this process. There are many examples on the internet, but none of them is complete, and there are always fewer and fewer.
1. Create a maven project.
Import the spring package and spring data elaticsearch package. The pom.xml file is as follows:
<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>me.ecs</groupId> <artifactId>my-spring-data-elaticsearch</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>my-spring-data-elaticsearch</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-data.elsaticsearch>2.1.0.M1</spring-data.elsaticsearch> <spring.version>4.2.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <!--This must include--> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>${spring-data.elsaticsearch}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.6</version> </dependency> </dependencies> </project>
This is the simplest configuration.
In the process of using, note that the file pom.xml must contain sping-context, because if you don't include it, he has spring-context 4.3 versions.
Errors are reported when running, as follows
java.lang.NoSuchMethodError
The reason is that it refers to incompatible versions in pom.xml
So be sure to fix the spring-context version, and he can fix the rest as well.
You can see that some core packages of spring are used here, but some packages of spring web are not used because the web is not used.
2. Establish the following package structure
a. With spring, it allows us to define repository bean s and initialize an Elastic search Client to connect to the designated server. These spring data are all ready for us, as long as we configure them as follows, we can use them directly later.
The configuration file is as follows
<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"> <!--Connected elaticsearch The server--> <elasticsearch:transport-client id="esTransportClient" cluster-nodes="127.0.0.1:9300" cluster-name="xiajunxiang-my-application" /> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg ref="esTransportClient"/> </bean> <!--Namespace creation Elasticsearch repositories) --> <elasticsearch:repositories base-package="me.ecs.my_spring_data_elaticsearch.repository"/> </beans>
b. Define a document object
package me.ecs.my_spring_data_elaticsearch.document; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; /** * Created by xiajunxiang on 2017 29 March 2000 */ @Document(indexName = "ecs.book", type = "book") public class Book { @Id private Long id; private String name; private Long price; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + "]"; } }
c. Create your own repository to inherit Elastic search repository
package me.ecs.my_spring_data_elaticsearch.repository; import me.ecs.my_spring_data_elaticsearch.document.Book; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * Created by xiajunxiang on 2017 29 March 2000 */ public interface BookRepositytory extends ElasticsearchRepository<Book,Long>{ }
d. Creating test cases
package me.ecs.my_spring_data_elaticsearch; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; import me.ecs.my_spring_data_elaticsearch.document.Book; import me.ecs.my_spring_data_elaticsearch.repository.BookRepositytory; 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; /** * Created by xiajunxiang on 2017 29 March 2000 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext-elasticsearch.xml"}) public class BookRepositytoryTest { @Autowired private BookRepositytory bookRepositytory; @Test public void shouldIndexSingleBookEntity(){ Book book = new Book(); book.setId(123456L); book.setName("Spring Data Elasticsearch"); book.setPrice(13L); //book.setVersion(System.currentTimeMillis()); bookRepositytory.save(book); //lets try to search same record in elasticsearch Book indexedBook = bookRepositytory.findOne(book.getId()); assertThat(indexedBook,is(notNullValue())); assertThat(indexedBook.getId(),is(book.getId())); } @Test public void countBook(){ long count=bookRepositytory.count(); assertThat(count, is(2L)); } }
It's ready to run.