LoginSignup
2
1

More than 5 years have passed since last update.

Magento2のREST APIの検索条件の書き方

Last updated at Posted at 2018-05-24

はじめに

初投稿です。
備忘録の為にブログを書こうかと思いました!

初投稿はREST APIについてです!
インターン先ではMagento2というECサイトのパッケージを扱っていて、よくREST APIが使われるのでここにまとめようかなーと思いました。
参考になりそうなサイトはほとんど英語で読むのだるいしね

検索条件の書き方

書き方はこんな感じです
ここのサイトから引用させていただきました

searchCriteria[filter_groups][<index>][filters][<index>][field]=<field_name>
searchCriteria[filter_groups][<index>][filters][<index>][value]=<search_value>
searchCriteria[filter_groups][<index>][filters][<index>][condition_type]=<operator>

私はじめにこれを見たときはわけがわかりませんでしたが使っていくうちになんとなく理解し始めました。
それでは一つづつ解説していきます。

  • searchCriteria  ... あんまり気にしなくていい 検索条件という意味
  • filter_groups  ... AND検索 後述します
  • index      ... 数字が入る 後ほど例を使って説明する
  • filters    ... OR検索 後述します
  • search_value  ...検索したい値 数字でも文字列でもいい
  • field_name    ... 検索したいフィールド 例えばpriceとかqtyとかskuとか
  • condition_type ...search_valueに対してどんな値なのか 後述します    

condition_typeについて

condition_type 意味
eq valueと値が等しい
finset 何かしらのvalueがセットされているか
from 範囲検索 toと一緒に使う
gt valueより大きい
gteq valueと同じか大きい
in わからん
like 曖昧検索
lt valueより小さい
lteq valueと同じか小さい
moreq valueと同じか大きい
neq valueと値が等しくない
nin わからん
notnull valueがnullじゃない
null valueがnull
to 範囲検索 fromと一緒に使う

使用例

今回はCUIでやることを想定してcurlを使用するので、事前にお使いのマシンに入れておいてください。

はじめに

適当なプロダクトをMagentoのGUIから登録してください

【商品一覧】

curl --globoff "http://localhost/index.php/rest/V1/products?&searchCriteria=0"

注)--globoffはプログラムで実行する場合はつけなくていいです。

登録しているすべての商品の情報を出力します。
取れる値はこちらです
《取れる値》

    id               = 商品のID
    sku              = skuの名称
    name             = 商品の名前
    attribute_set_id = 属性のID
    price            = 商品の値段
    status           = 商品があるかないか 0の場合はない 1の場合はある
    visibility       = 可視化されているかどうか 多分
    type_id          = コンフィグ商品かシンプル商品か
    created_at       = 商品を作成した日時
    updated_at       = 商品をアップデートした日時
    product_links    = 商品のリンク
    tier_prices      = 値段に関する何かしら

    custom_attributes          
        meta_title             = わからん
        meta_keyword           = わからん
        meta_description       = 商品の説明文 多分
        image                  = 商品の画像
        small_image            = 商品の画像
        thumbnail              = 商品の画像
        category_ids           = カテゴリーのID
        options_container      = オプションが含まれるか
        required_options       = 必要なオプションは何か
        has_options            = オプションを持っているか
        url_key                = urlのキー
        gift_message_available = ギフトメッセージが使えるかどうか
        swatch_image           = 見本の画像
        tax_class_id           = 税の何かしら

【カテゴリー検索】

ここから条件式が出てきます。

curl --globoff "http://localhost/index.php/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=カテゴリーID&searchCriteria[filterGroups][0][filters][0][condition_type]=eq"

これを見やすくするとこうなります。

curl --globoff "http://localhost/index.php/rest/V1/products?
searchCriteria[filterGroups][0][filters][0][field]=category_id&
searchCriteria[filterGroups][0][filters][0][value]=カテゴリーID&
searchCriteria[filterGroups][0][filters][0][condition_type]=eq"

〔条件式の書き方〕
商品一覧を取得するAPI

curl --globoff "http://localhost/index.php/rest/V1/products?&searchCriteria=0"

を叩き、出力結果の一つである{"attribute_code":"category_ids","value":["番号"]},を確認します。
searchCriteriaのfieldをcategory_idにして(category_idsにするとうまく動きません)、valueを"番号"にします。
condition_typeはeq(equal イコール)です。

【価格n円~m円の範囲検索】

curl --globoff "http://localhost/index.php/rest/V1/products?&searchCriteria[filterGroups][0][filters][0][field]=price&searchCriteria[filterGroups][0][filters][0][value]=100&searchCriteria[filterGroups][0][filters][0][condition_type]=from&searchCriteria[filterGroups][1][filters][0][field]=price&searchCriteria[filterGroups][1][filters][0][value]=200&searchCriteria[filterGroups][1][filters][0][condition_type]=to"

見やすくするとこうなります。

curl --globoff "http://localhost/index.php/rest/V1/products?&
searchCriteria[filterGroups][0][filters][0][field]=price&
searchCriteria[filterGroups][0][filters][0][value]=開始点&
searchCriteria[filterGroups][0][filters][0][condition_type]=from&
searchCriteria[filterGroups][1][filters][0][field]=price&
searchCriteria[filterGroups][1][filters][0][value]=終点&
searchCriteria[filterGroups][1][filters][0][condition_type]=to"

〔条件式の書き方〕
ここでは価格の範囲の開始点と終点を定義するため、二つの条件式を組み合わせます。
商品一覧を取得するAPI

curl --globoff "http://localhost/index.php/rest/V1/products?&searchCriteria=0"

を叩き、出力結果の一つである"price":"価格"を確認します。
まずは範囲の開始点を定義します。searchCriteriaの[field]をpriceに、[value]を設定したい価格の開始点に、
[condition_type]をfromに設定します。
次に範囲の終点を定義します。[filterGroups]を[1]にしてください。[0]のままだと開始点の定義を上書きしてしまいます。
[field]をpriceに、[value]を設定したい終点の価格に、[condition_type]をtoにしてください。

AND検索とOR検索

searchCriteria[filter_groups][<index>][filters][<index>][field]=<field_name>
searchCriteria[filter_groups][<index>][filters][<index>][value]=<search_value>
searchCriteria[filter_groups][<index>][filters][<index>][condition_type]=<operator>

AND検索の場合は[filter_groups]の[index]の番号を変えていきます。
上記の使用例は全てAND検索です。つまり指定した条件に完全一致するものが出力されます。

一方でOR検索をしたい場合は[filters]の[index]の番号を変えていきます。
例えば値段が10000円以下または15000円以上の商品を検索したい場合は

curl --globoff "http://localhost/index.php/rest/V1/products?&
searchCriteria[filterGroups][0][filters][0][field]=price&
searchCriteria[filterGroups][0][filters][0][value]=10000&
searchCriteria[filterGroups][0][filters][0][condition_type]=lteq&
searchCriteria[filterGroups][0][filters][1][field]=price&
searchCriteria[filterGroups][0][filters][1][value]=15000&
searchCriteria[filterGroups][0][filters][1][condition_type]=gteq"

のように検索します。

以上です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1