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

时间:2016-08-05 06:55:58

标签: elasticsearch spring-data-elasticsearch

我正在尝试将我的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()));
    }

虽然它创建了索引和映射但它没有把书放在里面 - 为什么?

修改 像这样的测试文件:

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内容

1 个答案:

答案 0 :(得分:2)

我的代码与您的代码几乎相似。我在很久以前玩Spring Data Elasticsearch时已经习惯了这个。我刚刚将版本更新为ISODate("2016-03-03T12:15:49.996Z")测试正在通过。

pom.xml

1.4.0.RELEASE

Book.class

<?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>

存储库

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> {
}