alpakka-elasticsearch客户端进行弹性搜索多查询

时间:2019-06-18 07:52:48

标签: scala elasticsearch alpakka

我是scala的新手,它使用alpakka-elasticsearch客户端通过弹性搜索执行查询。

单一搜索查询可以通过以下两种内部调用弹性搜索的_search url的方式正常工作:

val writeCustomIndex = ElasticsearchSource
 .typed[Book](
  indexName = "source",
  typeName = "_doc",
  query = """{"match_all": {}}"""
 )

OR

val readWithSearchParameters = ElasticsearchSource
.typed[TestDoc](
  indexName,
  Some(typeName),
  searchParams = Map(
  "query" -> """ {"match_all": {}} """,
  "_source" -> """ ["id", "a", "c"] """
  )

我要寻找的是对此客户端执行以下多次查询(_msearch)。

url - http://localhost:9200/index1/_msearch?
request :
[
 {"query" : {"match_all" : {}}, "from" : 0, "size" : 1},
 {"index" : "index2"},
 {"query" : {"match_all" : {}}, "from" : 0, "size" : 2}
]

弹性客户来源:https://doc.akka.io/docs/alpakka/current/elasticsearch.html

1 个答案:

答案 0 :(得分:0)

根据建议,我尝试了一种方法,其中我合并了两个来源。

希望,这可能会对某人有所帮助!

 var elasticRecord: Future[Seq[ElasticRecord]] = ElasticsearchSource
  .typed[ElasticRecord](
  indexName1,
  Some(typeName1),
  query1,
  settings = ElasticsearchSourceSettings()).map { message =>
  message.source
}.merge(ElasticsearchSource
  .typed[ElasticRecord](
  indexName2,
  Some(typeName2),
  query1,
  settings = ElasticsearchSourceSettings()).map { message =>
  message.source
}).runWith(Sink.seq)