@IndexName(child = true)
@Data
public class Comment {
// 省略其它字段...
/**
* 父子关系字段 须通过注解在父文档及子文档的实体类中指明其类型为Join,子文档中的父子关系可省略
*/
@IndexField(fieldType = FieldType.JOIN)
private JoinField joinField;
private String id;
private String commentContent;
}
/**
* 文档实体
*/
@Data
@IndexName(childClass = Comment.class)
public class Document {
/**
* es中的唯一id
*/
private String id;
/**
* 文档标题
*/
private String title;
/**
* 文档内容
*/
private String content;
@IndexField(fieldType = FieldType.JOIN, parentName = "document", childName = "comment")
private JoinField joinField;
}
@Test
public void testInsert() {
//documentMapper.createIndex();
// 测试新增父子文档,此处开启自动挡模式,父子类型索引已被自动处理
// 新新增父文档,然后再插入子文档
Document document = new Document();
document.setId("1");
document.setTitle("父文档的标题");
document.setContent("父文档的内容");
JoinField joinField = new JoinField();
joinField.setName("document");
document.setJoinField(joinField);
documentMapper.insert(document);
// 插入子文档
Comment comment = new Comment();
comment.setId("2");
comment.setCommentContent("文档的评论1");
// 这里特别注意,子文档必须指定其父文档的id,否则找不到父文档别怪我没提醒
joinField.setParent("1");
joinField.setName("comment");
comment.setJoinField(joinField);
commentMapper.insert(comment);
}
@Test
public void testSelect1() {
LambdaEsQueryWrapper<Document> documentWrapper = new LambdaEsQueryWrapper<>();
documentWrapper.hasChild("comment", FieldUtils.val(Comment::getCommentContent), "文档的评论1");
List<Document> documents = documentMapper.selectList(documentWrapper);
System.out.println(documents);
}
能够实现别名查询得功能,比如说通过吃鸡来匹配PUBG绝地求生等功能