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"
}
}
}
}
}
}
}
}
在同一个索引库中关联查询
于 2024-08-06 12:57:26 首次发布