quickwit
This commit is contained in:
@@ -2,11 +2,13 @@ use std::collections::HashSet;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use tracing::{debug, error};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::{Document, DocumentVersion};
|
||||
|
||||
pub const QUICKWIT_MAX_HITS: usize = 200;
|
||||
|
||||
pub fn build_quickwit_query(input: &str) -> Option<String> {
|
||||
@@ -164,3 +166,69 @@ struct QuickwitSearchResponse {
|
||||
#[serde(default)]
|
||||
hits: Vec<Value>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct QuickwitIngestRecord {
|
||||
pub document_id: Uuid,
|
||||
pub version_id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub title: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
pub fn build_quickwit_ingest_record(
|
||||
document: &Document,
|
||||
version: &DocumentVersion,
|
||||
tenant_id: Uuid,
|
||||
text: &str,
|
||||
) -> QuickwitIngestRecord {
|
||||
QuickwitIngestRecord {
|
||||
document_id: document.id,
|
||||
version_id: version.id,
|
||||
tenant_id,
|
||||
title: document.title.to_lowercase(),
|
||||
text: text.to_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn quickwit_ingest(
|
||||
client: &Client,
|
||||
endpoint: &str,
|
||||
index: &str,
|
||||
records: &[QuickwitIngestRecord],
|
||||
) -> Result<()> {
|
||||
if records.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let url = format!(
|
||||
"{}/api/v1/{}/ingest?commit=auto",
|
||||
endpoint.trim_end_matches('/'),
|
||||
index
|
||||
);
|
||||
|
||||
let mut body = String::new();
|
||||
for record in records {
|
||||
let line = serde_json::to_string(record)?;
|
||||
body.push_str(&line);
|
||||
body.push('\n');
|
||||
}
|
||||
|
||||
debug!(%url, lines = records.len(), "sending quickwit ingest request");
|
||||
let response = client
|
||||
.post(url)
|
||||
.header("content-type", "application/x-ndjson")
|
||||
.body(body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
error!(%status, %body, "quickwit ingest request failed");
|
||||
return Err(anyhow!("quickwit ingest failed with status {status}: {body}"));
|
||||
}
|
||||
|
||||
debug!("quickwit ingest request succeeded");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user