php - Elasticsearch-从具有连接字段的文档中排除子级

标签 php elasticsearch join relationship

因此,我使用以下映射设置了索引:

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来抓书,因此,如果可以避免不得不更改该查询,那将是很好的选择,但我想那是不可能的。

对于映射如何与联接字段一起使用,我也有些困惑,因为没有定义子文档需要哪些字段。例如,在我的映射中,没有地方指定“阅读器”文档必须具有“名称”和“点击”字段。它是否正确?

最佳答案

您需要在查询中使用has_child(仅搜索父文档)和has_parent(仅搜索子文档)关键字。

Is there some way I can exclude reader documents and only show books?



  • 您的查询将是:
    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

    关于php - Elasticsearch-从具有连接字段的文档中排除子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56552439/

    相关文章:

    elasticsearch - Elasticsearch 中嵌套数组的范围过滤器

    sql - oracle查询根据条件删除重复行

    mysql - 插入从内部联接查询派生的数据时出错

    php - 包含文件时 POST 数组为空

    php - 如何知道要在您的网站中更改哪些文件

    elasticsearch - 是否可以在父聚合定义的字段上执行Elasticsearch嵌套统计聚合?

    python - Geopandas sjoin()错误: list index out of range

    php - 我可以使用系统函数名称(在类内)定义自己的函数吗?

    php - CakePHP 使用 %like% 查找查询

    elasticsearch - Elasticsearch 按 bool 字段排序