Elasticsearch(九)数据关联

Elasticsearch: 6.4.2

1. 索引树形结构

创建简单映射

PUT /categories
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path_analyzer": {
          "tokenizer": "path_hierarchy"
        }
      }
    }
  },
  "mappings": {
    "category": {
      "properties": {
        "content": {
          "type": "text",
          "fields": {
            "path": {
              "analyzer": "path_analyzer",
              "type": "text",
              "store": true
            }
          }
        }
      }
    }
  }
}

测试

GET /categories/_analyze
{
  "field": "content.path",
  "text": "/home/ubuntu/work"
}
# 输出:/home、/home/ubuntu、/home/ubuntu/work
2. 嵌套对象

创建映射

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}

索引数据

PUT my_index/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

搜索

GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "white" }} 
          ]
        }
      }
    }
  }
}
3. 父子关系
  • 6.0之后父子结构使用join类型来定义,关系的映射使用relations字段来指定。
  • 一个索引中只能有一个join类型字段。
  • 建好索引之后, join字段中的relations集合,可以增加映射、或者给原有的映射添加child,但是不能删除原有的映射。
  • 父文档和子文档存在同一个分片上,因此查询、更新、删除子文档时应该使用routing参数。

创建映射

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "my_join_field": { 
          "type": "join",
          "relations": {
            "question": "answer" 
          }
        }
      }
    }
  }
}

创建父文档

PUT my_index/_doc/1?refresh
{
  "text": "This is a question",
  "my_join_field": "question" 
}

创建子文档

PUT my_index/_doc/3?routing=1&refresh 
{
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}

根据parent_id查询属于某一特定父文档的子文档

GET /my_index/_search
{
  "query": {
    "parent_id": {
      "type": "answer",
      "id": "1"
    }
  }
}

基于父文档查找子文档

GET my_index/_search
{
    "query": {
        "has_parent" : {
            "parent_type" : "question",
            "query" : {
                "match" : {
                    "text" : "This is"
                }
            }
        }
    }
}

基于子文档查找父文档

GET my_index/_search
{
"query": {
        "has_child" : {
            "type" : "answer",
            "query" : {
                "match" : {
                    "text" : "This is question"
                }
            }
        }
    }
}

Parent-join查询和聚合

  • 查询父文档id=1,子文档类型为answer的子文档;
  • 基于父文档类型question进行聚合;
  • 基于指定的field处理。

如果一个文档是子文档,则my_join_field#question包含的是该子文档指向的父文档的id值;如果是父文档,则是该文档的id值。

GET my_index/_search
{
  "query": {
    "parent_id": { 
      "type": "answer",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "my_join_field#question", 
        "size": 10
      }
    }
  },
  "script_fields": {
    "parent": {
      "script": {
         "source": "doc['my_join_field#question']" 
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值