因此,我使用以下映射设置了索引:
PUT test_index
{
"mappings": {
"doc": {
"properties": {
"title": {
"type": "text"
},
"author": {
"type": "text"
},
"reader_stats": {
"type": "join",
"relations": {
"book": "reader"
}
}
}
}
}
}
每个父文档代表一本书,其子文档代表该书的读者。但是,如果我要跑步:
GET test_index/_search
{
"query":{"match_all":{}}
}
结果将像这样被书籍和读者填充:
"hits" : [
{
"_index" : "test_index",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "my second book",
"author" : "mr author",
"reader_stats" : {
"name" : "book"
}
}
},
{
"_index" : "test_index",
"_type" : "doc",
"_id" : "7",
"_score" : 1.0,
"_routing" : "2",
"_source" : {
"name" : "michael bookworm",
"clicks" : 1,
"reader_stats" : {
"name" : "reader",
"parent" : 2
}
}
}
]
有什么方法可以排除读者文档而仅显示书本?我已经在我的应用程序中使用match_all来抓书,因此,如果我可以避免不得不更改该查询,那将是很好的选择,但我想那是不可能的。
对于映射如何与联接字段一起使用,我也有些困惑,因为没有定义子文档需要哪些字段。例如,在我的映射中,没有地方指定“阅读器”文档必须具有“名称”和“点击”字段。这是正确的吗?
答案 0 :(得分:1)
您需要在查询中使用has_child(仅搜索父文档)和has_parent(仅搜索子文档)关键字。
有什么办法可以排除读者文档而只显示书本?
您的查询将是:
GET test_index/_search
{
"query": {
"has_child": {
"type": "reader",
"query": {
"match_all": {}
}
}
}
}
有关更多详细信息,您可以在这里查看: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html