Spring 数据 Elasticsearch 继承

Spring Data Elasticsearch Inheritance

有什么方法可以制作超级class文档(例如index name = user)并创建两个子classes(AdminGuest ) 将所有这些保存到 user 索引但具有不同的字段?例如。添加到 super-class 字段 type 并基于此字段获取正确的实体? ELK 7.19,Spring 数据 4.3.1.

你可以的。使基础 class 抽象。我在以下 classes:

的测试设置中有这个
@Document(indexName = "type-hints")
public abstract class BaseClass {

    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String baseText;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBaseText() {
        return baseText;
    }

    public void setBaseText(String baseText) {
        this.baseText = baseText;
    }

    @Override
    public String toString() {
        return "BaseClass{" +
            "id='" + id + '\'' +
            ", baseText='" + baseText + '\'' +
            '}';
    }
}
public class DerivedOne extends BaseClass {
    @Field(type = FieldType.Text)
    private String derivedOne;

    public String getDerivedOne() {
        return derivedOne;
    }

    public void setDerivedOne(String derivedOne) {
        this.derivedOne = derivedOne;
    }

    @Override
    public String toString() {
        return "DerivedOne{" +
            "derivedOne='" + derivedOne + '\'' +
            "} " + super.toString();
    }
}
public class DerivedTwo extends BaseClass {
    @Field(type = FieldType.Text)
    private String derivedTwo;

    public String getDerivedTwo() {
        return derivedTwo;
    }

    public void setDerivedTwo(String derivedTwo) {
        this.derivedTwo = derivedTwo;
    }

    @Override
    public String toString() {
        return "DerivedTwo{" +
                "derivedTwo='" + derivedTwo + '\'' +
                "} " + super.toString();
    }
}
interface TypeHintRepository extends ElasticsearchRepository<BaseClass, String> {
    SearchHits<? extends BaseClass> searchAllBy();
}
@RestController
@RequestMapping("/typehints")
public class TypeHintController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TypeHintController.class);

    private final TypeHintRepository repository;

    public TypeHintController(TypeHintRepository repository) {
        this.repository = repository;
    }

    @GetMapping
    public void test() {

        List<BaseClass> docs = new ArrayList<>();

        DerivedOne docOne = new DerivedOne();
        docOne.setId("one");
        docOne.setBaseText("baseOne");
        docOne.setDerivedOne("derivedOne");
        docs.add(docOne);

        DerivedTwo docTwo = new DerivedTwo();
        docTwo.setId("two");
        docTwo.setBaseText("baseTwo");
        docTwo.setDerivedTwo("derivedTwo");
        docs.add(docTwo);

        repository.saveAll(docs);

        SearchHits<? extends BaseClass> searchHits = repository.searchAllBy();

        for (SearchHit<? extends BaseClass> searchHit : searchHits) {
            LOGGER.info(searchHit.toString());
        }
    }
}