使用Spark中的复杂过滤从elasticsearch中获取esJsonRDD

时间:2017-06-13 16:48:56

标签: scala apache-spark elasticsearch spark-dataframe rdd

我目前正在基于单行弹性查询的elasticsearch过滤中提取Spark Job RDD(例如):

val elasticRdds = sparkContext.esJsonRDD(esIndex, s"?default_operator=AND&q=director.name:DAVID + \n movie.name:SEVEN")

现在,如果我们的搜索查询变得如此复杂:

{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "default_operator": "AND",
                    "query": "director.name:DAVID + \n movie.name:SEVEN"
                }
            },
            "filter": {
                "nested": {
                    "path": "movieStatus.boxoffice.status",
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "match": {
                                        "movieStatus.boxoffice.status.rating": "A"
                                    }
                                },
                                {
                                    "match": {
                                        "movieStatus.boxoffice.status.oscar": "false"
                                    }
                                }
                            ]
                        }
                    }
                }
           }
        }
    }
}

我仍然可以将该查询转换为内联弹性查询,以便将其与 esJsonRDD 一起使用吗?或者,无论如何,上面的查询仍然可以 esJsonRDD ? 如果没有,在Spark中获取此类RDD的更好方法是什么?

因为esJsonRDD似乎只接受内联(一行)弹性查询。

1 个答案:

答案 0 :(得分:2)

使用三重引号:

val query = """{
"query": {
    "filtered": {
        "query": {
            "query_string": {
                "default_operator": "AND",
                "query": "director.name:DAVID + \n movie.name:SEVEN"
            }
        },
        "filter": {
            "nested": {
                "path": "movieStatus.boxoffice.status",
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "movieStatus.boxoffice.status.rating": "A"
                                }
                            },
                            {
                                "match": {
                                    "movieStatus.boxoffice.status.oscar": "false"
                                }
                            }
                        ]
                    }
                }
            }
        }
     }
  }
}"""

val elasticRdds = sparkContext.esJsonRDD(esIndex, query)