Пример: Обогащение данных на основе точных значений
match политики обогащения сопоставляют данные обогащения с поступающими документами на основе точного значения, например, адреса электронной почты или идентификатора, используя term запрос.
Следующий пример создаёт match политику обогащения, которая добавляет имя пользователя и контактную информацию в поступающие документы на основе адреса электронной почты. Затем она добавляет match политику обогащения в процессор в конвейере обработки данных.
Используйте API создания индекса или API индексирования, чтобы создать исходный индекс.
Следующий запрос API индексирования создаёт исходный индекс и индексирует новый документ в этом индексе.
resp = client.index(
index="users",
id="1",
refresh="wait_for",
document={
"email": "mardy.brown@asciidocsmith.com",
"first_name": "Mardy",
"last_name": "Brown",
"city": "New Orleans",
"county": "Orleans",
"state": "LA",
"zip": 70116,
"web": "mardy.asciidocsmith.com"
},
)
print(resp) response = client.index(
index: 'users',
id: 1,
refresh: 'wait_for',
body: {
email: 'mardy.brown@asciidocsmith.com',
first_name: 'Mardy',
last_name: 'Brown',
city: 'New Orleans',
county: 'Orleans',
state: 'LA',
zip: 70_116,
web: 'mardy.asciidocsmith.com'
}
)
puts response const response = await client.index({
index: "users",
id: 1,
refresh: "wait_for",
document: {
email: "mardy.brown@asciidocsmith.com",
first_name: "Mardy",
last_name: "Brown",
city: "New Orleans",
county: "Orleans",
state: "LA",
zip: 70116,
web: "mardy.asciidocsmith.com",
},
});
console.log(response); PUT /users/_doc/1?refresh=wait_for
{
"email": "mardy.brown@asciidocsmith.com",
"first_name": "Mardy",
"last_name": "Brown",
"city": "New Orleans",
"county": "Orleans",
"state": "LA",
"zip": 70116,
"web": "mardy.asciidocsmith.com"
} Используйте API создания политики обогащения, чтобы создать политику обогащения с типом политики match. Эта политика должна включать:
- Один или несколько исходных индексов
-
match_field, поле из исходных индексов, используемое для сопоставления с поступающими документами - Поля обогащения из исходных индексов, которые вы хотите добавить к поступающим документам
resp = client.enrich.put_policy(
name="users-policy",
match={
"indices": "users",
"match_field": "email",
"enrich_fields": [
"first_name",
"last_name",
"city",
"zip",
"state"
]
},
)
print(resp) response = client.enrich.put_policy(
name: 'users-policy',
body: {
match: {
indices: 'users',
match_field: 'email',
enrich_fields: [
'first_name',
'last_name',
'city',
'zip',
'state'
]
}
}
)
puts response const response = await client.enrich.putPolicy({
name: "users-policy",
match: {
indices: "users",
match_field: "email",
enrich_fields: ["first_name", "last_name", "city", "zip", "state"],
},
});
console.log(response); PUT /_enrich/policy/users-policy
{
"match": {
"indices": "users",
"match_field": "email",
"enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
}
} Используйте API выполнения политики обогащения, чтобы создать индекс обогащения для политики.
POST /_enrich/policy/users-policy/_execute?wait_for_completion=false
Используйте API создания или обновления конвейера, чтобы создать конвейер обработки данных. В конвейере добавьте процессор обогащения, который включает:
- Вашу политику обогащения.
-
fieldпоступающих документов, используемый для сопоставления документов из индекса обогащения. -
target_fieldдля хранения добавленных данных обогащения для поступающих документов. Это поле содержитmatch_fieldиenrich_fields, указанные в вашей политике обогащения.
resp = client.ingest.put_pipeline(
id="user_lookup",
processors=[
{
"enrich": {
"description": "Add 'user' data based on 'email'",
"policy_name": "users-policy",
"field": "email",
"target_field": "user",
"max_matches": "1"
}
}
],
)
print(resp) const response = await client.ingest.putPipeline({
id: "user_lookup",
processors: [
{
enrich: {
description: "Add 'user' data based on 'email'",
policy_name: "users-policy",
field: "email",
target_field: "user",
max_matches: "1",
},
},
],
});
console.log(response); PUT /_ingest/pipeline/user_lookup
{
"processors" : [
{
"enrich" : {
"description": "Add 'user' data based on 'email'",
"policy_name": "users-policy",
"field" : "email",
"target_field": "user",
"max_matches": "1"
}
}
]
} Используйте конвейер обработки данных для индексирования документа. Поступающий документ должен включать field, указанный в вашем процессоре обогащения.
resp = client.index(
index="my-index-000001",
id="my_id",
pipeline="user_lookup",
document={
"email": "mardy.brown@asciidocsmith.com"
},
)
print(resp) const response = await client.index({
index: "my-index-000001",
id: "my_id",
pipeline: "user_lookup",
document: {
email: "mardy.brown@asciidocsmith.com",
},
});
console.log(response); PUT /my-index-000001/_doc/my_id?pipeline=user_lookup
{
"email": "mardy.brown@asciidocsmith.com"
} Чтобы проверить, что процессор обогащения сопоставил и добавил соответствующие данные поля, используйте API получения, чтобы просмотреть индексированный документ.
resp = client.get(
index="my-index-000001",
id="my_id",
)
print(resp) response = client.get( index: 'my-index-000001', id: 'my_id' ) puts response
const response = await client.get({
index: "my-index-000001",
id: "my_id",
});
console.log(response); GET /my-index-000001/_doc/my_id
API возвращает следующий ответ:
{
"found": true,
"_index": "my-index-000001",
"_id": "my_id",
"_version": 1,
"_seq_no": 55,
"_primary_term": 1,
"_source": {
"user": {
"email": "mardy.brown@asciidocsmith.com",
"first_name": "Mardy",
"last_name": "Brown",
"zip": 70116,
"city": "New Orleans",
"state": "LA"
},
"email": "mardy.brown@asciidocsmith.com"
}
}
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/match-enrich-policy-type.html