mishoolak
mishoolak

Reputation: 43

elasticsearchrepository saves null values in fields

I'm trying to make a simple project with spring boot + spring data + elasticsearch. For now I'm trying to save a document manually in elasticsearch however the "save" method only creates an empty document with null fields and only an id. What am I missing here?

I'm using elasticsearch version 2.4.6 and spring-boot-starter-parent version 1.5.9

here is elasticsearch config

@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());
}
}

the document class, I tried using @Field but nothing changed, I tried to make my index mapping as simple as possible but again, nothing :(

@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 interface

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

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

List<Article> findByTitle(String title);

}

a service class

@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);
} 
}

the application class

@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);
}
}

I tried adding the @EnableElasticsearchRepositories configuration but didn't change a thing

and finally here is a controller where I save the document, I know it doesn't seem practical but now I just want to save a document and later on complete the jsps and etc.

@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";
}
}

so when I try to save it no errors occur and a new document is added to elasticsearch however this is what is indexed when I run a match_all

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

why none of the field values are saved? thanks in advance :)

Upvotes: 1

Views: 1743

Answers (1)

Vlad Mamaev
Vlad Mamaev

Reputation: 565

add getters and setters to your entity class, or annotate fields with Jackson @JsonProperty annotation

Upvotes: 2

Related Questions