Elastic4s - finding multiple exact values for one term

元气小坏坏 提交于 2020-01-16 04:10:01

问题


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

relaying on the ES https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html

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

I tried this:

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

But got an error from ES.

How can I do that?


回答1:


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.

So this will work:

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

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



来源:https://stackoverflow.com/questions/30735854/elastic4s-finding-multiple-exact-values-for-one-term

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!