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,
|
engine: &rhai::Engine,
|
||||||
edit_function: &rhai::AST,
|
edit_function: &rhai::AST,
|
||||||
doc_alloc: &'doc bumpalo::Bump,
|
doc_alloc: &'doc bumpalo::Bump,
|
||||||
) -> Result<Option<Self>> {
|
) -> Result<Option<Option<Self>>> {
|
||||||
let Some(data) = versions.next() else { return Ok(None) };
|
let Some(data) = versions.next() else { return Ok(None) };
|
||||||
|
|
||||||
let mut doc = doc.unwrap_or_default();
|
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();
|
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
|
||||||
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
|
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);
|
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
|
||||||
serde_json::to_writer(&mut vec, &value).unwrap();
|
serde_json::to_writer(&mut vec, &value).unwrap();
|
||||||
let key = doc_alloc.alloc_str(key.as_str());
|
let key = doc_alloc.alloc_str(key.as_str());
|
||||||
@ -439,6 +441,11 @@ impl<'doc> Versions<'doc> {
|
|||||||
data.insert(key, raw_value);
|
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
|
// We must also run the code after the last change
|
||||||
let mut scope = rhai::Scope::new();
|
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();
|
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
|
||||||
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
|
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);
|
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
|
||||||
serde_json::to_writer(&mut vec, &value).unwrap();
|
serde_json::to_writer(&mut vec, &value).unwrap();
|
||||||
let key = doc_alloc.alloc_str(key.as_str());
|
let key = doc_alloc.alloc_str(key.as_str());
|
||||||
let raw_value = serde_json::from_slice(vec.into_bump_slice()).unwrap();
|
let raw_value = serde_json::from_slice(vec.into_bump_slice()).unwrap();
|
||||||
data.insert(key, raw_value);
|
data.insert(key, raw_value);
|
||||||
}
|
}
|
||||||
|
Ok(Some(Some(Self::single(data))))
|
||||||
Ok(Some(Self::single(data)))
|
}
|
||||||
|
None => Ok(Some(None)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn single(version: RawMap<'doc, FxBuildHasher>) -> Self {
|
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.
|
/// Returns only the most recent version of a document based on the updates from the payloads.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn merge<'doc>(
|
fn merge<'doc>(
|
||||||
&self,
|
&self,
|
||||||
rtxn: &heed::RoTxn,
|
rtxn: &heed::RoTxn,
|
||||||
@ -587,7 +588,18 @@ impl<'pl> PayloadOperations<'pl> {
|
|||||||
.get(rtxn, &self.docid)?
|
.get(rtxn, &self.docid)?
|
||||||
.map(|obkv| obkv_to_rhaimap(obkv, fidmap))
|
.map(|obkv| obkv_to_rhaimap(obkv, fidmap))
|
||||||
.transpose()?;
|
.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)?,
|
None => Versions::multiple(versions)?,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user