我正在将弹性搜索与我的 Nest 应用程序集成。在下面提出我的工作流程和问题。
流量:
解决方案:
refresh:'wait-for'
配置。正如您所知,Elasticsearch 提供近实时搜索,如果您正在进行搜索调用,一旦文档被索引到 Elasticsearch,除非索引上发生
refresh
,否则该文档将不可用。
现在,正如您所提到的,您可以通过在索引/更新操作中使用
wait_for
来解决问题,唯一的缺点是它会占用大量资源,如果您在大型索引上频繁执行此操作,可能会导致严重的性能问题你的集群。
您的第二个选项将不起作用,因为您仍然可以在刷新完成之前从 FE 查询 Elasticsearch,并且您将获得过时的数据。
我遇到了类似的问题,需要输入至少 1 秒的等待时间。 我尝试了0.5秒,无法正确检索数据。 这对我有帮助:
# Function to retrieve the balance for a specific patient
def get_patient_balance(es_client, STRUCTURED_INDEX_NAME, patient_name):
query = {
"query": {
"match": {
"Patient Name": patient_name
}
}
}
response = es_client.search(index=STRUCTURED_INDEX_NAME, body=query)
if response['hits']['total']['value'] > 0:
return response['hits']['hits'][0]['_source']['Balance Remaining']
else:
return None
然后输入:
# Wait for a short period to ensure data is indexed
time.sleep(1)
# Now run the query
patient_3_balance = get_patient_balance(es_client, STRUCTURED_INDEX_NAME, 'Patient_3')
if patient_3_balance is not None:
print(f"Patient_3 Balance Remaining: {patient_3_balance:.2f}")
else:
print("Patient_3 Balance Remaining: Data not found")
patient_39_balance = get_patient_balance(es_client, STRUCTURED_INDEX_NAME, 'Patient_39')
if patient_39_balance is not None:
print(f"Patient_39 Balance Remaining: {patient_39_balance:.2f}")
else:
print("Patient_39 Balance Remaining: Data not found")
if patient_3_balance is not None and patient_39_balance is not None:
step_1 = patient_3_balance
print(f"Step 1: {step_1:.2f}")
step_2 = step_1 + patient_39_balance
print(f"Step 2: {step_1:.2f} + {patient_39_balance:.2f} = {step_2:.2f}")
combined_balance = step_2
print(f"The combined remaining balance of Patient_3 and Patient_39 is: {combined_balance:.2f}")
else:
print("Unable to calculate combined balance due to missing data")
Patient_39 Balance Remaining: 3018.84
Step 1: 37.34
Step 2: 37.34 + 3018.84 = 3056.18
The combined remaining balance of Patient_3 and Patient_39 is: 3056.18
之前它说“找不到数据”...