mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-05-14 16:23:57 +02:00
Allow users to delete documents
This commit is contained in:
parent
446b9c142c
commit
c8b7822d0d
@ -412,7 +412,7 @@ impl<'doc> Versions<'doc> {
|
||||
engine: &rhai::Engine,
|
||||
edit_function: &rhai::AST,
|
||||
doc_alloc: &'doc bumpalo::Bump,
|
||||
) -> Result<Option<Self>> {
|
||||
) -> Result<Option<Option<Self>>> {
|
||||
let Some(data) = versions.next() else { return Ok(None) };
|
||||
|
||||
let mut doc = doc.unwrap_or_default();
|
||||
@ -431,7 +431,9 @@ impl<'doc> Versions<'doc> {
|
||||
|
||||
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
|
||||
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
|
||||
for (key, value) in scope.get_value::<rhai::Map>("doc").unwrap() {
|
||||
match scope.get_value::<Option<rhai::Map>>("doc").unwrap() {
|
||||
Some(map) => {
|
||||
for (key, value) in map {
|
||||
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
|
||||
serde_json::to_writer(&mut vec, &value).unwrap();
|
||||
let key = doc_alloc.alloc_str(key.as_str());
|
||||
@ -439,6 +441,11 @@ impl<'doc> Versions<'doc> {
|
||||
data.insert(key, raw_value);
|
||||
}
|
||||
}
|
||||
// In case the deletes the document and it's not the last change
|
||||
// we simply set the document to an empty one and await the next change.
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
// We must also run the code after the last change
|
||||
let mut scope = rhai::Scope::new();
|
||||
@ -449,15 +456,19 @@ impl<'doc> Versions<'doc> {
|
||||
|
||||
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
|
||||
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
|
||||
for (key, value) in scope.get_value::<rhai::Map>("doc").unwrap() {
|
||||
match scope.get_value::<Option<rhai::Map>>("doc").unwrap() {
|
||||
Some(map) => {
|
||||
for (key, value) in map {
|
||||
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
|
||||
serde_json::to_writer(&mut vec, &value).unwrap();
|
||||
let key = doc_alloc.alloc_str(key.as_str());
|
||||
let raw_value = serde_json::from_slice(vec.into_bump_slice()).unwrap();
|
||||
data.insert(key, raw_value);
|
||||
}
|
||||
|
||||
Ok(Some(Self::single(data)))
|
||||
Ok(Some(Some(Self::single(data))))
|
||||
}
|
||||
None => Ok(Some(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn single(version: RawMap<'doc, FxBuildHasher>) -> Self {
|
||||
|
@ -510,6 +510,7 @@ impl<'pl> PayloadOperations<'pl> {
|
||||
}
|
||||
|
||||
/// Returns only the most recent version of a document based on the updates from the payloads.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn merge<'doc>(
|
||||
&self,
|
||||
rtxn: &heed::RoTxn,
|
||||
@ -587,7 +588,18 @@ impl<'pl> PayloadOperations<'pl> {
|
||||
.get(rtxn, &self.docid)?
|
||||
.map(|obkv| obkv_to_rhaimap(obkv, fidmap))
|
||||
.transpose()?;
|
||||
Versions::multiple_with_edits(doc, versions, engine, ast, doc_alloc)?
|
||||
match Versions::multiple_with_edits(doc, versions, engine, ast, doc_alloc)?
|
||||
{
|
||||
Some(Some(versions)) => Some(versions),
|
||||
Some(None) if self.is_new => return Ok(None),
|
||||
Some(None) => {
|
||||
return Ok(Some(DocumentChange::Deletion(Deletion::create(
|
||||
self.docid,
|
||||
external_doc,
|
||||
))));
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
None => Versions::multiple(versions)?,
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user