篩選器語法

本頁說明篩選帳戶時必須使用的語法。

語法

整數以外的所有值都必須以雙引號 (") 括住。如要瞭解特定欄位接受哪些值,請參閱該欄位的參考文件

您可以使用 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*" 會傳回含有 foo 的所有帳戶 accountName,例如「storeFoo」。

如要篩選不含特定序列的值,請使用 !=*。舉例來說,accountName != "*foo*" 會傳回所有不含 foo 的帳戶。accountName

系統會忽略多餘的空格。舉例來說,foo AND bar 等於 foo AND bar

以下列舉幾個使用 account.list 方法篩選帳戶的範例:

「* All MCA sub-accounts containing "Store"

accountName = "*store*" AND relationship(service(type = "ACCOUNT_AGGREGATION"))
  • 由供應商 123456 管理的所有帳戶
relationship(service(type = "ACCOUNT_MANAGEMENT") AND providerId = 123456)
  • 所有已傳送邀請給供應商 123456,或需要接受該供應商邀請的帳戶
relationship(service(handshakeState = "PENDING" AND type ="ACCOUNT_MANAGEMENT")
AND providerId = 123456)

如要篩選提出要求的使用者可存取的帳戶,請使用 google.shopping.merchant.accounts.v1.ListAccountsRequest 方法,如下列範例所示。

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.Account;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListAccountsPagedResponse;
import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1.ListAccountsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to filter the accounts the user making the request has access to. */
public class FilterAccountsSample {

  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(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);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();

    filterAccounts(config);
  }
}

規格

篩選器遵循 AIP 篩選器規格的子集,以及正式 EBNF 文法

filter
    : accountFilterDisj
    | accountFilterConj

accountFilterDisj
    : "(" accountFilterConj " OR " accountFilterConj ")"
    ;
accountFilterConj
    : accountFilter {" AND " accountFilter}
    ;

accountFilter
    : accountNameFilter | capabilityFilter | relationshipFn
    ;

accountNameFilter
    : "accountName" comparator value
    ;

capabilityFilter
    : "capabilities:" capabilityValue
    | "-capabilities:" capabilityValue
    | "NOT capabilities:" capabilityValue
    ;
capabilityValue
    : "CAN_UPLOAD_PRODUCTS"
    ;

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 = " handshakeState
    | "type = " serviceType
    ;

handshakeState
    : "PENDING"
    | "APPROVED"
    | "REJECTED"
    ;
serviceType
    : "ACCOUNT_AGGREGATION"
    | "ACCOUNT_MANAGEMENT"
    ;

comparator
    : " = " | " != "
    ;