AliNLPトークナイザーは、analysis-aliws プラグインとも呼ばれる、Alibaba Cloud Elasticsearch のビルトインプラグインです。 analysis-aliws プラグインを使用すると、Alibaba Cloud Elasticsearch の連携アナライザーとトークナイザーを使用して、ドキュメントを分析およびトークン化できます。

AliNLP トークナイザーをインストールする

重要 AliNLPトークナイザーをインストールする前に、Alibaba Cloud Elasticsearch インスタンスのタイプが 2 コア 8 GB 以上であることを確認してください。 Elasticsearch インスタンスが要件を満たしていない場合、最初にインスタンスタイプ を 2 コア 8 GB 以上にアップグレードしてください。 詳細については、「クラスターのアップグレード」をご参照ください。
Alibaba Cloud Elasticsearch コンソール にログインし、[インスタンス ID] > [プラグイン] > [ビルトインプラグインリスト] を選択します。 [ビルトインプラグインリスト] で、 analysis-aliws プラグインのインストールをクリックします。 詳細については、「手順」をご参照ください。 analysis-aliws plug-in
重要 デフォルトでは、analysis-aliws のステータスは、[未インストール] です。

AliNLP トークナイザーを使用する

AliNLP トークナイザーがインストールされると、Alibaba Cloud Elasticsearch は次のアナライザーおよびトークナイザーと連携します。
  • アナライザー:aliws
  • トークナイザー:aliws_tokenizer

アナライザーとトークナイザーを使用して、次のように指定されたコンテンツを含むドキュメントを検索できます。

  1. インデックスを作成します。
    PUT /index
    {
        "mappings": {
            "fulltext": {
                "properties": {
                    "content": {
                        "type": "text",
                        "analyzer": "aliws"
                    }
                }
            }
        }
    }

    上記のコードは、index という名前のインデックスを作成します。 インデックスのタイプは fulltext です。 インデックスには、content プロパティが含まれています。 プロパティのタイプは text です。 また、コードは aliws アナライザーを追加します。

    コードが正常に実行されると、次の結果が返されます。
    {
      "acknowledged": true,
      "shards_acknowledged": true,
      "index": "index"
    }
  2. ドキュメントを追加します。
    POST /index/fulltext/1
    {
      "content": "学校に行くのが好き"
    }

    上記のコードは、1 という名前のドキュメントを追加します。また、ドキュメントの content フィールドの値を学校に行くのが好きに設定します。

    データディスクが正常にマウントされると、次の結果が返されます。
    {
      "_index": "my_index",
      "type": "text",
      "_id": "1",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful" : 2,
        "failed": 0,
      },
      "_seq_no": 0,
      "_primary_term": 1
    }
  3. ドキュメントを検索します。
    GET /index/fulltext/_search
    {
      "query": {
        "match": {
          "content": "学校"
        }
      }
    }

    上記のコードでは、aliws アナライザーを使用してすべての fulltext タイプのドキュメントを分析し、content フィールドに学校 が含まれるドキュメントを返します。

    コードが正常に実行されると、次の結果が返されます。
    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
          {
            "_index": "index",
            "_type": "fulltext",
            "_id": "2",
            "_score": 0.2876821,
            "_source": {
              "content": "学校に行くのが好き"
            }
          }
        ]
      }
    }
analysis-aliws プラグインを使用して期待される結果を取得できない場合は、#d7e177 および #d7e192 を参照して原因を特定してください。

アナライザーのデバッグ

GET _analyze
{
  "text": "I like go to school.",
  "analyzer": "aliws"
}
想定結果は以下の通りです。
{
  "tokens" : [
    {
      "token" : "i",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "like",
      "start_offset" : 2,
      "end_offset" : 6,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "go",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "word",
      "position" : 4
    },
    {
      "token" : "school",
      "start_offset" : 13,
      "end_offset" : 19,
      "type" : "word",
      "position" : 8
    }
  ]
}

トークナイザーのデバッグ

GET _analyze
{
  "text": "I like go to school.",
  "tokenizer": "aliws_tokenizer"
}
想定結果は以下の通りです。
{
  "tokens" : [
    {
      "token" : "I",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : " ",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "like",
      "start_offset" : 2,
      "end_offset" : 6,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : " ",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "word",
      "position" : 3
    },
    {
      "token" : "go",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "word",
      "position" : 4
    },
    {
      "token" : " ",
      "start_offset" : 9,
      "end_offset" : 10,
      "type" : "word",
      "position" : 5
    },
    {
      "token" : "to",
      "start_offset" : 10,
      "end_offset" : 12,
      "type" : "word",
      "position" : 6
    },
    {
      "token" : " ",
      "start_offset" : 12,
      "end_offset" : 13,
      "type" : "word",
      "position" : 7
    },
    {
      "token" : "school",
      "start_offset" : 13,
      "end_offset" : 19,
      "type" : "word",
      "position" : 8
    },
    {
      "token" : ".",
      "start_offset" : 19,
      "end_offset" : 20,
      "type" : "word",
      "position" : 9
    }
  ]
}