elasticsearch7.8父子结构设计以及mapping映射

elasticsearch7.8父子结构设计以及mapping映射

设计父子结构

建立index1索引,父:parent,子:child
父包含id, parentName字段(数据:父亲1,父亲2
子包含id, childName, parentId字段(数据:父亲1有一儿一女,父亲2有一儿
relation表示关系,名称自定义,type:join 表示父子结构

PUT index1
PUT index1/_mapping
{
  "properties": {
    "parent": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "parentName": {
          "type": "text"
        }
      }
    },
    "child": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "childName": {
          "type": "text"
        },
        "parentId": {
          "type": "integer"
        }
      }
    },
    "relation": {
      "type": "join",
      "relations": {
        "parent": [
          "child"
        ]
      }
    }
  }
}


建立父 parent 数据

POST index1/_doc/1
{
  "parent.id":1,
  "relation":"parent",
  "parent.parentName":"父亲1"
}
POST index1/_doc/2
{
  "parent.id":2,
  "relation":"parent",
  "parent.parentName":"父亲2"
}

建立父亲1的子数据

POST index1/_doc/101?routing=1
{ 
  "child.id":101,
  "relation":
  {
    "name":"child",
    "parent":1
  },
  "child.childName":"父亲1的儿子",
  "child.parentId":1
}
POST index1/_doc/102?routing=1
{
  "child.id":101,
  "relation":
  {
    "name":"child",
    "parent":1
  },
  "child.childName":"父亲1的女儿",
  "child.parentId":1
}

建立父亲2的子数据

POST index1/_doc/201?routing=2
{
  "child.id":201,
  "relation":
  {
    "name":"child",
    "parent":2
  },
  "child.childName":"父亲2的儿子",
  "child.parentId":2
}

测试

#以子查父
GET index1/_search
{
  "query": {
    "has_child": {
      "type": "child",
      "query": {
        "term": {
          "child.parentId": "1"
        }
      }
    }
  }
}

在这里插入图片描述

#带inner_hits 以子查父,查出父子级联信息
GET index1/_search
{
  "query": {
    "has_child": {
      "type": "child",
      "query": {
        "term": {
          "child.parentId": 1
        }
      },
      "inner_hits": {}
    }
  }
}

在这里插入图片描述

#以父查子
GET index1/_search
{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "query": {
        "term": {
          "parent.id": "1"
        }
        
        }
      }
    }  
}

查询结果:
在这里插入图片描述
使用RestHighLevelClient代码查询

 // 带子级查询
    @Test
    public void test1() throws IOException{
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("index1");
        HasChildQueryBuilder qb = JoinQueryBuilders.hasChildQuery(
                "child",                         //要查询的子类型
                QueryBuilders.termQuery("child.parentId","1"),
                ScoreMode.None
        ).innerHit(new InnerHitBuilder());
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(qb);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        response.getHits().forEach(message->{
            System.out.println(message.getSourceAsString());
            message.getInnerHits().get("child").forEach(e ->{
                System.out.println(e.getSourceAsString());
            });
        });
    }

在这里插入图片描述

要注意的细节挺多,父子数据要在一个分配,索引建立子数据,必须设定routing为父id
注意父子结构容易出现的问题是,添加数据时,要注意带上 parent.id 这种关系,否则不会用我们自定义的映射mapping关系,es会自动匹配字段类型,添加完数据一定要 GET index1/_mapping查看结构是否变化了。

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Elasticsearch 7.8的安装和配置包括以下几个步骤: 1. 修改端口配置:将elasticsearch-a、elasticsearch-b、elasticsearch-c三个es服务的端口分别修改为9211、9212、9213。具体操作是编辑每个服务的配置文件,分别为elasticsearch-a/config/elasticsearch.yml、elasticsearch-b/config/elasticsearch.yml和elasticsearch-c/config/elasticsearch.yml,在这些文件中找到http.port配置项,并将其分别修改为9211、9212、9213。这样可以确保每个服务使用不同的端口进行通信。 2. 复制单机示例:将已经搭建完成的单机版本目录拷贝到集群目录下,并重新命名。例如,如果单机版本目录为/home/soft/elasticsearch,则可以使用以下命令分别复制出elasticsearch-a、elasticsearch-b、elasticsearch-c三个目录: ``` cp elasticsearch-7.8.0/ /home/soft/es-cluster/elasticsearch-a -r cp elasticsearch-7.8.0/ /home/soft/es-cluster/elasticsearch-b -r cp elasticsearch-7.8.0/ /home/soft/es-cluster/elasticsearch-c -r ``` 这样可以在集群目录下创建三个独立的服务实例,每个实例对应一个节点。 3. 启动单机服务:首先,分别启动elasticsearch-a、elasticsearch-b、elasticsearch-c三个es服务。在启动之前,确保每个服务的配置文件已经正确编辑并保存。使用以下命令启动每个服务: ``` ./elasticsearch-a/bin/elasticsearch ./elasticsearch-b/bin/elasticsearch ./elasticsearch-c/bin/elasticsearch ``` 通过这些命令,可以依次启动每个服务,并且在启动成功后,它们将开始组成一个集群。 请注意,以上步骤是基于Elasticsearch 7.8版本进行的。如果使用其他版本,可能会有些许差异,请参考相应版本的官方文档进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux搭建elasticsearch-7.8.0集群](https://blog.csdn.net/zhuocailing3390/article/details/126082384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值