过滤器语法

本页介绍了您必须使用的账号过滤条件语法。

语法

除了整数之外,所有值都必须用英文双引号 (") 括起来。如需了解特定字段接受哪些值,请参阅该字段的参考文档

您可以使用 AND 在同一查询中过滤多个字段。您还可以使用 AND 组合多个 relationship(...)service(...) 过滤条件。下面的示例结合使用了多个 relationship(...)service(...) 过滤器:

(relationship(service(type = "ACCOUNT_MANAGEMENT") AND service(handshakeState = "PENDING"))) OR (accountName = "store" AND relationship(...))

此示例会返回以下账号:

  • 与其他账号存在账号管理关系且存在待接受的其他关系的所有账号。

  • 显示名称为 "store" 且与其他账号有关系的所有账号。

您无法使用 AND 在同一字段中过滤多个值。例如,您不能使用 accountName = "*A*" AND accountName = "*B*"

您可以在同一查询中使用 OR 过滤两个字段。使用圆括号括住 OR 运算符两侧的过滤条件。例如 (accountName = "storeA") OR (accountName = "storeB")

您只能使用 OR 来组合两个字段。例如,您不能使用 (accountName = "storeA") OR (accountName = "storeB") OR (accountName = "storeC")

除了 ANDOR 运算符以及函数调用(例如 relationship(...)service(...))之外,不允许使用圆括号。

对于 accountNameaccountIdAlias 等字符串字段,您可以通过将字符串用星号 (*) 括起来,过滤出包含特定字词或字符序列的值。例如,accountName = "*foo*" 会返回 accountName 包含 foo 的所有账号,例如“storeFoo”。

您可以使用 !=* 过滤不包含特定序列的值。例如,accountName != "*foo*" 会返回 accountName 不包含 foo 的所有账号。

系统会忽略多余的空格。例如,foo AND barfoo AND bar 相同。

如需过滤发出请求的用户有权访问的账号,您可以使用 google.shopping.merchant.accounts.v1beta.ListAccountsRequest 方法,如以下示例所示。

Java

  public static void filterAccounts(Config config) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    AccountsServiceSettings accountsServiceSettings =
        AccountsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Calls the API and catches and prints any network failures/errors.
    try (AccountsServiceClient accountsServiceClient =
        AccountsServiceClient.create(accountsServiceSettings)) {

      // Filter for accounts with display names containing "store" and a provider with the ID "123":
      String filter = "accountName = \"*store*\" AND relationship(providerId = 123)";

      // Filter for all subaccounts of account "123":
      // String filter2 = "relationship(callerHasAccessToProvider() AND providerId = 123 AND
      // service(type = \"ACCOUNT_AGGREGATION\"))";

      // String filter3 = "relationship(service(handshakeState = \"APPROVED\" AND type =
      // \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";

      ListAccountsRequest request = ListAccountsRequest.newBuilder().setFilter(filter).build();

      System.out.println("Sending list accounts request with filter:");
      ListAccountsPagedResponse response = accountsServiceClient.listAccounts(request);

      int count = 0;

      // Iterates over all rows in all pages and prints the sub-account
      // in each row.
      // `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
      // request to fetch all pages of data.
      for (Account account : response.iterateAll()) {
        System.out.println(account);
        count++;
      }
      System.out.print("The following count of elements were returned: ");
      System.out.println(count);
    } catch (Exception e) {
      System.out.println(e);
    }
  }

规范

这些过滤器遵循 AIP 过滤器规范的一部分及其正式 EBNF 语法

filter
    : accountFilterDisj
    | accountFilterConj
    ;
accountFilterDisj
    : "(" accountFilterConj " OR " accountFilterConj ")"
    ;
accountFilterConj
    : accountFilter {" AND " accountFilter}
    ;
accountFilter
    : displayNameFilter | relationshipFn
    ;
displayNameFilter
    : "displayName" comparator value
    ;
relationshipFn
    : "relationship(" relationshipConj ")"
    ;
relationshipConj
    : relationshipFilter {" AND " relationshipFilter}
    ;
relationshipFilter
    : "providerId = " numValue
    | "accountIdAlias" comparator value
    | serviceFn
    ;
serviceFn
    : "service(" serviceConj ")"
    ;
serviceConj
    : serviceFilter {" AND " serviceFilter}
    ;
serviceFilter
    : "externalAccountId" comparator value
    | "handshakeState = " value
    | "type = " value
    ;
comparator
    : " = " | " != "
    ;