2

I have a entity Person, inherited by Musician and Politician and a repository PersonRepository.

I am trying to have all three entities saved into the a collection "person" in MongoDB using PersonRepository.save(..) default method but somehow, Spring-Data-MongoDB save it into 3 separate collections "person", "musician" and "politician".

Java Code:

@Document
public class Person {
    @Id private String id;

    @Indexed private String name;

    @TextIndexed private String biography;
}

@Document  
public Musician extends Person {
    private String something;
}

@Document  
public Politician extends Person {
    private String somethingElse;
}

@Repository
public interface PersonRepository extends CrudRepository<User, String> {
}

After reading some posts saying I have to set the collection name into the annotation @Document(collection = "person") for all three of the entity for the repository to save it into a same collection.

It works well but when I check the Indexes in MongoDB, somehow I get the TextIndex being named after the last entity class which is saved.

It seems that TextIndex will always being named after the entity class name being saved, and not the collection name which I have set in the document annotation.

MongoDB Shell:

db.person.getIndexes()
[
    {
        "v" : 1,
        "key" : {
                "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.person"
    },
    {
        "v" : 1,
        "key" : {
                "_fts" : "text",
                "_ftsx" : 1
        },
        "name" : "Musician_TextIndex",
        "ns" : "test.person",
        "weights" : {
                "description" : 1,
                "name" : 10
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 3
    }
]

Is there any way I could set the TextIndex name instead of naming it after the entity class which I am saving. Tried to search could find any.

1 Answer 1

0

Currently there is no way of setting the index name for TextIndex using the annotation based setup. To do so please use the IndexOperations via the template to set up the text index manually.

template.indexOps(Person.class)
  .ensureIndex(
     new TextIndexDefinitionBuilder()
       .named("YourIndexNameHere")
       .onField("biography")
       .build());
Sign up to request clarification or add additional context in comments.

5 Comments

Tried this but if your the manually created TextIndex name is different from the {class name}_TextIndex, whenever you do a findXXX mongoTemplate will throw an exception saying {class name}_TextIndex not found.
I've set up a sample and everything seems to work as expected. Please have a look.
Thanks for the sample. I think I missed the part setting text index manually. I didn't remove the TextIndex annotation when I try manual adding index. Works like a charm. Thanks. But still wondering why does spring mongodb TextIndex doesn't named according to the annotated collection name. I studied some of the codes to the part where TextIndex are created. The collection name is there to be used.
@ChristophStrobl I used your PR as an answer :)

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.