3

我正在编写一个服务,它接受一个案例类并将其序列化为 json,然后我将其发送到运行 Elastic Search 的实例。

我希望 scalacheck 生成几个带有随机缺失数据的案例类,如下所示:

val searchDescAndBrand = SearchEntry("", "Ac Adapters", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchBrand = SearchEntry("", ", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchPartNumberAndBrand = SearchEntry("02DUYT", "", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)

你明白了,要么填写值,要么将它们留空(最后一个是Long类型。

这是容易的部分,问题是生成的json不只是省略“归档”,而是省略了整个部分,例如:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

如果我有一个带有数据的前 3 个字段的案例类,则 json 将是:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "part_number" : {
  |            "query" : "02D875",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

所以,简而言之,拥有价值意味着增加

{"match" : {
  |          "<specific name here, based on which value we have>" : {
  |            "query" : "<value from scalacheck>",
  |            "type" : "phrase"
  |          }
  |        }}

到结果。

您将如何处理这样的用例?

4

0 回答 0