mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-09-06 12:46:31 +00:00
Refactor build_doc
This commit is contained in:
@ -16,7 +16,7 @@ use meilisearch_types::error::ResponseError;
|
|||||||
use meilisearch_types::heed::RoTxn;
|
use meilisearch_types::heed::RoTxn;
|
||||||
use meilisearch_types::index_uid::IndexUid;
|
use meilisearch_types::index_uid::IndexUid;
|
||||||
use meilisearch_types::keys::actions;
|
use meilisearch_types::keys::actions;
|
||||||
use meilisearch_types::milli::prompt::{get_document, OwnedFields};
|
use meilisearch_types::milli::prompt::{build_doc, build_doc_fields, OwnedFields};
|
||||||
use meilisearch_types::milli::vector::db::IndexEmbeddingConfig;
|
use meilisearch_types::milli::vector::db::IndexEmbeddingConfig;
|
||||||
use meilisearch_types::milli::vector::json_template::{self, JsonTemplate};
|
use meilisearch_types::milli::vector::json_template::{self, JsonTemplate};
|
||||||
use meilisearch_types::milli::vector::EmbedderOptions;
|
use meilisearch_types::milli::vector::EmbedderOptions;
|
||||||
@ -532,12 +532,18 @@ async fn render(index: Index, query: RenderQuery) -> Result<RenderResult, Respon
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(document_id) = input.document_id {
|
if let Some(document_id) = input.document_id {
|
||||||
let (document, fields) = get_document(&index, &rtxn, &document_id, insert_fields)?
|
if insert_fields {
|
||||||
.ok_or_else(|| DocumentNotFound(document_id))?;
|
let fid_map_with_meta = index.fields_ids_map_with_metadata(&rtxn)?;
|
||||||
|
let (document, fields) =
|
||||||
object.insert("doc".into(), document);
|
build_doc_fields(&index, &rtxn, &document_id, &fid_map_with_meta)?
|
||||||
if let Some(fields) = fields {
|
.ok_or_else(|| DocumentNotFound(document_id))?;
|
||||||
|
object.insert("doc".into(), document);
|
||||||
object.insert("fields".into(), fields);
|
object.insert("fields".into(), fields);
|
||||||
|
} else {
|
||||||
|
let fid_map = index.fields_ids_map(&rtxn)?;
|
||||||
|
let document = build_doc(&index, &rtxn, &document_id, &fid_map)?
|
||||||
|
.ok_or_else(|| DocumentNotFound(document_id))?;
|
||||||
|
object.insert("doc".into(), document);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ pub use self::context::Context;
|
|||||||
use crate::fields_ids_map::metadata::FieldIdMapWithMetadata;
|
use crate::fields_ids_map::metadata::FieldIdMapWithMetadata;
|
||||||
use crate::update::del_add::DelAdd;
|
use crate::update::del_add::DelAdd;
|
||||||
use crate::update::new::document::DocumentFromDb;
|
use crate::update::new::document::DocumentFromDb;
|
||||||
use crate::{GlobalFieldsIdsMap, Index, MetadataBuilder};
|
use crate::{FieldsIdsMap, GlobalFieldsIdsMap, Index};
|
||||||
|
|
||||||
pub struct Prompt {
|
pub struct Prompt {
|
||||||
template: liquid::Template,
|
template: liquid::Template,
|
||||||
@ -167,18 +167,49 @@ fn truncate(s: &mut String, max_bytes: usize) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the liquid objects corresponding to the `doc` and `fields` object of a [`Prompt`] from the given external document id.
|
/// Build the liquid objects corresponding to the `doc` object (without `fields`) of a [`Prompt`] from the given external document id.
|
||||||
pub fn get_document(
|
pub fn build_doc(
|
||||||
index: &Index,
|
index: &Index,
|
||||||
rtxn: &RoTxn<'_>,
|
rtxn: &RoTxn<'_>,
|
||||||
external_id: &str,
|
external_id: &str,
|
||||||
with_fields: bool,
|
fid_map: &FieldsIdsMap,
|
||||||
|
) -> Result<Option<LiquidValue>, crate::Error> {
|
||||||
|
_build_doc_fields(index, rtxn, external_id, fid_map, None)
|
||||||
|
.map(|opt| opt.map(|(doc, _fields)| doc))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build the liquid objects corresponding to the `doc` and `fields` object of a [`Prompt`] from the given external document id.
|
||||||
|
pub fn build_doc_fields(
|
||||||
|
index: &Index,
|
||||||
|
rtxn: &RoTxn<'_>,
|
||||||
|
external_id: &str,
|
||||||
|
fid_map_with_meta: &FieldIdMapWithMetadata,
|
||||||
|
) -> Result<Option<(LiquidValue, LiquidValue)>, crate::Error> {
|
||||||
|
_build_doc_fields(
|
||||||
|
index,
|
||||||
|
rtxn,
|
||||||
|
external_id,
|
||||||
|
fid_map_with_meta.as_fields_ids_map(),
|
||||||
|
Some(fid_map_with_meta),
|
||||||
|
)
|
||||||
|
.map(|opt| {
|
||||||
|
opt.map(|(doc, fields)| {
|
||||||
|
(doc, fields.expect("fid_map_with_meta were provided so fields must be Some"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _build_doc_fields(
|
||||||
|
index: &Index,
|
||||||
|
rtxn: &RoTxn<'_>,
|
||||||
|
external_id: &str,
|
||||||
|
fid_map: &FieldsIdsMap,
|
||||||
|
fid_map_with_meta: Option<&FieldIdMapWithMetadata>,
|
||||||
) -> Result<Option<(LiquidValue, Option<LiquidValue>)>, crate::Error> {
|
) -> Result<Option<(LiquidValue, Option<LiquidValue>)>, crate::Error> {
|
||||||
let Some(internal_id) = index.external_documents_ids().get(rtxn, external_id)? else {
|
let Some(internal_id) = index.external_documents_ids().get(rtxn, external_id)? else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let fid_map = index.fields_ids_map(rtxn)?;
|
|
||||||
let Some(document_from_db) = DocumentFromDb::new(internal_id, rtxn, index, &fid_map)? else {
|
let Some(document_from_db) = DocumentFromDb::new(internal_id, rtxn, index, &fid_map)? else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
@ -186,11 +217,8 @@ pub fn get_document(
|
|||||||
let doc_alloc = Bump::new();
|
let doc_alloc = Bump::new();
|
||||||
let parseable_document = ParseableDocument::new(document_from_db, &doc_alloc);
|
let parseable_document = ParseableDocument::new(document_from_db, &doc_alloc);
|
||||||
|
|
||||||
if with_fields {
|
if let Some(fid_map_with_meta) = fid_map_with_meta {
|
||||||
let metadata_builder = MetadataBuilder::from_index(index, rtxn)?;
|
let fields = OwnedFields::new(&parseable_document, fid_map_with_meta);
|
||||||
let fid_map_with_meta = FieldIdMapWithMetadata::new(fid_map.clone(), metadata_builder);
|
|
||||||
let fields = OwnedFields::new(&parseable_document, &fid_map_with_meta);
|
|
||||||
|
|
||||||
Ok(Some((parseable_document.to_value(), Some(fields.to_value()))))
|
Ok(Some((parseable_document.to_value(), Some(fields.to_value()))))
|
||||||
} else {
|
} else {
|
||||||
Ok(Some((parseable_document.to_value(), None)))
|
Ok(Some((parseable_document.to_value(), None)))
|
||||||
|
Reference in New Issue
Block a user