本文介绍了Elastic4s - 找到一个术语的多个确切值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤一个术语以匹配数组中的一个值。

I'm trying to filter a term to be matching one of the values in an array.

在ES上传递

 GET /my_store/products/_search
            {
                "query" : {
                    "filtered" : {
                        "filter" : {
                            "terms" : { 
                                "price" : [20, 30]
                            }
                        }
                    }
                }
            }

我尝试过:

    val res =  ESclient.execute {
        search in "index" query {
          filteredQuery query {
            matchall
          } filter {
                   termsFilter("category", Array(1,2))
          }
        }

但是从ES发送错误。

我该怎么做?

推荐答案

当调用termsFilter时,该方法期待一个var args调用 Any * ,所以 termsFilter(类别,1,2)将工作。但是 termsFilter(category,Array(1,2))被视为单个参数,因为Array是当然的子类。通过添加:_ * 我们强制scala将其视为一个vars arg调用。

When calling termsFilter, the method is expecting a var args invocation of Any*, so termsFilter("category", 1, 2) would work. But termsFilter("category", Array(1,2)) is treated as a single argument, since Array is a subclass of Any of course. By adding : _ * we force scala to see it as a vars arg invocation.

所以这将工作:

val res =  ESclient.execute {
  search in "index" query {
    filteredQuery query {
      matchall
   } filter {
        termsFilter("category", Array(1,2) : _ *)
   }
}

也许最好的解决方案是更新客户端在Iterables上重载。

Maybe the best solution of all is to update the client to be overloaded on Iterables.

这篇关于Elastic4s - 找到一个术语的多个确切值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:20