elasticsearchrepository在字段中保存空值

时间:2018-09-22 19:04:41

标签: java spring-boot elasticsearch spring-data

我正在尝试使用spring boot + spring data + elasticsearch创建一个简单的项目。现在,我正在尝试在Elasticsearch中手动保存文档,但是“保存”方法只会创建一个空文档,其中包含空字段和一个id。我在这里想念什么?

我使用的是Elasticsearch版本2.4.6和spring-boot-starter-parent版本1.5.9

这是elasticsearch配置

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.hellokoding.springboot.Article.repository")
public class EsConfig {

@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;

@Bean
public Client client() throws Exception {

    Settings esSettings = Settings.settingsBuilder()
            .put("cluster.name", EsClusterName)
            .build();

    //https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
    return TransportClient.builder()
            .settings(esSettings)
            .build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}
}

文档类,我尝试使用@Field,但未做任何更改,我试图使索引映射尽可能简单,但又没有什么:(

@Document(indexName = "library", type = "articles")
public class Article {

@Id
private String id;

//@Field(type = FieldType.String)
private String title;

//@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
private String author;

//@Field(type = FieldType.Integer, index = FieldIndex.not_analyzed)
private int year;

//@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
private String isbn;

//@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
private String publisher;

//@Field(type = FieldType.String)
private String pub_type;

//@Field(type = FieldType.String)
private String abstrct;

//@Field(type = FieldType.String)
private String tags;

public Article() {
}

public Article(String title, String author, int year, String isbn, String publisher, String pub_type, String articleAbstract, String tags) {
    this.title = title;
    this.author = author;
    this.year = year;
    this.isbn = isbn;
    this.publisher = publisher;
    this.pub_type = pub_type;
    this.abstrct = articleAbstract;
    this.tags = tags;
}

elasticsearch存储库界面

@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {

Page<Article> findByAuthor(String author, Pageable pageable);

List<Article> findByTitle(String title);

}

服务类别

@Service
public class ArticleServiceImpl implements ArticleService {

@Autowired
private ArticleRepository articleRepository;


public void setBookRepository(ArticleRepository articleRepository) {
    this.articleRepository = articleRepository;
}

public Article save(Article article) {
    return articleRepository.save(article);
} 
}

应用程序类

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer 
{
   @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(WebApplication.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(WebApplication.class, args);
}
}

我尝试添加@EnableElasticsearchRepositories配置,但是没有改变

最后是一个用于保存文档的控制器,我知道它似乎并不实用,但是现在我只想保存文档,然后再完成jsps等。

@Controller
public class HelloController {

@Autowired
private ArticleServiceImpl articleServiceImpl;

@RequestMapping("/insert")
public String insert() {

    String title = "a title";

    String author = "sheldon cooper";

    String isbn = "an isbn";

    String publisher = "that publisher";

    String pub_type = "book";

    String articleAbstract = "this is an abstract";

    String tags = "big bang theory";

    Article article = new Article(title, author, 2020, isbn, publisher, pub_type, articleAbstract, tags);
    articleServiceImpl.save(article);

    return "hello";
}
}

因此,当我尝试保存它时,不会发生任何错误,并且会向Elasticsearch添加新文档,但这是我运行match_all时被索引的内容

{
"_index": "library",
"_type": "articles",
"_id": "AWYCh2clu71V92oy9KTB",
"_score": 1,
"_source":{
}
}

为什么没有字段值被保存? 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

将getter和setter添加到您的实体类,或使用Jackson @JsonProperty注释对字段进行注释