📝

[小ネタ] Amazon Kendra の Search console の言語設定について

に公開

データソースの言語と一致しない場合、検索結果が表示されませんでした。

検証内容

  • S3 バケットを作成し、test.txt をアップロード
  • Kendra インデックス作成
  • Kendra データソース作成
    • データソースは S3 バケット
    • 言語は日本語
  • 同期実行

上記設定後、Search console で「test」と検索したものの、結果が表示されませんでした。

そこで、Setting を確認したところ、言語設定があったのでデータソースの言語である日本語に設定しました。
設定変更後に再度検索し、結果が表示されました。

API の場合

上記 API でも言語を明示的に指定しない場合には結果を取得できませんでした。

  • 失敗パターン
import { KendraClient, RetrieveCommand } from "@aws-sdk/client-kendra";

const client = new KendraClient({ region: "ap-northeast-1" });

export const handler = async (event) => {
  const indexId = "YOUR_INDEX_ID";
  const queryText = "test";

  const command = new RetrieveCommand({
    IndexId: indexId,
    QueryText: queryText,
  });

  const response = await client.send(command);
  console.log(JSON.stringify(response, null, 2));
};

上記コードの実行結果は以下の通り、ResultItems が空です。

{
  "$metadata": {
    "httpStatusCode": 200,
    "requestId": "f8018f7a-464b-4af9-9b66-464a2be78bd5",
    "attempts": 1,
    "totalRetryDelay": 0
  },
  "QueryId": "6a0ada39-8bf5-41c6-a0ab-447cdcb2c0ae",
  "ResultItems": []
}
  • 成功パターン
import { KendraClient, RetrieveCommand } from "@aws-sdk/client-kendra";

const client = new KendraClient({ region: "ap-northeast-1" });

export const handler = async (event) => {
  const indexId = "YOUR_INDEX_ID";
  const queryText = "test";

  const command = new RetrieveCommand({
    IndexId: indexId,
    QueryText: queryText,
    AttributeFilter: {
      EqualsTo: {
        Key: "_language_code",
        Value: {
          StringValue: "ja",
        },
      },
    },
  });

  const response = await client.send(command);
  console.log(JSON.stringify(response, null, 2));
};

上記コードの実行結果では ResultItems に検索結果が含まれていました。

{
  "$metadata": {
    "httpStatusCode": 200,
    "requestId": "0e4191f4-90b3-4fd4-9795-5f7889bcfa6a",
    "attempts": 1,
    "totalRetryDelay": 0
  },
  "QueryId": "6583a289-e9b4-4ec3-851e-adf608a13089",
  "ResultItems": [
    {
      "Content": "test",
      "DocumentAttributes": [
        {
          "Key": "_source_uri",
          "Value": {
            "StringValue": "xxx"
          }
        },
        {
          "Key": "s3_document_id",
          "Value": {
            "StringValue": "test.txt"
          }
        }
      ],
      "DocumentId": "xxx",
      "DocumentTitle": "TEST.txt",
      "DocumentURI": "xxx",
      "Id": "6583a289-e9b4-4ec3-851e-adf608a13089-c1d58142-412f-4753-a80d-259b518839d4",
      "ScoreAttributes": {
        "ScoreConfidence": "NOT_AVAILABLE"
      }
    }
  ]
}

言語の設定について

Creating a data source connector - Amazon Kendra

If you don't specify a language, Amazon Kendra indexes documents in a data source in English by default.

言語を指定しない場合、デフォルトでは英語でインデックス化される仕様があるので、検索時には明示的に言語を指定する必要がありました。

まとめ

今回は Amazon Kendra の Search console の言語設定について紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion