我正在尝试使用 elasticsearch-spark
连接器查询 elasticsearch
,我只想返回几个结果:
例如:
val conf = new SparkConf().set("es.nodes","localhost").set("es.index.auto.create", "true").setMaster("local")
val sparkContext = new SparkContext(conf)
val query = "{\"size\":1}"
println(sparkContext.esRDD("index_name/type", query).count())
但是这将返回索引中的所有文档。
最佳答案
有些参数实际上是在设计上从查询中忽略的,例如:from
、size
、fields
等。
它们在 elasticsearch-spark
连接器内部使用。
不幸的是,这个不受支持的参数列表没有记录在案。但是,如果您希望使用 size
参数,您始终可以依赖 pushdown
谓词并使用 DataFrame
/Dataset
limit
方法。
因此,您应该改用 Spark SQL DSL,例如:
val df = sqlContext.read.format("org.elasticsearch.spark.sql")
.option("pushdown","true")
.load("index_name/doc_type")
.limit(10) // instead of size : 10
此查询将返回连接器默认使用的 match_all
查询返回的前 10 个文档。
注意:以下内容在任何层面上都不正确。
This is actually on purpose. Since the connector does a parallel query, it also looks at the number of documents being returned so if the user specifies a parameter, it will overwrite it according to the es.scroll.limit setting (see the configuration option).
当您查询 elasticsearch 时,它还会在所有索引分片上并行运行查询,而不会覆盖它们。
关于scala - elasticsearch-spark 连接器大小限制参数在查询中被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968344/