Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Cognitive Search
- Azure
- 동화책
- 토끼
- Docker
- AI 동화
- 파라미터 힌트
- 대스크
- Redshift
- 아기동화
- 제로샷
- GPT
- Compute
- 프롬프트 튜닝
- dask
- FOR
- 인컨텍스트 러닝
- 모험
- 컨텍스트 매니져
- UDF
- 검색
- Cognitive Service
- Ai
- 퓨샷
- 도커
- Python
- 인자 힌트
- 파이썬
- 반복문
- 조건문
Archives
- Today
- Total
호랭이 분석가
[Azure] Cognitive Search로 자동 완성 검색 구현하기 Suggest API(Python) 본문
Azure/Cognitive Search
[Azure] Cognitive Search로 자동 완성 검색 구현하기 Suggest API(Python)
데이터호랑이 2023. 7. 7. 00:14반응형
[Azure] Cognitive Search로 검색 및 필터 설정 (Python)
[Azure] Cognitive Search Index 생성, 삭제, 업로드 Python을 이용하여 Azure Cognitive Search의 Indexes를 생성할 때, 사용할 수 있는 Class는 무엇이 있는지 알아보겠습니다. Azure Cognitive Search client library for Python Ta
dataiger.tistory.com
기본적인 검색과 필터링을 하였으니 검색 클라이언트 중
Suggest API를 이용한 자동 완성 기능을 구현해 보겠습니다.
기본적인 검색을 구현하기 위해서 인덱스를 생성하였는데
자동 완성 검색을 구현하려면 추가적으로 suggesters를 설정해주셔야 합니다.
# 인덱스 생성
from azure.core.credentials import AzureKeyCredential
from azrue.search.documents.indexes import SearchIndexClient
search_service_name = 'your-service-name'
key = 'your-api-key'
endpoint = f'https://{serach_service_name}.search.windows.net/'
# 인덱스 클라이언트를 생성
index_client = SearchIndexClient(endpoint, AzureKeyCredential(key))
# 샘플 인덱스명 / 필드 상세
index_name = 'test'
fields = [
SimpleField(name="testId", type=SearchFieldDataType.String, key=True),
SearchableField(name="testRate", type=SearchFieldDataType.Double,
filterable=True),
SearchableField(name="description", type=SearchFieldDataType.String),
SearchableField(name="testTag", type=SearchFieldDataType.String,
filterable=True, collection=True)
]
위 필드에 상세 설정까지는 기본적으로 동일합니다만 CORS Option과 Scoring Profile은 동일하게 두고,
추가적으로 suggeter를 생성하는 코드를 작성합니다.
# Suggester 설정
from azure.search.documents.indexes.models import (
Suggester,
CorsOptions,
ScoringProfile
)
suggester = Suggester(
name = 'testSuggester' # suggester title
source_fields = ['description', 'testTag'] # 자동 완성 대상 필드
)
cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
scoring_profiles = []
index = SearchIndex(
name=index_name,
fields=fields,
suggester=suggester,
scoring_profiles=scoring_profiles,
cors_options=cors_options)
result = index_client.create_index(index)
이제 자동 완성 검색을 위한 준비는 모두 끝났습니다.
이제 자동 완성 검색을 해보도록 하겠습니다.
from azure.search.documents import SearchClient
# 검색 클라이언트 생성
search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
# 자동완성 검색
auto_response = list(search_client.suggest('가중', 'testSuggester'))
# 일반 검색
response = list(search_client.search('가중'))
# 데이터 예제
{
"testId": "4",
"testRate": 4.5,
"description" : "가중치 +0 테스트"
"testTag": [
"성공",
"가중치",
"검색"
]
},
{
"testId": "6",
"testRate": 3.5,
"description" : "가중치 +1 테스트"
"testTag": [
"실패",
"종료"
]
}
위 코드에서 "description"과 "testTag"를 대상 필드로 지정하였으므로
자동 완성 단어는 두 필드를 기준으로 생성하여 결과를 보내주게 됩니다.
'Azure > Cognitive Search' 카테고리의 다른 글
[Azure] Cognitive Search Vector Search 벡터 유사도 검색(Python) (0) | 2023.07.11 |
---|---|
[Azure] Cognitive Search 검색 스코어링 프로파일 설정 (Python) (0) | 2023.07.09 |
[Azure] Cognitive Search로 검색 및 필터 설정 (Python) (0) | 2023.07.05 |
[Azure] Cognitive Search Index 생성, 삭제, 업로드 (Python) (0) | 2023.07.04 |
[Azure] Cognitive Search Index Field 종류 (Python) (0) | 2023.07.04 |
Comments