You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by du...@apache.org on 2018/07/05 16:02:47 UTC

[incubator-openwhisk] branch master updated: Allow Elasticsearch 'must' query to take an array of 'ranges' (#3795)

This is an automated email from the ASF dual-hosted git repository.

dubeejw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new eb05298  Allow Elasticsearch 'must' query to take an array of 'ranges' (#3795)
eb05298 is described below

commit eb05298129bbde4b8b48f7eb8a61ae96a6e87e3f
Author: James Dubee <jw...@us.ibm.com>
AuthorDate: Thu Jul 5 12:02:44 2018 -0400

    Allow Elasticsearch 'must' query to take an array of 'ranges' (#3795)
    
    * Allow Elasticsearch 'must' query to take an array of 'ranges'
---
 .../core/containerpool/logging/ElasticSearchRestClient.scala  |  6 ++++--
 .../containerpool/logging/ElasticSearchRestClientTests.scala  | 11 ++++++-----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/core/containerpool/logging/ElasticSearchRestClient.scala b/common/scala/src/main/scala/whisk/core/containerpool/logging/ElasticSearchRestClient.scala
index 60a9b49..750728e 100644
--- a/common/scala/src/main/scala/whisk/core/containerpool/logging/ElasticSearchRestClient.scala
+++ b/common/scala/src/main/scala/whisk/core/containerpool/logging/ElasticSearchRestClient.scala
@@ -59,7 +59,8 @@ case class EsQueryBoolMatch(key: String, value: String)
 case class EsQueryOrder(field: String, kind: EsOrder)
 case class EsQuerySize(size: Integer)
 case class EsQueryAll() extends EsQueryMethod
-case class EsQueryMust(matches: Vector[EsQueryBoolMatch], range: Option[EsQueryRange] = None) extends EsQueryMethod
+case class EsQueryMust(matches: Vector[EsQueryBoolMatch], range: Vector[EsQueryRange] = Vector.empty)
+    extends EsQueryMethod
 case class EsQueryMatch(field: String, value: String, matchType: Option[EsMatch] = None) extends EsQueryMethod
 case class EsQueryTerm(key: String, value: String) extends EsQueryMethod
 case class EsQueryString(queryString: String) extends EsQueryMethod
@@ -108,7 +109,8 @@ object ElasticSearchJsonProtocol extends DefaultJsonProtocol {
   implicit object EsQueryMustJsonFormat extends RootJsonFormat[EsQueryMust] {
     def read(query: JsValue) = ???
     def write(query: EsQueryMust) = {
-      val boolQuery = Map("must" -> query.matches.toJson) ++ query.range.map(r => "filter" -> r.toJson)
+      val boolQuery = Map("must" -> query.matches.toJson) ++ Map("filter" -> query.range.toJson)
+        .filter(_._2 != JsArray.empty)
       JsObject("bool" -> boolQuery.toJson)
     }
   }
diff --git a/tests/src/test/scala/whisk/core/containerpool/logging/ElasticSearchRestClientTests.scala b/tests/src/test/scala/whisk/core/containerpool/logging/ElasticSearchRestClientTests.scala
index 9617ba9..a6dd318 100644
--- a/tests/src/test/scala/whisk/core/containerpool/logging/ElasticSearchRestClientTests.scala
+++ b/tests/src/test/scala/whisk/core/containerpool/logging/ElasticSearchRestClientTests.scala
@@ -92,9 +92,10 @@ class ElasticSearchRestClientTests
     // Test must with ranges
     Seq((EsRangeGte, "gte"), (EsRangeGt, "gt"), (EsRangeLte, "lte"), (EsRangeLt, "lt")).foreach {
       case (rangeArg, rangeValue) =>
-        val queryRange = EsQueryRange("someKey", rangeArg, "someValue")
+        val queryRange1 = EsQueryRange("someKey1", rangeArg, "someValue1")
+        val queryRange2 = EsQueryRange("someKey2", rangeArg, "someValue2")
         val queryTerms = Vector(EsQueryBoolMatch("someKey1", "someValue1"), EsQueryBoolMatch("someKey2", "someValue2"))
-        val queryMust = EsQueryMust(queryTerms, Some(queryRange))
+        val queryMust = EsQueryMust(queryTerms, Vector(queryRange1, queryRange2))
 
         EsQuery(queryMust).toJson shouldBe JsObject(
           "query" ->
@@ -106,9 +107,9 @@ class ElasticSearchRestClientTests
                       JsObject("match" -> JsObject("someKey1" -> JsString("someValue1"))),
                       JsObject("match" -> JsObject("someKey2" -> JsString("someValue2")))),
                   "filter" ->
-                    JsObject("range" ->
-                      JsObject("someKey" ->
-                        JsObject(rangeValue -> "someValue".toJson))))))
+                    JsArray(
+                      JsObject("range" -> JsObject("someKey1" -> JsObject(rangeValue -> "someValue1".toJson))),
+                      JsObject("range" -> JsObject("someKey2" -> JsObject(rangeValue -> "someValue2".toJson)))))))
     }
   }