2

I have some trouble with inheritence and ElasticsearchRepository. My code (short version) : Entities :

public abstract class father{
    @Id 
    protected String identifier;

    // constructors
    @Field(type= FieldType.String, index = FieldIndex.not_analyzed)
    public String uri;
    // getter setter
}

/*
* Stored in elastic search
*/
@Document(indexName = "entity1", type = "entity1")
public abstract class sonA extends father{
    // constructors
}

/*
* Stored in elastic search
*/
@Document(indexName = "entity2", type = "entity2")
public abstract class sonB extends father{
    // constructors
}

Repository :

public interface DataBnfRepository extends
        ElasticsearchRepository<SonA, String> {

    public SonA findByUri(String uri);

    @Query("{ \"filtered\":{ \"filter\":{\"term\":{\"uri\":\"?0\"}}}}")
    public SonA findWithUri(String uri);
}

My trouble : I'm able to put data into elastic search but not to retreive them. If I do a repository.findall() it works. If I do a repository.findWithUri(uri) it does not work (null result) Parse exception for findByUri

I tried to search for exemples : https://github.com/spring-projects/spring-data-elasticsearch/blob/master/src/test/java/org/springframework/data/elasticsearch/entities/SampleInheritedEntity.java But there's no repository.

What I have found: If i replace the uri by"foo", it works. So the problem is with https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters

So i made a test with

uri = foo:bar => parse exception.

uri = "foo:bar" => parse exception.

uri = foo\:bar => parse exception.

uri = "foo\:bar" => parse exception

Before it was inherited I had no problems with the code. I just put the URI in an object, repository.save(object) and repository.findWithUri(uri), it give me the object.

Any help/suggestion/comments is greatly appreciated. Please. Thank you so much

2
  • Hi, did you find any solution for using the abstract class in spring data elasticsearch as I am facing issues in using spring-data-elasticsearch and there are not any examples/references on the internet. Commented Feb 15, 2020 at 9:24
  • If you query the index is there data in it? Try curl http://localhost:9200/_cat/indices Commented May 15, 2020 at 10:53

1 Answer 1

0

Try the following method.

findByFatherUri(uri);

This is how it works for Child Entities. I think it's the same for Extended Classes as well. From Spring Data's point of view, main class is SonA for DataBnfRepository and it's id is the one linked to the Repository when you use findOne method. You have to traverse through the child class to the field you are trying use to find the object. Since, I am not sure 100%, let me know if this works once you tried.

Sign up to request clarification or add additional context in comments.

3 Comments

It does not work (for the moment) I have the error : No property FatherUri found for type SonA. But thanks for the help, i looking forward on this possible solution, maybe there is a trick to do it.
Okies, Sorry. Quick question though, what is DataBnfEntity'? and how it is related to sonA? Are these same? If they are same, why do you have it as abstract class? If DataBnfEntity` is parent of sonA, you have to traverse from DataBnfEntity to Father like findByDataBnfEntitySonAFatherUri(uri).
Sorry I forgot to rename it too. DataBnfEntity => SonA (I work on RDF database, but I filtered informations) Edited the question, sorry about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.