gpt4 book ai didi

elasticsearch - 如何将单个文档保存到Elasticsearch索引?

转载 作者:行者123 更新时间:2023-12-02 22:59:33 25 4
gpt4 key购买 nike

我正在尝试将ES索引映射到示例性的两个字段book类实体中,如下所示。结果,我仅获得创建的索引和映射,但是未放置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()));
}

在创建索引和映射BUT时,没有将Book放入其中-为什么?

编辑
测试这样的文件:
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"
}
}
}
}
}
}

我也检查我创建的索引和映射,但是当我尝试获取内容时:
GET / bookshop / book / 1
{
"_index": "bookshop",
"_type": "book",
"_id": "1",
"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()));
}
}

关于elasticsearch - 如何将单个文档保存到Elasticsearch索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38783002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com