Highlight query matches
출처: 문서
본문
검색어 강조 표시
강조 표시는 결과에서 검색어를 강조해서 쿼리 일치 항목을 눈에 띄게 해요.
검색어를 강조 표시하려면 쿼리 블록 외부에 highlight 매개변수를 추가하세요:
GET shakespeare/_search
{
"query": {
"match": {
"text_entry": "life"
}
},
"size": 3,
"highlight": {
"fields": {
"text_entry": {}
}
}
}
결과의 각 문서에는 검색어가 em 태그로 감싸져 표시된 highlight 객체가 포함돼요:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 805,
"relation" : "eq"
},
"max_score" : 7.450247,
"hits" : [
{
"_index" : "shakespeare",
"_id" : "33765",
"_score" : 7.450247,
"_source" : {
"type" : "line",
"line_id" : 33766,
"play_name" : "Hamlet",
"speech_number" : 60,
"line_number" : "2.2.233",
"speaker" : "HAMLET",
"text_entry" : "my life, except my life."
},
"highlight" : {
"text_entry" : [
"my <em>life</em>, except my <em>life</em>."
]
}
},
{
"_index" : "shakespeare",
"_id" : "51877",
"_score" : 6.873042,
"_source" : {
"type" : "line",
"line_id" : 51878,
"play_name" : "King Lear",
"speech_number" : 18,
"line_number" : "4.6.52",
"speaker" : "EDGAR",
"text_entry" : "The treasury of life, when life itself"
},
"highlight" : {
"text_entry" : [
"The treasury of <em>life</em>, when <em>life</em> itself"
]
}
},
{
"_index" : "shakespeare",
"_id" : "39245",
"_score" : 6.6167283,
"_source" : {
"type" : "line",
"line_id" : 39246,
"play_name" : "Henry V",
"speech_number" : 7,
"line_number" : "4.7.31",
"speaker" : "FLUELLEN",
"text_entry" : "mark Alexanders life well, Harry of Monmouths life"
},
"highlight" : {
"text_entry" : [
"mark Alexanders <em>life</em> well, Harry of Monmouths <em>life</em>"
]
}
}
]
}
}
강조 표시 기능은 실제 필드 내용에 대해 작동해요. OpenSearch는 저장된 필드(매핑에서 true로 설정된 필드) 또는 필드가 저장되지 않은 경우 _source 필드에서 이러한 내용을 검색해요. force_source 매개변수를 true로 설정하여 _source 필드에서 필드 내용을 강제로 검색할 수 있어요.
highlight 매개변수는 검색 자체에 동의어나 형태소 분석을 사용하는 경우에도 원래 용어를 강조 표시해요.
{: .note}
오프셋을 얻는 방법
검색어를 강조 표시하려면 하이라이터가 각 용어의 시작 및 끝 문자 오프셋이 필요해요. 오프셋은 원본 텍스트에서 용어의 위치를 나타내요. 하이라이터는 다음 소스에서 오프셋을 얻을 수 있어요:
-
Postings: 문서가 색인될 때 OpenSearch는 역색인(inverted search index)을 생성해요. 이는 문서를 검색하는 데 사용되는 핵심 데이터 구조예요. Postings는 역색인을 나타내며 분석된 각 용어가 발생하는 문서 목록에 대한 매핑을 저장해요. 텍스트 필드를 매핑할 때
index_options매개변수를offsets로 설정하면 OpenSearch는 각 용어의 시작 및 끝 문자 오프셋을 역색인에 추가해요. 강조 표시 중에 하이라이터는 Postings에서 직접 원래 쿼리를 다시 실행하여 각 용어를 찾아요. 따라서 오프셋을 저장하면 대형 필드의 경우 텍스트를 다시 분석할 필요가 없으므로 강조 표시가 더 효율적이에요. 용어 오프셋을 저장하려면 추가 디스크 공간이 필요하지만 용어 벡터를 저장하는 것보다 적은 디스크 공간을 사용해요. -
Term vectors: 텍스트 필드를 매핑할 때
term_vector매개변수를with_positions_offsets로 설정하면 하이라이터는term_vector를 사용하여 필드를 강조 표시해요. 용어 벡터를 저장하려면 가장 많은 디스크 공간이 필요해요. 그러나 1MB보다 큰 필드와 접두사 또는 와일드카드와 같은 다중 용어 쿼리의 경우 용어 벡터가 각 문서의 용어 사전에 대한 액세스를 제공하므로 강조 표시 속도가 빨라져요. -
Text reanalysis: Postings와 term vectors가 모두 없는 경우 하이라이터는 강조 표시를 위해 텍스트를 다시 분석해요. 강조 표시가 필요한 모든 문서와 필드에 대해 하이라이터는 작은 인메모리 인덱스를 생성하고 Lucene의 쿼리 실행 계획자를 통해 원래 쿼리를 다시 실행하여 현재 문서의 낮은 수준의 일치 정보에 액세스해요. 텍스트 재분석은 대부분의 사용 사례에서 잘 작동해요. 그러나 이 방법은 대형 필드의 경우 메모리와 시간이 더 많이 소요돼요.
하이라이터 유형
OpenSearch는 plain, unified, fvh(Fast Vector Highlighter), semantic의 네 가지 하이라이터 구현을 지원해요.
다음 표는 각 하이라이터에 대한 오프셋을 얻는 방법을 나열해요.
| Highlighter | 오프셋을 얻는 방법 |
|---|---|
unified |
term_vector가 with_positions_offsets로 설정된 경우 용어 벡터,index_options가 offsets로 설정된 경우 postings,그 외의 경우 텍스트 재분석. |
fvh |
용어 벡터. |
plain |
텍스트 재분석. |
semantic |
모델 추론. |
하이라이터 유형 설정
하이라이터 유형을 설정하려면 type 필드에 지정하세요:
GET shakespeare/_search
{
"query": {
"match": {
"text_entry": "life"
}
},
"highlight": {
"fields": {
"text_entry": { "type": "plain"}
}
}
}
unified 하이라이터
unified 하이라이터는 Lucene Unified Highlighter를 기반으로 하며 OpenSearch의 기본 하이라이터예요. 텍스트를 문장으로 나누고 해당 문장을 개별 문서로 처리하여 BM25 알고리즘을 사용해 유사성 측면에서 점수를 매겨요. unified 하이라이터는 정확한 구문 강조와 퍼지, 접두사, 정규식을 포함한 다중 용어 강조를 모두 지원해요. 여러 문서의 여러 필드를 강조 표시하기 위해 복잡한 쿼리를 사용하는 경우 postings 또는 term_vector 필드에서 unified 하이라이터를 사용하는 것이 좋아요.
fvh 하이라이터
fvh 하이라이터는 Lucene Fast Vector Highlighter를 기반으로 해요. 이 하이라이터를 사용하려면 위치 오프셋이 있는 용어 벡터를 저장해야 하며, 이는 인덱스 크기를 증가시켜요. fvh 하이라이터는 여러 필드의 일치하는 용어를 하나의 결과로 결합할 수 있어요. 또한 일치 항목의 위치에 따라 가중치를 할당할 수 있으므로 구문 일치를 용어 일치보다 높게 부스트하는 쿼리를 강조 표시할 때 구문 일치를 용어 일치보다 높게 정렬할 수 있어요. 또한 fvh 하이라이터를 구성하여 반환된 텍스트 조각의 경계를 선택할 수 있으며, 여러 단어를 서로 다른 태그로 강조 표시할 수 있어요.
plain 하이라이터
plain 하이라이터는 표준 Lucene 하이라이터를 기반으로 해요. 강조 표시할 필드가 개별적으로 또는 _source 필드에 저장되어 있어야 해요. plain 하이라이터는 쿼리 일치 논리, 특히 단어 중요성과 구문 쿼리에서의 위치를 반영해요. 대부분의 사용 사례에서 작동하지만 강조 표시할 텍스트를 다시 분석해야 하므로 대형 필드의 경우 느릴 수 있어요.
semantic 하이라이터
Introduced 3.0 {: .label .label-purple }
semantic 하이라이터는 기계 학습(ML) 모델을 사용하여 쿼리의 의미에 따라 텍스트 필드 내에서 가장 의미론적으로 관련성 높은 문장이나 구절을 식별하고 강조 표시해요. 이는 다른 하이라이터가 제공하는 전통적인 어휘 일치를 넘어서는 것이에요. Postings나 term vectors의 오프셋에 의존하지 않고 배포된 ML 모델(model_id로 지정)을 사용하여 필드 내용에 대한 추론을 수행해요. 이 접근 방식을 사용하면 정확한 용어가 쿼리와 일치하지 않더라도 문맥상 관련된 텍스트를 강조 표시할 수 있어요. 강조 표시는 문장 수준에서 수행돼요.
semantic 하이라이터는 두 가지 처리 모드를 지원해요:
- 단일 추론 모드(기본값): 문서당 하나의 ML 추론 호출을 사용하여 각 문서를 개별적으로 처리해요. 로컬 및 외부 호스팅 모델을 모두 지원해요.
- 배치 추론 모드: 모든 문서를 단일 ML 추론 호출로 처리하여 여러 문서 결과의 성능을 크게 향상시켜요.
프로덕션 환경에서는 최적의 성능과 확장성을 위해 배치 추론이 활성화된 외부 호스팅 모델을 사용하는 것을 권장해요. {: .tip}
semantic 하이라이터를 사용하기 전에 문장 강조 표시 모델을 구성하고 배포해야 해요. OpenSearch에서 ML 모델을 사용하는 방법에 대한 자세한 내용은 ML 모델 통합을 참조하세요. OpenSearch에서 제공하는 문장 강조 표시 모델에 대한 자세한 내용은 의미론적 문장 강조 표시 모델을 참조하세요.
{: .note}
기본 사용법(단일 추론 모드)
semantic 하이라이터를 사용하려면 fields 객체에서 type을 semantic으로 설정하고 전역 highlight.options 객체 내에 배포된 문장 변환기 또는 질문-답변 모델의 model_id를 제공하세요. 다음 예제는 neural 쿼리를 사용하여 "신경퇴행성 질환 치료"와 관련된 문서를 찾은 다음 지정된 sentence_model_id를 사용하여 의미론적 강조 표시를 적용해요:
POST /neural-search-index/_search
{
"_source": {
"excludes": ["text_embedding"]
},
"query": {
"neural": {
"text_embedding": {
"query_text": "treatments for neurodegenerative diseases",
"model_id": "your-text-embedding-model-id",
"k": 5
}
}
},
"highlight": {
"fields": {
"text": {
"type": "semantic"
}
},
"options": {
"model_id": "your-sentence-model-id"
}
}
}
{% include copy-curl.html %}
응답에는 각 히트에 대한 highlight 객체가 포함되며, 가장 의미론적으로 관련성 높은 문장을 태그로 강조하여 표시해요:
{
"took": 628,
"timed_out": false,
"_shards": { ... },
"hits": {
"total": { "value": 5, "relation": "eq" },
"max_score": 0.4841726,
"hits": [
{
"_index": "neural-search-index",
"_id": "srL7G5YBmDiZSe-G2pDc",
"_score": 0.4841726,
"_source": {
"text": "Alzheimer's disease is a progressive neurodegenerative disorder characterized by accumulation of amyloid-beta plaques and neurofibrillary tangles in the brain. Early symptoms include short-term memory impairment, followed by language difficulties, disorientation, and behavioral changes. While traditional treatments such as cholinesterase inhibitors and memantine provide modest symptomatic relief, they do not alter disease progression. Recent clinical trials investigating monoclonal antibodies targeting amyloid-beta, including aducanumab, lecanemab, and donanemab, have shown promise in reducing plaque burden and slowing cognitive decline. Early diagnosis using biomarkers such as cerebrospinal fluid analysis and PET imaging may facilitate timely intervention and improved outcomes."
},
"highlight": {
"text": [
"Alzheimer's disease is a progressive neurodegenerative disorder ... <em>Recent clinical trials investigating monoclonal antibodies targeting amyloid-beta, including aducanumab, lecanemab, and donanemab, have shown promise in reducing plaque burden and slowing cognitive decline.</em> Early diagnosis using biomarkers ..."
]
}
},
// ... other hits with highlighted sentences ...
]
}
}
단계별 가이드는 의미론적 강조 표시 튜토리얼을 참조하세요.
배치 추론 모드
Introduced 3.3 {: .label .label-purple }
여러 문서를 강조 표시할 때 성능을 향상시키려면 배치 추론 모드를 활성화하세요. 배치 추론 모드는 모든 일치 문서를 단일 ML 모델 추론 호출로 처리하여 단일 추론 모드(문서당 하나의 호출)에 비해 지연 시간을 줄이고 처리량을 향상시켜요.
배치 추론 모드에는 배치 처리 기능이 있는 외부 호스팅 모델이 필요해요. 로컬 모델은 배치 추론을 지원하지 않아요. 외부 호스팅 모델에 대한 자세한 내용은 외부 호스팅 모델에 연결을 참조하세요. {: .note}
먼저 클러스터 설정을 구성합니다(일회성 설정):
PUT _cluster/settings
{
"persistent": {
"search.pipeline.enabled_system_generated_factories": ["semantic-highlighter"]
}
}
{% include copy-curl.html %}
그런 다음 요청 수준에서 ext.semantic_highlighting_batch를 true로 설정해요. 이렇게 하면 요청에서 type: semantic으로 구성된 모든 강조 표시 필드에 대해 배치 추론이 활성화돼요.
다음 예제는 최상위 필드에 대해 배치 의미론적 강조 표시를 활성화해요:
POST /neural-search-index/_search
{
"_source": {
"excludes": ["text_embedding"]
},
"query": {
"neural": {
"text_embedding": {
"query_text": "treatments for neurodegenerative diseases",
"model_id": "your-text-embedding-model-id",
"k": 5
}
}
},
"highlight": {
"fields": {
"text": {
"type": "semantic"
}
},
"options": {
"model_id": "your-remote-semantic-highlighting-model-id"
}
},
"ext": {
"semantic_highlighting_batch": true
}
}
{% include copy-curl.html %}
다음 예제는 inner_hits 내부의 필드에 대해 배치 의미론적 강조 표시를 활성화해요:
POST /neural-search-index/_search
{
"query": {
"nested": {
"path": "chunks",
"query": {
"match": { "chunks.text": "treatments for neurodegenerative diseases" }
},
"inner_hits": {
"size": 5,
"highlight": {
"fields": {
"chunks.text": { "type": "semantic" }
},
"options": {
"model_id": "your-remote-semantic-highlighting-model-id"
}
}
}
}
},
"ext": {
"semantic_highlighting_batch": true
}
}
{% include copy-curl.html %}
배치 추론은 단일 추론과 동일한 형식으로 응답을 제공해요.
하이라이트 옵션
다음 표에서는 전역 또는 필드 수준에서 지정할 수 있는 하이라이트 옵션을 설명해요. 필드 수준 설정이 전역 설정을 재정의해요.
| 옵션 | 설명 |
|---|---|
type |
사용할 하이라이터를 지정해요. 유효한 값은 unified, fvh, plain, semantic이에요. 기본값은 unified예요. |
fields |
하이라이트할 텍스트를 검색할 필드를 지정해요. 와일드카드 표현식을 지원해요. 와일드카드를 사용하면 text 및 keyword 필드만 하이라이트돼요. 예를 들어 fields를 my_field*로 설정하면 접두사 my_field로 시작하는 모든 text 및 keyword 필드가 포함돼요. |
force_source |
하이라이트를 위한 필드 값을 저장된 필드 값 대신 _source 필드에서 가져오도록 지정해요. 기본값은 false예요. |
require_field_match |
검색 쿼리와 일치하는 필드만 하이라이트할지 여부를 지정해요. 기본값은 true예요. 모든 필드를 하이라이트하려면 이 옵션을 false로 설정해요. |
pre_tags |
하이라이트된 텍스트의 HTML 시작 태그를 문자열 배열로 지정해요. |
post_tags |
하이라이트된 텍스트의 HTML 종료 태그를 문자열 배열로 지정해요. |
tags_schema |
이 옵션을 styled로 설정하면 OpenSearch가 내장 태그 스키마를 사용해요. 이 스키마에서 pre_tags는 <em class="hlt1">, <em class="hlt2">, <em class="hlt3">, <em class="hlt4">, <em class="hlt5">, <em class="hlt6">, <em class="hlt7">, <em class="hlt8">, <em class="hlt9">, <em class="hlt10">이고 post_tags는 </em>이에요. |
boundary_chars |
모든 경계 문자를 문자열로 결합한 값이에요. 기본값은 ".,!? \t\n"이에요. |
boundary_scanner |
unified 및 fvh 하이라이터에만 유효해요. 하이라이트된 조각을 문장, 단어 또는 문자로 분할할지 여부를 지정해요. 유효한 값은 다음과 같아요:- sentence: BreakIterator에 정의된 대로 문장 경계에서 하이라이트된 조각을 분할해요. boundary_scanner_locale 옵션에서 BreakIterator의 로케일을 지정할 수 있어요.- word: BreakIterator에 정의된 대로 단어 경계에서 하이라이트된 조각을 분할해요. boundary_scanner_locale 옵션에서 BreakIterator의 로케일을 지정할 수 있어요.- chars: boundary_chars에 나열된 문자에서 하이라이트된 조각을 분할해요. fvh 하이라이터에만 유효해요. |
boundary_scanner_locale |
boundary_scanner에 로케일을 제공해요. 유효한 값은 언어 태그(예: "en-US")예요. 기본값은 Locale.ROOT이에요. |
boundary_max_scan |
fvh 하이라이터의 boundary_scanner 매개변수가 chars로 설정된 경우 경계 문자를 스캔할 범위를 제어해요. 기본값은 20이에요. |
encoder |
하이라이트된 조각이 반환되기 전에 HTML 인코딩되어야 하는지 여부를 지정해요. 유효한 값은 default(인코딩 없음) 또는 html(먼저 HTML 텍스트를 이스케이프한 다음 하이라이트 태그를 삽입)이에요. 예를 들어 필드 텍스트가 <h3>Hamlet</h3>이고 encoder가 html로 설정된 경우 하이라이트된 텍스트는 "<h3><em>Hamlet</em></h3>"이에요. |
fragmenter |
텍스트를 하이라이트된 조각으로 분할하는 방법을 지정해요. plain 하이라이터에만 유효해요. 유효한 값은 다음과 같아요:- span (기본값): 텍스트를 동일한 크기의 조각으로 분할하지만 하이라이트된 용어 사이에서 텍스트를 분할하지 않으려고 해요.- simple: 텍스트를 동일한 크기의 조각으로 분할해요. |
fragment_offset |
하이라이트를 시작할 문자 오프셋을 지정해요. fvh 하이라이터에만 유효해요. |
fragment_size |
하이라이트된 조각의 크기로, 문자 수로 지정해요. number_of_fragments가 0으로 설정되면 fragment_size는 무시돼요. 기본값은 100이에요. |
number_of_fragments |
반환되는 최대 조각 수예요. number_of_fragments가 0으로 설정되면 OpenSearch는 전체 필드의 하이라이트된 내용을 반환해요. 기본값은 5예요. |
order |
하이라이트된 조각의 정렬 순서예요. order를 score로 설정하면 관련성별로 조각을 정렬해요. 각 하이라이터는 다른 알고리즘을 사용하여 관련성 점수를 계산해요. 기본값은 none이에요. |
highlight_query |
검색 쿼리 외의 쿼리에 대한 일치 항목을 하이라이트해야 한다고 지정해요. highlight_query 옵션은 더 빠른 쿼리를 사용하여 문서 일치 항목을 가져오고 더 느린 쿼리(예: rescore_query)를 사용하여 결과를 세분화할 때 유용해요. 검색 쿼리를 highlight_query의 일부로 포함하는 것이 좋아요. |
matched_fields |
여러 필드의 일치 항목을 결합하여 하나의 필드를 하이라이트해요. 이 기능의 가장 일반적인 사용 사례는 다양한 방식으로 분석되고 다중 필드에 보관된 텍스트를 하이라이트하는 것이에요. fvh를 사용하는 경우 matched_fields 목록의 모든 필드는 term_vector 필드가 with_positions_offsets로 설정되어야 해요. 일치 항목이 결합되는 필드만 로드되는 필드이므로 store 옵션을 yes로 설정하는 것이 좋아요. fvh 및 unified 하이라이터에만 유효해요. |
no_match_size |
일치하는 하이라이트 조각이 없는 경우 필드 시작 부분부터 반환할 문자 수를 지정해요. 기본값은 0이에요. |
phrase_limit |
고려되는 문서의 일치하는 구문 수예요. 많은 메모리를 소비하지 않도록 fvh 하이라이터가 분석할 구문 수를 제한해요. matched_fields가 사용되는 경우 phrase_limit는 각 일치 필드의 구문 수를 지정해요. phrase_limit가 높을수록 쿼리 시간이 증가하고 메모리 소비가 늘어나요. fvh 하이라이터에만 유효해요. 기본값은 256이에요. |
max_analyzer_offset |
하이라이트 요청에서 분석할 최대 문자 수를 지정해요. 나머지 텍스트는 처리되지 않아요. 하이라이트할 텍스트가 이 오프셋을 초과하면 빈 하이라이트가 반환돼요. 하이라이트 요청에 대해 분석될 최대 문자 수는 index.highlight.max_analyzed_offset에 의해 정의돼요. 이 제한에 도달하면 오류가 반환돼요. 오류를 방지하려면 max_analyzer_offset을 index.highlight.max_analyzed_offset보다 낮은 값으로 설정해요. |
options |
하이라이터별 옵션을 포함하는 전역 객체예요. |
options.max_inference_batch_size |
배치 추론 모드를 사용할 때 모델 서버에 대한 각 추론 요청에 포함할 최대 문서 수를 지정해요. 처리할 문서 수가 이 값을 초과하면 문서는 이 크기의 배치로 반복적으로 처리돼요. 기본값은 100이에요. 배치 추론 모드가 활성화된 경우 semantic 하이라이터에만 유효해요. |
options.model_id |
하이라이트에 사용할 배포된 ML 모델의 ID예요. semantic 하이라이터에 필요해요. 배치 추론 모드가 활성화된 경우 모델은 배치 처리 기능이 있는 외부 호스팅 모델이어야 해요. 외부 호스팅 모델에 연결을 참조하세요. |
통합 하이라이터의 문장 스캐너는 fragment_size보다 큰 문장을 fragment_size에 도달한 후 첫 번째 단어 경계에서 분할해요. 분할 없이 전체 문장을 반환하려면 fragment_size를 0으로 설정해요.
{: .note}
하이라이트 태그 변경
애플리케이션 코드를 설계하여 highlight 객체의 결과를 구문 분석하고 검색어의 색상을 변경하거나 굵게 또는 기울임꼴 서식을 적용하는 등의 작업을 수행해요.
기본 em 태그를 변경하려면 pretag 및 posttag 매개변수에 새 태그를 지정해요:
GET shakespeare/_search
{
"query": {
"match": {
"play_name": "Henry IV"
}
},
"size": 3,
"highlight": {
"pre_tags": [
"<strong>"
],
"post_tags": [
"</strong>"
],
"fields": {
"play_name": {}
}
}
}
응답에서 새 태그로 플레이 이름이 하이라이트돼요:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3205,
"relation" : "eq"
},
"max_score" : 3.548232,
"hits" : [
{
"_index" : "shakespeare",
"_id" : "0",
"_score" : 3.548232,
"_source" : {
"type" : "act",
"line_id" : 1,
"play_name" : "Henry IV",
"speech_number" : "",
"line_number" : "",
"speaker" : "",
"text_entry" : "ACT I"
},
"highlight" : {
"play_name" : [
"<strong>Henry IV</strong>"
]
}
},
{
"_index" : "shakespeare",
"_id" : "1",
"_score" : 3.548232,
"_source" : {
"type" : "scene",
"line_id" : 2,
"play_name" : "Henry IV",
"speech_number" : "",
"line_number" : "",
"speaker" : "",
"text_entry" : "SCENE I. London. The palace."
},
"highlight" : {
"play_name" : [
"<strong>Henry IV</strong>"
]
}
},
{
"_index" : "shakespeare",
"_id" : "2",
"_score" : 3.548232,
"_source" : {
"type" : "line",
"line_id" : 3,
"play_name" : "Henry IV",
"speech_number" : "",
"line_number" : "",
"speaker" : "",
"text_entry" : "Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others"
},
"highlight" : {
"play_name" : [
"<strong>Henry IV</strong>"
]
}
}
]
}
}
하이라이트 쿼리 지정
기본적으로 OpenSearch는 하이라이트를 위해 검색 쿼리만 고려해요. 빠른 쿼리를 사용하여 문서 일치 항목을 가져오고 rescore_query와 같은 느린 쿼리를 사용하여 결과를 세분화하는 경우 세분화된 결과를 하이라이트하는 것이 유용해요. highlight_query를 추가하여 이 작업을 수행할 수 있어요:
GET shakespeare/_search
{
"query": {
"match": {
"text_entry": {
"query": "thats my name"
}
}
},
"rescore": {
"window_size": 20,
"query": {
"rescore_query": {
"match_phrase": {
"text_entry": {
"query": "thats my name",
"slop": 1
}
}
},
"rescore_query_weight": 5
}
},
"_source": false,
"highlight": {
"order": "score",
"fields": {
"text_entry": {
"highlight_query": {
"bool": {
"must": {
"match": {
"text_entry": {
"query": "thats my name"
}
}
},
"should": {
"match_phrase": {
"text_entry": {
"query": "that is my name",
"slop": 1,
"boost": 10.0
}
}
},
"minimum_should_match": 0
}
}
}
}
}
}
여러 필드의 일치 항목을 결합하여 하나의 필드 하이라이트
fvh 하이라이터를 사용하여 여러 필드의 일치 항목을 결합하여 하나의 필드를 하이라이트할 수 있어요. 이 기능의 가장 일반적인 사용 사례는 다양한 방식으로 분석되고 다중 필드에 보관된 텍스트를 하이라이트하는 것이에요. matched_fields 목록의 모든 필드는 term_vector 필드가 with_positions_offsets로 설정되어야 해요. 일치 항목이 결합되는 필드만 로드되는 필드이므로 store 옵션을 yes로 설정하는 것이 좋아요.
예제
text_entry 필드가 standard 분석기로 분석되고 english 분석기로 분석되는 english 하위 필드가 있는 shakespeare 인덱스에 대한 매핑을 생성해요:
PUT shakespeare
{
"mappings" : {
"properties" : {
"text_entry" : {
"type" : "text",
"term_vector": "with_positions_offsets",
"fields": {
"english": {
"type": "text",
"analyzer": "english",
"term_vector": "with_positions_offsets"
}
}
}
}
}
}
standard 분석기는 text_entry 필드를 개별 단어로 분할해요. analyze API 작업을 사용하여 이를 확인할 수 있어요:
GET shakespeare/_analyze
{
"text": "bragging of thine",
"field": "text_entry"
}
응답에는 공백으로 분할된 원래 문자열이 포함돼요:
{
"tokens" : [
{
"token" : "bragging",
"start_offset" : 0,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "of",
"start_offset" : 9,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "thine",
"start_offset" : 12,
"end_offset" : 17,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
english 분석기는 문자열을 단어로 분할할 뿐만 아니라 토큰을 형태소 분석하고 불용어를 제거해요. text_entry.english 필드와 함께 analyze API 작업을 사용하여 이를 확인할 수 있어요:
GET shakespeare/_analyze
{
"text": "bragging of thine",
"field": "text_entry.english"
}
응답에는 형태소 분석된 단어가 포함돼요:
{
"tokens" : [
{
"token" : "brag",
"start_offset" : 0,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "thine",
"start_offset" : 12,
"end_offset" : 17,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
bragging이라는 단어의 모든 형태를 검색하려면 다음 쿼리를 사용해요:
GET shakespeare/_search
{
"query": {
"query_string": {
"query": "text_entry.english:bragging",
"fields": [
"text_entry"
]
}
},
"highlight": {
"order": "score",
"fields": {
"text_entry": {
"matched_fields": [
"text_entry",
"text_entry.english"
],
"type": "fvh"
}
}
}
}
응답은 text_entry 필드에서 "bragging"이라는 단어의 모든 버전을 하이라이트해요:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 26,
"relation" : "eq"
},
"max_score" : 10.153671,
"hits" : [
{
"_index" : "shakespeare",
"_id" : "56666",
"_score" : 10.153671,
"_source" : {
"type" : "line",
"line_id" : 56667,
"play_name" : "macbeth",
"speech_number" : 34,
"line_number" : "2.3.118",
"speaker" : "MACBETH",
"text_entry" : "Is left this vault to brag of."
},
"highlight" : {
"text_entry" : [
"Is left this vault to <em>brag</em> of."
]
}
},
{
"_index" : "shakespeare",
"_id" : "71445",
"_score" : 9.284528,
"_source" : {
"type" : "line",
"line_id" : 71446,
"play_name" : "Much Ado about nothing",
"speech_number" : 18,
"line_number" : "5.1.65",
"speaker" : "LEONATO",
"text_entry" : "As under privilege of age to brag"
},
"highlight" : {
"text_entry" : [
"As under privilege of age to <em>brag</em>"
]
}
},
{
"_index" : "shakespeare",
"_id" : "86782",
"_score" : 9.284528,
"_source" : {
"type" : "line",
"line_id" : 86783,
"play_name" : "Romeo and Juliet",
"speech_number" : 8,
"line_number" : "2.6.31",
"speaker" : "JULIET",
"text_entry" : "Brags of his substance, not of ornament:"
},
"highlight" : {
"text_entry" : [
"<em>Brags</em> of his substance, not of ornament:"
]
}
},
{
"_index" : "shakespeare",
"_id" : "44531",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 44532,
"play_name" : "King John",
"speech_number" : 15,
"line_number" : "3.1.124",
"speaker" : "CONSTANCE",
"text_entry" : "A ramping fool, to brag and stamp and swear"
},
"highlight" : {
"text_entry" : [
"A ramping fool, to <em>brag</em> and stamp and swear"
]
}
},
{
"_index" : "shakespeare",
"_id" : "63208",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 63209,
"play_name" : "Merchant of Venice",
"speech_number" : 11,
"line_number" : "3.4.79",
"speaker" : "PORTIA",
"text_entry" : "A thousand raw tricks of these bragging Jacks,"
},
"highlight" : {
"text_entry" : [
"A thousand raw tricks of these <em>bragging</em> Jacks,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "73026",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 73027,
"play_name" : "Othello",
"speech_number" : 75,
"line_number" : "2.1.242",
"speaker" : "IAGO",
"text_entry" : "but for bragging and telling her fantastical lies:"
},
"highlight" : {
"text_entry" : [
"but for <em>bragging</em> and telling her fantastical lies:"
]
}
},
{
"_index" : "shakespeare",
"_id" : "85974",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 85975,
"play_name" : "Romeo and Juliet",
"speech_number" : 20,
"line_number" : "1.5.70",
"speaker" : "CAPULET",
"text_entry" : "And, to say truth, Verona brags of him"
},
"highlight" : {
"text_entry" : [
"And, to say truth, Verona <em>brags</em> of him"
]
}
},
{
"_index" : "shakespeare",
"_id" : "96800",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 96801,
"play_name" : "Titus Andronicus",
"speech_number" : 60,
"line_number" : "1.1.311",
"speaker" : "SATURNINUS",
"text_entry" : "Agree these deeds with that proud brag of thine,"
},
"highlight" : {
"text_entry" : [
"Agree these deeds with that proud <em>brag</em> of thine,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "18189",
"_score" : 7.9273787,
"_source" : {
"type" : "line",
"line_id" : 18190,
"play_name" : "As you like it",
"speech_number" : 12,
"line_number" : "5.2.30",
"speaker" : "ROSALIND",
"text_entry" : "and Caesars thrasonical brag of I came, saw, and"
},
"highlight" : {
"text_entry" : [
"and Caesars thrasonical <em>brag</em> of I came, saw, and"
]
}
},
{
"_index" : "shakespeare",
"_id" : "32054",
"_score" : 7.9273787,
"_source" : {
"type" : "line",
"line_id" : 32055,
"play_name" : "Cymbeline",
"speech_number" : 52,
"line_number" : "5.5.211",
"speaker" : "IACHIMO",
"text_entry" : "And then a mind put int, either our brags"
},
"highlight" : {
"text_entry" : [
"And then a mind put int, either our <em>brags</em>"
]
}
}
]
}
}
단어 "bragging"의 원래 형태에 더 높은 점수를 부여하려면 text_entry 필드를 부스트할 수 있어요:
GET shakespeare/_search
{
"query": {
"query_string": {
"query": "bragging",
"fields": [
"text_entry^5",
"text_entry.english"
]
}
},
"highlight": {
"order": "score",
"fields": {
"text_entry": {
"matched_fields": [
"text_entry",
"text_entry.english"
],
"type": "fvh"
}
}
}
}
응답에는 "bragging"이라는 단어가 포함된 문서가 먼저 나열돼요:
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 26,
"relation" : "eq"
},
"max_score" : 49.746853,
"hits" : [
{
"_index" : "shakespeare",
"_id" : "45739",
"_score" : 49.746853,
"_source" : {
"type" : "line",
"line_id" : 45740,
"play_name" : "King John",
"speech_number" : 10,
"line_number" : "5.1.51",
"speaker" : "BASTARD",
"text_entry" : "Of bragging horror: so shall inferior eyes,"
},
"highlight" : {
"text_entry" : [
"Of <em>bragging</em> horror: so shall inferior eyes,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "63208",
"_score" : 47.077244,
"_source" : {
"type" : "line",
"line_id" : 63209,
"play_name" : "Merchant of Venice",
"speech_number" : 11,
"line_number" : "3.4.79",
"speaker" : "PORTIA",
"text_entry" : "A thousand raw tricks of these bragging Jacks,"
},
"highlight" : {
"text_entry" : [
"A thousand raw tricks of these <em>bragging</em> Jacks,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "68474",
"_score" : 47.077244,
"_source" : {
"type" : "line",
"line_id" : 68475,
"play_name" : "A Midsummer nights dream",
"speech_number" : 101,
"line_number" : "3.2.427",
"speaker" : "PUCK",
"text_entry" : "Thou coward, art thou bragging to the stars,"
},
"highlight" : {
"text_entry" : [
"Thou coward, art thou <em>bragging</em> to the stars,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "73026",
"_score" : 47.077244,
"_source" : {
"type" : "line",
"line_id" : 73027,
"play_name" : "Othello",
"speech_number" : 75,
"line_number" : "2.1.242",
"speaker" : "IAGO",
"text_entry" : "but for bragging and telling her fantastical lies:"
},
"highlight" : {
"text_entry" : [
"but for <em>bragging</em> and telling her fantastical lies:"
]
}
},
{
"_index" : "shakespeare",
"_id" : "39816",
"_score" : 44.679565,
"_source" : {
"type" : "line",
"line_id" : 39817,
"play_name" : "Henry V",
"speech_number" : 28,
"line_number" : "5.2.138",
"speaker" : "KING HENRY V",
"text_entry" : "armour on my back, under the correction of bragging"
},
"highlight" : {
"text_entry" : [
"armour on my back, under the correction of <em>bragging</em>"
]
}
},
{
"_index" : "shakespeare",
"_id" : "63200",
"_score" : 44.679565,
"_source" : {
"type" : "line",
"line_id" : 63201,
"play_name" : "Merchant of Venice",
"speech_number" : 11,
"line_number" : "3.4.71",
"speaker" : "PORTIA",
"text_entry" : "Like a fine bragging youth, and tell quaint lies,"
},
"highlight" : {
"text_entry" : [
"Like a fine <em>bragging</em> youth, and tell quaint lies,"
]
}
},
{
"_index" : "shakespeare",
"_id" : "56666",
"_score" : 10.153671,
"_source" : {
"type" : "line",
"line_id" : 56667,
"play_name" : "macbeth",
"speech_number" : 34,
"line_number" : "2.3.118",
"speaker" : "MACBETH",
"text_entry" : "Is left this vault to brag of."
},
"highlight" : {
"text_entry" : [
"Is left this vault to <em>brag</em> of."
]
}
},
{
"_index" : "shakespeare",
"_id" : "71445",
"_score" : 9.284528,
"_source" : {
"type" : "line",
"line_id" : 71446,
"play_name" : "Much Ado about nothing",
"speech_number" : 18,
"line_number" : "5.1.65",
"speaker" : "LEONATO",
"text_entry" : "As under privilege of age to brag"
},
"highlight" : {
"text_entry" : [
"As under privilege of age to <em>brag</em>"
]
}
},
{
"_index" : "shakespeare",
"_id" : "86782",
"_score" : 9.284528,
"_source" : {
"type" : "line",
"line_id" : 86783,
"play_name" : "Romeo and Juliet",
"speech_number" : 8,
"line_number" : "2.6.31",
"speaker" : "JULIET",
"text_entry" : "Brags of his substance, not of ornament:"
},
"highlight" : {
"text_entry" : [
"<em>Brags</em> of his substance, not of ornament:"
]
}
},
{
"_index" : "shakespeare",
"_id" : "44531",
"_score" : 8.552448,
"_source" : {
"type" : "line",
"line_id" : 44532,
"play_name" : "King John",
"speech_number" : 15,
"line_number" : "3.1.124",
"speaker" : "CONSTANCE",
"text_entry" : "A ramping fool, to brag and stamp and swear"
},
"highlight" : {
"text_entry" : [
"A ramping fool, to <em>brag</em> and stamp and swear"
]
}
}
]
}
}
쿼리 제한 사항
다음 제한 사항에 유의하세요:
- 하이라이트할 용어를 추출할 때 하이라이터는 쿼리의 부울 논리를 반영하지 않아요. 따라서 중첩 부울 쿼리나
minimum_should_match를 사용하는 쿼리와 같은 일부 복잡한 부울 쿼리의 경우 OpenSearch가 쿼리 일치 항목에 해당하지 않는 용어를 하이라이트할 수 있어요. fvh하이라이터는 span 쿼리를 지원하지 않아요.semantic하이라이터는highlight.options의model_id로 지정된 배포된 ML 모델이 필요해요. 전통적인 오프셋 방법(포스팅 및 용어 벡터)을 사용하지 않고 전적으로 모델 추론에 의존해요. 배치 추론 모드의 경우 배치 처리 기능이 있는 외부 호스팅 모델을 사용해야 해요.