> ## Documentation Index
> Fetch the complete documentation index at: https://generaltranslation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 統合ガイド

> Search Posts エンドポイントを統合するための重要な概念とベストプラクティス

このガイドでは、Search Posts エンドポイントをアプリケーションに統合するために必要となる重要な概念について説明します。

***

<div id="authentication">
  ## 認証
</div>

<div id="recent-search">
  ### 最近の検索
</div>

最近の検索エンドポイントでは、複数の認証方法をサポートしています。

| Method                                                                                                                            | 利用ケース     |
| :-------------------------------------------------------------------------------------------------------------------------------- | :-------- |
| [OAuth 2.0 App-Only](/ja/resources/fundamentals/authentication#oauth-2-0)                                                         | 公開ポストのデータ |
| [OAuth 2.0 Authorization Code with PKCE](/ja/resources/fundamentals/authentication#oauth-2-0-authorization-code-flow-with-pkce-2) | 非公開のメトリクス |
| [OAuth 1.0a User Context](/ja/resources/fundamentals/authentication)                                                              | 非公開のメトリクス |

<div id="full-archive-search">
  ### フルアーカイブ検索
</div>

フルアーカイブ検索は、[OAuth 2.0 App-Only](/ja/resources/fundamentals/authentication#oauth-2-0) 認証にのみ対応しています。

<Warning>
  プライベートメトリクス (`non_public_metrics`、`organic_metrics`、`promoted_metrics`) は、App-Only 認証のみがサポートされているため、フルアーカイブ検索では利用できません。
</Warning>

***

<div id="building-queries">
  ## クエリの構築
</div>

クエリではオペレーターを使って投稿をマッチさせます。これらのオペレーターをブール論理で組み合わせます。

```
(AI OR "machine learning") lang:en -is:retweet has:links
```

<div id="query-length-limits">
  ### クエリ長の上限
</div>

| アクセスレベル    | 直近検索    | 全アーカイブ検索 |
| :--------- | :------ | :------- |
| Self-serve | 512文字   | 1,024文字  |
| Enterprise | 4,096文字 | 4,096文字  |

<div id="operator-types">
  ### オペレーターの種類
</div>

| Type                     | 説明                                 | 例                         |
| :----------------------- | :--------------------------------- | :------------------------ |
| **Standalone**           | 単体で使用できる                           | `#python`, `from:user`    |
| **Conjunction-required** | Standalone のオペレーターと組み合わせて使用する必要がある | `has:media`, `is:retweet` |

<Card title="クエリを作成する" icon="magnifying-glass" href="/ja/x-api/posts/search/integrate/build-a-query">
  クエリ構文について詳しく学ぶ
</Card>

<Card title="オペレーターリファレンス" icon="list-check" href="/ja/x-api/posts/search/integrate/operators">
  利用可能なすべてのオペレーターを確認する
</Card>

***

<div id="fields-and-expansions">
  ## フィールドとexpansions
</div>

デフォルトでは、レスポンスには `id`、`text`、`edit_history_tweet_ids` の3つだけが含まれます。追加のデータを取得するには、パラメータを指定してください。

<div id="example-request">
  ### リクエスト例
</div>

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl "https://api.x.com/2/tweets/search/recent?\
  query=python&\
  tweet.fields=created_at,public_metrics,author_id&\
  expansions=author_id,attachments.media_keys&\
  user.fields=username,verified&\
  media.fields=url,type" \
    -H "Authorization: Bearer $BEARER_TOKEN"
  ```

  ```python Python SDK theme={null}
  from xdk import Client

  client = Client(bearer_token="YOUR_BEARER_TOKEN")

  # 最近の投稿を検索
  for page in client.posts.search_recent(
      query="python",
      tweet_fields=["created_at", "public_metrics", "author_id"],
      expansions=["author_id", "attachments.media_keys"],
      user_fields=["username", "verified"],
      media_fields=["url", "type"],
      max_results=100
  ):
      for post in page.data:
          print(f"{post.text} - Likes: {post.public_metrics.like_count}")
  ```

  ```javascript JavaScript SDK theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

  // 最近の投稿を検索
  const paginator = client.posts.searchRecent("python", {
    tweetFields: ["created_at", "public_metrics", "author_id"],
    expansions: ["author_id", "attachments.media_keys"],
    userFields: ["username", "verified"],
    mediaFields: ["url", "type"],
    maxResults: 100,
  });

  for await (const page of paginator) {
    page.data?.forEach((post) => {
      console.log(`${post.text} - Likes: ${post.public_metrics?.like_count}`);
    });
  }
  ```
</CodeGroup>

<div id="available-expansions">
  ### 利用可能なexpansions
</div>

| Expansion                    | 戻り値                |
| :--------------------------- | :----------------- |
| `author_id`                  | 投稿者のユーザーオブジェクト     |
| `attachments.media_keys`     | 添付されたメディアオブジェクト    |
| `attachments.poll_ids`       | 添付された投票オブジェクト      |
| `referenced_tweets.id`       | 引用または返信先の投稿        |
| `geo.place_id`               | 場所オブジェクト           |
| `entities.mentions.username` | メンションされたユーザーオブジェクト |

<Card title="フィールドとexpansionsのガイド" icon="sliders" href="/ja/x-api/fundamentals/fields">
  レスポンスのカスタマイズについて詳しく見る
</Card>

***

<div id="pagination">
  ## ページネーション
</div>

検索エンドポイントは結果をページごとに返します。レスポンスに含まれる `next_token` を使用して、後続のページを取得します。

<div id="how-it-works">
  ### 動作の仕組み
</div>

1. 最初のリクエストで `max_results` を指定する
2. `meta` オブジェクト内に `next_token` があるか確認する
3. 後続のリクエストに `next_token` を含める
4. `next_token` が返されなくなるまで繰り返す

<div id="example">
  ### 例
</div>

<CodeGroup dropdown>
  ```bash cURL theme={null}
  # 最初のリクエスト
  curl "https://api.x.com/2/tweets/search/recent?query=python&max_results=100" \
    -H "Authorization: Bearer $BEARER_TOKEN"

  # ページネーション用トークンを使った後続リクエスト
  curl "https://api.x.com/2/tweets/search/recent?query=python&max_results=100&next_token=NEXT_TOKEN" \
    -H "Authorization: Bearer $BEARER_TOKEN"
  ```

  ```python Python SDK theme={null}
  from xdk import Client

  client = Client(bearer_token="YOUR_BEARER_TOKEN")

  # SDK がページネーションを自動で処理します
  all_posts = []

  for page in client.posts.search_recent(query="python", max_results=100):
      if page.data:
          all_posts.extend(page.data)

  print(f"Found {len(all_posts)} posts")
  ```

  ```javascript JavaScript SDK theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

  async function getAllResults(query) {
    const allPosts = [];

    // SDK がページネーションを自動で処理します
    const paginator = client.posts.searchRecent(query, { maxResults: 100 });

    for await (const page of paginator) {
      if (page.data) {
        allPosts.push(...page.data);
      }
    }

    return allPosts;
  }

  // 使用例
  const posts = await getAllResults("python");
  console.log(`Found ${posts.length} posts`);
  ```
</CodeGroup>

<Card title="ページネーションガイド" icon="arrow-right" href="/ja/x-api/posts/search/integrate/paginate">
  ページネーションの詳細はこちら
</Card>

***

<div id="post-edits">
  ## ポストの編集
</div>

投稿は、30分以内であれば最大5回まで編集できます。検索エンドポイントは常に最新版を返します。

<div id="considerations">
  ### 考慮事項
</div>

* `edit_history_tweet_ids` には、すべてのポストID (古いものから順) が含まれます
* 30分の編集可能ウィンドウ経過後に取得された投稿は、最終版を表します
* ほぼリアルタイムのユースケースでは、公開されたばかりの投稿が引き続き編集されている可能性があります

<Card title="ポスト編集の基本" icon="clock-rotate-left" href="/ja/x-api/fundamentals/edit-posts">
  ポストの編集について詳しく学ぶ
</Card>

***

<div id="best-practices">
  ## ベストプラクティス
</div>

<CardGroup cols={2}>
  <Card title="条件を具体的にする" icon="crosshairs">
    複数の演算子を組み合わせて結果を絞り込み、ノイズを減らします。
  </Card>

  <Card title="反復してテストする" icon="flask">
    まずは広い条件から始めて、結果に基づいて徐々に絞り込みます。
  </Card>

  <Card title="ページネーションを実装する" icon="arrow-right">
    大規模な結果セットには、適切なページネーションを実装します。
  </Card>

  <Card title="結果をキャッシュする" icon="database">
    同じリクエストを繰り返さないように、結果をローカルに保存します。
  </Card>
</CardGroup>

***

<div id="next-steps">
  ## 次のステップ
</div>

<CardGroup cols={2}>
  <Card title="クエリを作成する" icon="magnifying-glass" href="/ja/x-api/posts/search/integrate/build-a-query">
    クエリ構文を習得する
  </Card>

  <Card title="オペレーターリファレンス" icon="list-check" href="/ja/x-api/posts/search/integrate/operators">
    利用可能なすべてのオペレーター
  </Card>

  <Card title="ページネーション" icon="arrow-right" href="/ja/x-api/posts/search/integrate/paginate">
    大規模な結果セットを処理する
  </Card>

  <Card title="APIリファレンス" icon="code" href="/ja/x-api/posts/recent-search">
    エンドポイントの詳細なドキュメント
  </Card>
</CardGroup>
