如何将单个文档保存到 Elasticsearch 索引?
How to save single document to Elasticsearch index?
我正在尝试将示例性的两个字段 book
class 实体放入我的 ES 索引映射中,如下所示。结果我只创建了索引和映射,但 JSON 没有放在适当的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ElasticSearchTransactionsApplicationTests {
@Autowired
private BookRepository bookRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void shouldCreateSingleBookEntityWitRepository(){
elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class);
//given
IndexQuery queryCreatingBookEntity = new BookBuilder("1").setTitle("BBB").buildBookIndex();
//when
elasticsearchTemplate.index(queryCreatingBookEntity);
elasticsearchTemplate.refresh(Book.class);
//then
assertThat(bookRepository.findOne("1"), is(notNullValue()));
}
虽然它创建了索引和映射,但它没有将书放入其中 - 为什么?
编辑
像这样测试文件:
java.lang.AssertionError:
Expected: is not null
but: was null
Expected :is not null
Actual :null
<Click to see difference>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
当我GET _all/_mapping
{
"bookshop": {
"mappings": {
"book": {
"properties": {
"bid": {
"type": "string"
},
"title": {
"type": "string"
}
}
}
}
}
}
我还检查了我创建的索引和映射,但是当我尝试获取内容时:
获取/bookshop/book/1
{
"_index": "bookshop",
"_type": "book",
"_id": "1",
"found": false
}
所以我得到 found false。所以没有添加书籍。
编辑 2
我有 ES 2.3.5 从他们的官方回购。以下是版本包。
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency> <!--version 2.0.2 to getClient form ElasticTemplate-->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
</dependencies>
<!-- For executable JAR packaging-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository><!--version 2.0.2 to getClient form ElasticTemplate-->
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
Book.java
@Data
@Document(indexName = "bookshop", type = "book", shards = 1, replicas = 0, refreshInterval = "-1")
public class Book {
@Id
private String bID;
@Field(type = FieldType.String, store = true)
private String title;
}
我还把我的 book.java 与 lombok 生成的@Data 和扩展的@Document 内容放在一起
我的代码和你的差不多。我很久以前玩 Spring Data Elasticsearch 时就用过这个。我刚刚更新了版本 1.4.0.RELEASE
测试通过了。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.demo</groupId>
<artifactId>data-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>data-demo</name>
<description>Demo project for Spring Data ES and JPA</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Book.class
package com.demo.model;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.annotation.Id;
/**
* Holds the details of book.
*/
@Document(indexName = "book")
public class Book {
@Id
private String bookId;
private String title;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
存储库
package com.demo.search.repository;
import com.demo.model.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* Holds the Elasticsearch CRUD operations for {@link Book} entity.
*/
public interface BookSearchRepository extends ElasticsearchRepository<Book, String> {
}
最后,测试。
package com.demo.search.repository;
import com.demo.model.Book;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Test things on {@link BookSearchRepository}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BookSearchRepositoryTest {
@Autowired
private BookSearchRepository bookRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class);
}
@Test
public void shouldSaveBook(){
Book book = new Book();
book.setBookId("1");
book.setTitle("Learning Scala");
Book indexedBook = bookRepository.save(book);
assertThat(indexedBook, is(notNullValue()));
assertThat(indexedBook.getBookId(), is(book.getBookId()));
assertThat(bookRepository.findOne("1"), is(notNullValue()));
}
}
我正在尝试将示例性的两个字段 book
class 实体放入我的 ES 索引映射中,如下所示。结果我只创建了索引和映射,但 JSON 没有放在适当的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ElasticSearchTransactionsApplicationTests {
@Autowired
private BookRepository bookRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void shouldCreateSingleBookEntityWitRepository(){
elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class);
//given
IndexQuery queryCreatingBookEntity = new BookBuilder("1").setTitle("BBB").buildBookIndex();
//when
elasticsearchTemplate.index(queryCreatingBookEntity);
elasticsearchTemplate.refresh(Book.class);
//then
assertThat(bookRepository.findOne("1"), is(notNullValue()));
}
虽然它创建了索引和映射,但它没有将书放入其中 - 为什么?
编辑 像这样测试文件:
java.lang.AssertionError:
Expected: is not null
but: was null
Expected :is not null
Actual :null
<Click to see difference>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
当我GET _all/_mapping
{
"bookshop": {
"mappings": {
"book": {
"properties": {
"bid": {
"type": "string"
},
"title": {
"type": "string"
}
}
}
}
}
}
我还检查了我创建的索引和映射,但是当我尝试获取内容时: 获取/bookshop/book/1
{
"_index": "bookshop",
"_type": "book",
"_id": "1",
"found": false
}
所以我得到 found false。所以没有添加书籍。
编辑 2 我有 ES 2.3.5 从他们的官方回购。以下是版本包。 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency> <!--version 2.0.2 to getClient form ElasticTemplate-->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
</dependencies>
<!-- For executable JAR packaging-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository><!--version 2.0.2 to getClient form ElasticTemplate-->
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
Book.java
@Data
@Document(indexName = "bookshop", type = "book", shards = 1, replicas = 0, refreshInterval = "-1")
public class Book {
@Id
private String bID;
@Field(type = FieldType.String, store = true)
private String title;
}
我还把我的 book.java 与 lombok 生成的@Data 和扩展的@Document 内容放在一起
我的代码和你的差不多。我很久以前玩 Spring Data Elasticsearch 时就用过这个。我刚刚更新了版本 1.4.0.RELEASE
测试通过了。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.demo</groupId>
<artifactId>data-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>data-demo</name>
<description>Demo project for Spring Data ES and JPA</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Book.class
package com.demo.model;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.annotation.Id;
/**
* Holds the details of book.
*/
@Document(indexName = "book")
public class Book {
@Id
private String bookId;
private String title;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
存储库
package com.demo.search.repository;
import com.demo.model.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* Holds the Elasticsearch CRUD operations for {@link Book} entity.
*/
public interface BookSearchRepository extends ElasticsearchRepository<Book, String> {
}
最后,测试。
package com.demo.search.repository;
import com.demo.model.Book;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Test things on {@link BookSearchRepository}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BookSearchRepositoryTest {
@Autowired
private BookSearchRepository bookRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class);
}
@Test
public void shouldSaveBook(){
Book book = new Book();
book.setBookId("1");
book.setTitle("Learning Scala");
Book indexedBook = bookRepository.save(book);
assertThat(indexedBook, is(notNullValue()));
assertThat(indexedBook.getBookId(), is(book.getBookId()));
assertThat(bookRepository.findOne("1"), is(notNullValue()));
}
}