HTML strip character filter
출처: 문서
본문
HTML strip 문자 필터
html_strip 문자 필터는 입력 텍스트에서 <div>, <p>, <a> 같은 HTML 태그를 제거하고 일반 텍스트로 변환해요. 이 필터는 특정 태그를 보존하거나 같은 HTML 엔티티를 공백으로 디코딩하도록 설정할 수 있어요.
예시
다음 요청은 제공된 텍스트에 html_strip 문자 필터를 적용해요:
GET /_analyze
{
"tokenizer": "keyword",
"char_filter": [
"html_strip"
],
"text": "<p>Commonly used calculus symbols include α, β and θ </p>"
}
{% include copy-curl.html %}
응답에는 HTML 문자가 디코딩된 값으로 변환된 토큰이 포함되어 있어요:
{
"tokens": [
{
"token": """
Commonly used calculus symbols include α, β and θ
""",
"start_offset": 0,
"end_offset": 74,
"type": "word",
"position": 0
}
]
}
매개변수
html_strip 문자 필터는 다음 매개변수로 설정할 수 있어요.
| 매개변수 | 필수/선택 | 데이터 타입 | 설명 |
|---|---|---|---|
escaped_tags |
선택 | 문자열 배열 | HTML 요소 이름의 배열로, 꺾쇠괄호(< >) 없이 지정해요. 필터는 텍스트에서 HTML을 제거할 때 이 목록에 있는 요소는 제거하지 않아요. 예를 들어 배열을 ["b", "i"]로 설정하면 <b>와 <i> 요소가 제거되지 않아요. |
예시: 소문자 필터를 사용한 사용자 정의 분석기
다음 예제 요청은 html_strip 분석기와 lowercase 필터를 사용하여 HTML 태그를 제거하고 일반 텍스트를 소문자로 변환하는 사용자 정의 분석기를 만들어요:
PUT /html_strip_and_lowercase_analyzer
{
"settings": {
"analysis": {
"char_filter": {
"html_filter": {
"type": "html_strip"
}
},
"analyzer": {
"html_strip_analyzer": {
"type": "custom",
"char_filter": ["html_filter"],
"tokenizer": "standard",
"filter": ["lowercase"]
}
}
}
}
}
{% include copy-curl.html %}
다음 요청을 사용하여 분석기로 생성된 토큰을 확인해요:
GET /html_strip_and_lowercase_analyzer/_analyze
{
"analyzer": "html_strip_analyzer",
"text": "<h1>Welcome to <strong>OpenSearch</strong>!</h1>"
}
{% include copy-curl.html %}
응답에서 HTML 태그가 제거되고 일반 텍스트가 소문자로 변환되었어요:
{
"tokens": [
{
"token": "welcome",
"start_offset": 4,
"end_offset": 11,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "to",
"start_offset": 12,
"end_offset": 14,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "opensearch",
"start_offset": 23,
"end_offset": 42,
"type": "<ALPHANUM>",
"position": 2
}
]
}
예시: HTML 태그를 보존하는 사용자 정의 분석기
다음 예제 요청은 HTML 태그를 보존하는 사용자 정의 분석기를 만들어요:
PUT /html_strip_preserve_analyzer
{
"settings": {
"analysis": {
"char_filter": {
"html_filter": {
"type": "html_strip",
"escaped_tags": ["b", "i"]
}
},
"analyzer": {
"html_strip_analyzer": {
"type": "custom",
"char_filter": ["html_filter"],
"tokenizer": "keyword"
}
}
}
}
}
{% include copy-curl.html %}
다음 요청을 사용하여 분석기로 생성된 토큰을 확인해요:
GET /html_strip_preserve_analyzer/_analyze
{
"analyzer": "html_strip_analyzer",
"text": "<p>This is a <b>bold</b> and <i>italic</i> text.</p>"
}
{% include copy-curl.html %}
응답에서 사용자 정의 분석기 요청에 지정된 대로 italic과 bold 태그가 유지되었어요:
{
"tokens": [
{
"token": """
This is a <b>bold</b> and <i>italic</i> text.
""",
"start_offset": 0,
"end_offset": 52,
"type": "word",
"position": 0
}
]
}