http://stackoverflow.com/questions/23730641/parent-child-relationships-in-spring-data-elastic-search
父实体类及子实体类的搜索方法如下:
- @Document(indexName = "parent-child", type = "parent-entity")
- public class ParentEntity {
- @Id
- private String id;
- @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
- private String name;
- // setter/getter
- public ParentEntity() {
- }
- public ParentEntity(String id, String name) {
- this.id = id;
- this.name = name;
- }
- }
- @Document(indexName = "parent-child", type = "child-entity")
- public class ChildEntity {
- @Id
- private String id;
- @Field(type = FieldType.String, store = true)
- @Parent(type = "parent-entity")
- private String parentId;
- @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
- private String name;
- public ChildEntity() {
- }
- public ChildEntity(String id, String parentId, String name) {
- this.id = id;
- this.parentId = parentId;
- this.name = name;
- }
- }
// 为父实体类做索引(你还可以使用许多其它的方法做索引 例如: repositories)
- ParentEntity parent1 = new ParentEntity("parent1", "First Parent");
- IndexQuery parentIndex1 = new IndexQuery();
- parentIndex1.setId(parent1.getId());
- parentIndex1.setObject(parent1);
- elasticsearchTemplate.index(parentIndex1);
- ParentEntity parent2 = new ParentEntity("parent2", "Second Parent");
- IndexQuery parentIndex2 = new IndexQuery();
- parentIndex2.setId(parent2.getId());
- parentIndex2.setObject(parent2);
- elasticsearchTemplate.index(parentIndex2);
// 为子实体类做索引
- ChildEntity child1 = new ChildEntity("child1", parent1.getId(), "First");
- IndexQuery childIndex1 = new IndexQuery();
- childIndex1.setId(child1.getId());
- childIndex1.setObject(child1);
- childIndex1.setParentId(child1.getParentId());
- elasticsearchTemplate.index(childIndex1);
- ChildEntity child2 = new ChildEntity("child2", parent1.getId(), "Second");
- IndexQuery childIndex2 = new IndexQuery();
- childIndex2.setId(child2.getId());
- childIndex2.setObject(child2);
- childIndex2.setParentId(child2.getParentId());
- elasticsearchTemplate.index(childIndex2);
//搜索
对于主从结构的实体类,有三种可选的搜索方法:
1.has children 点击打开链接
2.has parent 点击打开链接
3.top children 点击打开链接
- QueryBuilder query = topChildrenQuery("child-entity", QueryBuilders.termQuery("name", child1name.toLowerCase()));
- SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
- List<ParentEntity> parents = elasticsearchTemplate.queryForList(searchQuery, ParentEntity.class);
http://blog.csdn.net/wilsonke/article/details/44113785
相关推荐
Spring Data Elasticsearch API(Spring Data Elasticsearch 开发文档).CHM。 官网 Spring Data Elasticsearch API
spring-data-elasticsearch中文使用文档,spring-data-elasticsearch、elasticsearch、ES、ElasticSearch、ES中文教程
解决spring-data-elasticsearch 5.4.0 不支持 5.4.1的elasticsearch问题
本示例程序主要是对spring data elasticsearch的实践,包含接口声明查询、注解查询和自定义repository查询。运行TestCase时候,请先将配置文件中ES服务端ip:port配置替换成真是的服务端地址
spring data elasticsearch
spring-data-elasticsearch api 离线文档, spring-data-elasticsearch2.0.2spring-data-elasticsearch api spring-data-elasticsearch api 离线文档
本示例程序主要是对spring data elasticsearch的实践,包含接口声明查询、注解查询和自定义repository查询。运行TestCase时候,请先将配置文件中ES服务端ip:port配置替换成真是的服务端地址
v /Users/xingyue/Home/xingyue/学习/工程化/es/data:/usr/share/elasticsearch/data -v /Users/xingyue/Home/xingyue/学习/工程化/es/config:/usr/share/elasticsearch/config -v /Users/xingyue/Home/xingyue/学习...
5.SpringData ElasticSearch实现CRUD操作 第九章 SpringData MongDB 1.SpringData MongDB简介 2.MongDB环境搭建 3.MongDB基础知识回顾 4.SpringData MongDB入门案例 5.SpringData MongDB实现CRUD操作 第十章 综合...
2. Spring Data Elasticsearch:展示了基本文本搜索、地理空间搜索和分面搜索的示例。使用了High Level REST Client作为模板和仓库的后端。 - example:展示了使用基本文本搜索、地理空间搜索和分面搜索的示例。 -...
主要介绍了SpringBoot整合Spring Data Elasticsearch的过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本示例程序主要是spring 整合elasticsearch-2.3.5的实践,测试时先将配置文件es.properties中ES服务端es.ip,es.port, es.cluster 配置替换成自己的服务器信息
spring-data-elasticsearch api文档
Spring-Data-Elasticsearch 中文文档
spring-data-jest, Jest的Spring Data 实现 Spring Data Jest 基于on客户端的ElasticSearch的Spring Data 实现仅在 HTTP ( 例如AWS上) 可以访问的情况下使用 Spring Data 和ElasticSearch群集。
spring data elasticsearch官方文档(中文翻译)
Spring-data-elasticsearch参考文档英文版,喜欢英语或者有受虐倾向的可以尝试下
基于ElasticSearch6.8.2进行开发项目描述基于Docker环境运行的ElasticSearch6.8.2,目前项目中集成了3种与ElasticSearch交互的API:使用Spring Data ElasticSearch 3.2.0,需使用ES API为6.8.2使用Rest High Level ...
1、进入到Elasticsearch的安装路径下,本文中以该路径为例子:/ultra/ES/elasticsearch-2.3.4。先安装license,执行以下命令: ./bin/plugin install license 2、再安装shield,执行以下命令: ./bin/plugin ...