在同一个索引库中关联查询

delete my_index1

PUT my_index1
{
  "mappings": {
      "properties": {
        "text":{"type": "keyword"},
        "my_join_field": { 
          "type": "join",
          "relations": {
            "question": "answer" ,
            "answer": "vote" 
          }
        }
      }
    }
}
 PUT my_index1/_doc/1?refresh
{
  "text": "我是第一个问题",
  "my_join_field": {
    "name": "question" 
  }
}

PUT my_index1/_doc/2?refresh
{
  "text": "我是第二个问题",
  "my_join_field": {
    "name": "question"
  }
}

#子表建立关联 共用同一个索引库索引id, 通过name(answer)区分 ,以及parent id,向量值
PUT my_index1/_doc/3?routing=1&refresh 
{
  "text": "问题一的答案1",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}
#子表建立关联

PUT my_index1/_doc/4?routing=1&refresh
{
  "text": "问题一的答案2",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}


PUT my_index1/_doc/5?routing=1&refresh 
{
  "text": "This is a vote",
  "my_join_field": {
    "name": "vote",
    "parent": "3" 
  }
}

#通过子表查父表(通过其中一个答案查提问)
 
GET my_index1/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "term": {
          "text": {
            "value": "问题一的答案1"
          }
        }
      }
    }
  }
}



#通过父表查子表(通过一个提问查多个答案)
GET my_index1/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "query": {
        "term": {
          "text": {
            "value": "我是第一个问题"
          }
        }
      }
    }
  }
}

#通过vote 查提问(先查答案,在查提问)

GET my_index1/_search
{
  "query": {
     "has_child": {
      "type": "answer",
      "query": {
        "has_child": {
        "type": "vote",
        "query": {
          "term": {
            "text": {
              "value": "This is a vote"
            }
          }
        }
      }
      }
    }
    
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值