解決策 1
コンテンツをインデックス付けしてそのまま保存する前に html を完全に取り除きたい場合は、マッパー添付プラグインを使用できます。このプラグインでは、マッピングを定義するときに、content_type を「html」に分類できます。
マッパー アタッチメントは、特に複数のドキュメント タイプを処理している場合に多くの場合に役立ちますが、最も注目すべき点は、html タグを取り除く目的でこれを使用するだけで十分だと思います (これは html_strip char フィルターでは実行できません)。
ただし、事前の警告として、html タグは保存されません。したがって、何らかの方法でこれらのタグが必要な場合は、元のコンテンツを格納する別のフィールドを定義することをお勧めします。別の注意: マッパーの添付ドキュメントにはマルチフィールドを指定できないため、マッパーの添付ドキュメントの外部に格納する必要があります。以下の私の作業例を参照してください。
このマッピングを行う必要があります。
{
"html5-es" : {
"aliases" : { },
"mappings" : {
"document" : {
"properties" : {
"delete" : {
"type" : "boolean"
},
"file" : {
"type" : "attachment",
"fields" : {
"content" : {
"type" : "string",
"store" : true,
"term_vector" : "with_positions_offsets",
"analyzer" : "autocomplete"
},
"author" : {
"type" : "string",
"store" : true,
"term_vector" : "with_positions_offsets"
},
"title" : {
"type" : "string",
"store" : true,
"term_vector" : "with_positions_offsets",
"analyzer" : "autocomplete"
},
"name" : {
"type" : "string"
},
"date" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
},
"keywords" : {
"type" : "string"
},
"content_type" : {
"type" : "string"
},
"content_length" : {
"type" : "integer"
},
"language" : {
"type" : "string"
}
}
},
"hash_id" : {
"type" : "string"
},
"path" : {
"type" : "string"
},
"raw_content" : {
"type" : "string",
"store" : true,
"term_vector" : "with_positions_offsets",
"analyzer" : "raw"
},
"title" : {
"type" : "string"
}
}
}
},
"settings" : { //insert your own settings here },
"warmers" : { }
}
}
NESTでは、コンテンツを次のように組み立てます。
Attachment attachment = new Attachment();
attachment.Content = Convert.ToBase64String(File.ReadAllBytes("path/to/document"));
attachment.ContentType = "html";
Document document = new Document();
document.File = attachment;
document.RawContent = InsertRawContentFromString(originalText);
これをSenseでテストしました-結果は次のとおりです。
"file": {
"_content": "PGh0bWwgeG1sbnM6TWFkQ2FwPSJodHRwOi8vd3d3Lm1hZGNhcHNvZnR3YXJlLmNvbS9TY2hlbWFzL01hZENhcC54c2QiPg0KICA8aGVhZCAvPg0KICA8Ym9keT4NCiAgICA8aDE+VG9waWMxMDwvaDE+DQogICAgPHA+RGVsZXRlIHRoaXMgdGV4dCBhbmQgcmVwbGFjZSBpdCB3aXRoIHlvdXIgb3duIGNvbnRlbnQuIENoZWNrIHlvdXIgbWFpbGJveC48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+YXNkZjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD4xMDwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5MYXZlbmRlci48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+MTAvNiAxMjowMzwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD41IDA5PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPjExIDQ3PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPkhhbGxvd2VlbiBpcyBpbiBPY3RvYmVyLjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5qb2c8L3A+DQogIDwvYm9keT4NCjwvaHRtbD4=",
"_content_length": 0,
"_content_type": "html",
"_date": "0001-01-01T00:00:00",
"_title": "Topic10"
},
"delete": false,
"raw_content": "<h1>Topic10</h1><p>Delete this text and replace it with your own content. Check your mailbox.</p><p> </p><p>asdf</p><p> </p><p>10</p><p> </p><p>Lavender.</p><p> </p><p>10/6 12:03</p><p> </p><p>5 09</p><p> </p><p>11 47</p><p> </p><p>Halloween is in October.</p><p> </p><p>jog</p>"
},
"highlight": {
"file.content": [
"\n <em>Topic10</em>\n\n Delete this text and replace it with your own content. Check your mailbox.\n\n \n\n asdf\n\n \n\n 10\n\n \n\n Lavender.\n\n \n\n 10/6 12:03\n\n \n\n 5 09\n\n \n\n 11 47\n\n \n\n Halloween is in October.\n\n \n\n jog\n\n "
]
}
解決策 2
コンテンツにインデックスを付け、標準アナライザーを使用して検索するには、NGram アナライザーを構築する必要があります。
"analyzer" : {
"standard" : {
"type" : "standard"
},
"autocomplete" : {
"filter" : [ "standard", "lowercase" ],
"char_filter" : [ "html_strip" ],
"type" : "custom",
"tokenizer" : "ngram"
}
この例:
入力:「ブラウン」
NGram アナライザー:
- [b]、[br]、[bro]、[brow]、[brown]
- [r]、[ro]、[row]、[row]
- [o]、[ow]、[own]
- [w]、[wn]
- [n]
したがって、オートコンプリート検索を実行すると、それらのインデックス付きフラグメントのいずれかに一致します。ただし、標準のアナライザーを使用して SEARCH (結果のページを返す) のみを行うことが重要です。これにより、それらのランダムなフラグメントのいずれとも一致しないようになります。